Let's talk about the dry principle.
Don't repeat yourself.
The Dry principle says that you should not duplicate your code.
Now again, this is best explained with an example.
Here we have a discount calculator.
Let's just switch to that class.
And this discount calculator has three methods.
Each of them just calculates the discount for a specific kind of person.
Here the first method we calculate the discount for a regular customer here for a premium customer,
and here for an employee.
Each of those methods nearly has the same statements here.
Right.
So this is a lot of duplication here.
We just check if the total amount of that price that the customer is paying is higher than 1000.
If so, he will get a bigger discount.
If it's below 1000 bucks, he will get a smaller discount.
So those values are changing.
So regular customer gets less discount than an employee for example.
So let's apply the Dry principle and think about how we could solve that.
How we could reduce the amount of code and how to improve the maintainability of our program.
Because the thing is that as soon as you want to change something, let's say, okay, the threshold
where you would want to apply the bigger discount, which is now set to 1000, you want to change it
to 800.
You have to change it three times here, here and here again.
And I can guarantee that this will sooner or later cause a bug, because someone forgets to change 1000
to 800 in one of those three places.
Okay, let's think about how we can apply the Dry principle here.
So how we can stop repeating ourselves.
So we have three methods, each of them for different kind of customer or different type of customer.
A good data structure to use here would be an enum, because we could switch for different customer
types.
So we would have one algorithm that checks if he is a regular customer do this.
If he is another customer type, do that.
So let's create an enum.
Usually I will put that into its own file, but for the sake of this video I will put it here at the
top public enum customer type.
For that enum, we will now specify the types that we have regular customer, premium customer, and
finally the employee customer.
There we go.
Now let's reduce our three methods that we have right here to a single one.
And inside of that method we will use our customer type.
So let's create a public double here.
Call it calculate discount.
We receive the customer type.
Call it customer type and we receive the double total amount of whatever the customer has to pay.
Now we will create the variable here.
Inside double discount equals to zero.
And then we will switch for the customer type that we have.
And then create cases for every customer type that we have, including its own calculation.
Before we do that we want to get rid of that 1000 uh magic number here.
Let's create a const here.
That will be a good example for using a const.
So let's go ahead and write down a private const of type integer for example.
Or double or float whatever you want to use.
And let's call it discount threshold.
Set the value to 1000.
Great.
Okay.
Let's start working on that switch here.
The first case that we have is that we have the case customer type dot regular.
And he will set the discount.
Just call it discount.
Go discount equals two.
Now we check if the total amount from our parameter is bigger or equals.
It really depends on the algorithm right here it says it has to be bigger.
Could also be equals.
But let's stick with that.
It's bigger than our discount threshold.
So 1000 for example.
If it is we want to return 10% discount.
So 0.1.
And if it's not we want to return 0.05.
So 5% discount.
Great.
Finally let's add a break here to break our case.
Then let's just duplicate that two times regular change it to premium customer, change the values from
15 and from 15.
Here 15 and ten.
There we go.
And then we have the.
Employee customer change it to 20 and 15.
So 20 and 15 you can do it that way.
Great.
So finally let's return our discount here.
Right.
We create that um variable inside of our method.
Then we calculate the value depending on each customer type.
Then we use a const here for that.
Then we have other values here that we just have to change for every specific customer type.
And then we return our discount that we have calculated.
Now let's remove all of that.
There we go.
We have now reduced that to a single method.
Just take a look like these have been somewhat ten lines of code or eight or what per method.
So that would be like 30 lines of code or what.
And we have massively reduced that.
But also we have reduced the chances that someone will add a bug, because if we now wants to change
the calculation of a discount, he will simply come to that method.
He will see everything right at once instead of one method.
And if he needs to change the algorithm for that, he can simply adjust the numbers right here.
Or that const value right over here at the top.
Great.
Save it like this and then go to the program.
So now you can see that we for sure cannot call these methods anymore.
We will now use our new method.
So calculate discount.
And then inside here as the first parameter we will provide a customer type let's say customer type
dot regular for the first one.
Right.
And then we just use the method everywhere here.
And we use different customer types.
So there we go.
Let's switch it to premium.
That was a premium customer here.
And this one has been a employee customer.
So we simply provide one more parameter.
But therefore we gain clean code right.
So let's start our application.
Take a look at the console.
Output should work fine.
And this is how you can apply the Dry principle for your code right.
Regular customer discount.
There we go.
All right so you can see we got 10% discount here 10% here.
It really depends on the value that we submit here.
What we also could do just a small addition would be instead of returning the discount only we could
also return the value of how much discount does he get like depending on the total amount.
So we would say total amount times discount.
So that would be the amount of money that he would be saving.
Right.
That would really depend on your use case.
But then that way we will be able to see the actual value that he will save for his purchase.
And that was one applied example of how to follow the Dry principle.