0% found this document useful (0 votes)
71 views

Week 11 Activity

The DRY principle states that each piece of logic in a system should have a single representation to avoid redundancy. It stands for "Don't Repeat Yourself" and means avoiding duplicate code. Benefits of following DRY include less work, better maintainability, fewer bugs, improved testability, and better readability. However, overusing DRY can make code too complicated; sometimes repetition is needed for clarity. The example shows applying DRY by defining a common "eatFood()" method in a parent "Animal" class that is inherited by "Dog" and "Cat" subclasses.

Uploaded by

kashinath.kb
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views

Week 11 Activity

The DRY principle states that each piece of logic in a system should have a single representation to avoid redundancy. It stands for "Don't Repeat Yourself" and means avoiding duplicate code. Benefits of following DRY include less work, better maintainability, fewer bugs, improved testability, and better readability. However, overusing DRY can make code too complicated; sometimes repetition is needed for clarity. The example shows applying DRY by defining a common "eatFood()" method in a parent "Animal" class that is inherited by "Dog" and "Cat" subclasses.

Uploaded by

kashinath.kb
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

20CS43P : Object Oriented Programming and Design with Java

Tutorial Activity: 11

1. Study DRY principle, identify the benefits..

DRY is simply an approach, or we can say, a different perspective to


programmers. DRY stands for Don’t Repeat Yourself. In Java, it means don’t
write the same code repeatedly. Suppose you are having the same code at
many places in your program, then it means you are not following the DRY
approach; You are repeating the same code at different places. Hence, the
solution is obtained using the DRY concept by placing the methods in place of
all repeated codes and defining the code in one method. So by calling methods,
we will reach the principle DRY. The DRY concept is very important to make the
code better by reducing code redundancy and to encourage its reusability.
Applications:
• Online marketing applications
• Education
• Financial applications

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.

Less chance to break the code


If you have to do a change in 10 places, there is a chance that you’ll make a
mistake. If the logic is only in 1 place, you have a much higher chance of getting it
right.

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.

Kashinath Bedare, Lecturer in Govt. Women Polytechnic, Shiralakoppa. Page 1


20CS43P : Object Oriented Programming and Design with Java

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.

Kashinath Bedare, Lecturer in Govt. Women Polytechnic, Shiralakoppa. Page 2


20CS43P : Object Oriented Programming and Design with Java

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:

Within a system, each piece of logic should have a single unambiguous


representation.

Let's take an example of the DRY principle

1. public class Animal {


2. public void eatFood() {
3. System.out.println("Eat Food");
4. }
5. }
6.
7. public class Dog extends Animal {
8. public void woof() {
9. System.out.println("Dog Woof! ");
10. }
11. }
12. public class Cat extends Animal {
13. public void meow() {
14. System.out.println("Cat Meow!");
15. }
16. }

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.

1. Dog obj1 = new Dog();


2. obj1.eatFood();
3. obj1.woof();
4.

Kashinath Bedare, Lecturer in Govt. Women Polytechnic, Shiralakoppa. Page 3


20CS43P : Object Oriented Programming and Design with Java

5. Cat obj2 = new Cat();


6. obj2.eatFood();
7. obj2.meow();

After compile and run the above code, we will see the following output:

Output:

Eat food
Cat Meow!
Eat food
Dog Woof!

Violation of the DRY Principle

Violations of the DRY principles are referred to as WET solutions. WET is an abbreviation
for the following things:

1. Write everything twice

2. We enjoy typing

3. Write every time

4. Waste everyone's twice.

These violations are not always bad because repetitions are sometimes advisable to
make code less inter-dependent, more readable, inherently dissimilar classes, etc.

DRY (Don’t Repeat Yourself) In Java


DRY object-oriented design principle refers to don’t repeat yourself. In programming it

means don’t write the same code repeatedly. If we have a block of code at two or more

Kashinath Bedare, Lecturer in Govt. Women Polytechnic, Shiralakoppa. Page 4


20CS43P : Object Oriented Programming and Design with Java
places than to place it in a separate method. For constants if we are using a hard-coded

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

we have a Mechanic class which services bus and cars.

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.

public class Mechanic {


public void serviceBus() {
System.out.println("Servicing bus now");
//Process washing
}
public void serviceCar() {
System.out.println("Servicing car now");
//Process washing
}
}
As we can see washing processing code is duplicate. We can put this code in a separate

method and use it wherever required.

public class Mechanic {


public void serviceBus() {
System.out.println("Servicing bus now");
washVehicle();
}
public void serviceCar() {
System.out.println("Servicing car now");
washVehicle();
}
public void washVehicle() {

Kashinath Bedare, Lecturer in Govt. Women Polytechnic, Shiralakoppa. Page 5


20CS43P : Object Oriented Programming and Design with Java
//Process washing
}
}

Kashinath Bedare, Lecturer in Govt. Women Polytechnic, Shiralakoppa. Page 6

You might also like