Week 11 Activity
Week 11 Activity
Tutorial Activity: 11
Advantages of DRY
Less work
By not repeating code, you have less work to do. If you have a logic that spans 10
lines and you extract it to a method. You will need 1 line instead of 10 lines at every
occasion where you use it.
Better maintainability
There is always a chance that your code will need to be changed because a bug is
found and you need to fix it or a change request came in. If you have the code in
only one place, it’s much easier to modify as you only have to do it once.
Better testability
If you extract a piece of code to a well-separated and reusable unit, then unit testing
it is easy. You can you test the extracted code and you can mock that logic at every
occurrence.
If you have that block of code in several places then you have to take it into
account at all of its occurrences. So using DRY makes testing easier.
Better readability
As a result of following DRY, you will have less and better-structured code that is
easier to follow.
Disadvantages of DRY
DRY is a great principle to follow but if it is overused it can be a source of issues. If
you are trying too hard to make sure that nothing is repeated, it is possible that your
code will become too complicated and hard to understand.
You have to weigh the benefits of DRY. Sometimes it’s better to repeat some code if
it clearly helps readability a lot.
DRY Principle
The DRY principle stands for the Don't Repeat Yourself principle. It is one of the
common principles for all programming languages. The DRY principle says:
Both Dog and Cat speak differently, but they need to eat food. The common
functionality in both of them is Eating food so, we can put this common functionality
into the parent class, i.e., Animals, and then we can extend that parent class in the child
class, i.e., Dog and Cat.
Now, each class can focus on its own unique logic, so there is no need to implement the
same functionality in both classes.
After compile and run the above code, we will see the following output:
Output:
Eat food
Cat Meow!
Eat food
Dog Woof!
Violations of the DRY principles are referred to as WET solutions. WET is an abbreviation
for the following things:
2. We enjoy typing
These violations are not always bad because repetitions are sometimes advisable to
make code less inter-dependent, more readable, inherently dissimilar classes, etc.
means don’t write the same code repeatedly. If we have a block of code at two or more
value at two or more places than make them public final constant.
Let us understand the DRY principle with help of below example. In the below example
Example:
public class Mechanic {
public void serviceBus() {
System.out.println("Servicing bus now");
}
public void serviceCar() {
System.out.println("Servicing car now");
}
}
Mechanic class have two methods serviceBus() and serviceCar() which will perform
respective tasks. Now consider the case when workshop is offering you other services
like washing the vehicle after service. After updating the code in Mechanic class.