Don't Repeat Yourself (DRY)
Is your code DRY or WET?
Don’t Repeat Yourself (DRY) is a software development principle, the main aim of which is to minimise repetition of code.
Write Every Time (WET) or Write Everything Twice is a cheeky abbreviation to mean the opposite i.e. code that doesn’t adhere to the DRY principle.
The focus of DRY is to avoid the repetition of information.
Why?
When you write code that performs the same tasks over and over again, any modification of one task requires the same change to be made to every single instance of that task! Editing every instance of a task is a lot of work.
Instead, you can create functions that perform those tasks, using sets of arguments or inputs to specify how the task is performed.
Here is what some WET code looks like:
class Builder
{ int number;
public void addItem (int item) {
number=number + item;
}
public void deleteItem (int item) {
number=number-item;
}
}
class Bridge{
{ int number;
public void addItem(int item) {
number=number+item;
}
public void deleteItem(int item) {
number=number-item;
}
}
Can be refactored into
class Builder
{ int number;
public void addItem(int item) {
number=number+item;
}
public void deleteItem(int item) {
number=number-item;
}
}
class Bridge{
{ int item;
Builder builder= new Builder(); //….
public addItem(int item){
builder.additem(item);
}
}
Example -2
// Program to find the binomial coefficient
import java.util.*;
class NCR {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
// Taking inputs as n and r
System.out.print("Enter the value of n: ");
int n = sc.nextInt();
System.out.print("Enter the value of r: ");
int r = sc.nextInt();
factr=1;
for(int i=1;i<=r;i++{
factr *=i;
}
factr=1;
for(int i=1;i<=n;i++){
factn *=i;
}
factn_r=1;
for(int i=1;i<=n-r;i++{
factn_r *=i;
}
int ncr = factn/(factr*factn_r);
System.out.println(" Binomial Coefficient is = " + ncr);
}
}
Can be refactored as
// Program to find the binomial coefficient
import java.util.*;
class NCR {
int factorial(int number)
{ int fact=1;
for(int i=1; i<=n;i++)
{ fact *=i;
}
return fact;
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
// Taking inputs as n and r
System.out.print("Enter the value of n: ");
int n = sc.nextInt();
System.out.print("Enter the value of r: ");
int r = sc.nextInt();
NCR obj = new NCR();
int ncr = obj.factorial(n)/(obj.factorial(r)*obj.factorial(n-r));
System.out.println(" Binomial Coefficient is = " + ncr);
}
}
The Benefits of Functions
- Modularity: If you write functions for specific individual tasks, you can use them over and over. A function that you write for one script can even be reused in other scripts!
- Fewer global variables: When you run a function, the intermediate variables that it creates are not stored in your global environment. This saves memory and keeps your global environment cleaner.
- Better documentation: Well-documented functions help the user understand the steps of your processing.
- Easier to maintain/edit: When you create a function for a repeated task, it is easy to edit that one function. Then every location in your code where that same task is performed is automatically updated.
Multiple Choice Questions
1. The DRY principle is stated as
a) Every piece of knowledge must have a multiple ambiguous, authoritative representation within a system.
b) Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.
c) Do repeat yourself
d) None of the above.
Answer (b)
2. The following statement is true or false
“The way to achieve DRY is by creating functions and classes to make sure that any logic should be written in only one place”
a) True
b) False
Answer (a)
3. Why DRY is important?
a) Duplication of code can make code very easy to update.
b) Duplication of code can make code very easy to maintain.
c) Duplication of code can make code very difficult to maintain.
d) All
Answer (c)
4. Keep it Simple Stupid has few benefits. Select the correct answer.
a) Less time to write
b) Less chances of bugs
c) Easier to understand, debug and modify.
d) All of the above
Answer (d)
5. The boy-scout rule –
a) Always leave the code behind in a better state than you found it.
b) Always leave the code behind in a worst state than you found it.
c) Both a & b are correct
d) Both a & b are not correct
Answer (a)
6. Curly's Law is about choosing a single, clearly defined goal for any particular bit of code: Do One Thing.
a) False
b) True
Answer (b)
Reference: