This is the third post in my Know your JAVA better series, and its after a long time I am writing about this again. I realized a need to write this because of some recent experiences. One fine day I was just going through code of a product and realized code is too long there are excessive use of if and loops, it was like developer was practicing his typing skills.
Centralize code What does it mean by practicing typing skills is that if you can write a code in a centralized location and reuse it, it does reduce your typing, in actual code will execute exact same instructions, it will also increase readability of code but if you rewrite it again and again instead, it nothing but you are practicing typing.
For example, below code can be easily modified to later
Modified code:
This way you encapsulated child search in your parent class itself, this way you not also reduced line of code but it is more readable, error proof (null checks and all can be in one place) and reusable.
Coding in your Beans This is same as point above but also some mutability. In this kind of change you provide mutable methods in your bean that don't only do what their name suggest but also modify the relevant status. For eg:
Now these line of code can be simply reduced to below
and its implementation will cover rest of the obvious things
Now above code is just for example, setStatus or hasChildrent can simply not required as these can be calculated based on childrent.size();
Non default constructor: you can reduce number of setter method calls if you provide a constructor of your pojos, instead of creating pojo with its default constructor and then setting individual property you can provide additional constructor to set those properties in one place for convenience.
At last, enjoy coding but not typing. Think of reusability.
Centralize code What does it mean by practicing typing skills is that if you can write a code in a centralized location and reuse it, it does reduce your typing, in actual code will execute exact same instructions, it will also increase readability of code but if you rewrite it again and again instead, it nothing but you are practicing typing.
For example, below code can be easily modified to later
Collection col=parent.getChildren();
Iterator itr=col.iterator();
while(itr.hasNext()){
Child ch-(Child)itr.next();
if("John".equals(ch.getName()))
return ch;
}
return null;
Modified code:
return parent.getChildWithName("John");
This way you encapsulated child search in your parent class itself, this way you not also reduced line of code but it is more readable, error proof (null checks and all can be in one place) and reusable.
Coding in your Beans This is same as point above but also some mutability. In this kind of change you provide mutable methods in your bean that don't only do what their name suggest but also modify the relevant status. For eg:
Collection col=person.getChildren();
col.add(new Child("Jane","21/05/1986",Gender.F)); // Child(String name,String dob,Gender g);
person.setStatus("Parent");
person.hasChildren(true);
Now these line of code can be simply reduced to below
person.addChild(new Child("Jane","21/05/1986",Gender.F));
and its implementation will cover rest of the obvious things
pubic class Person{
.
.
.
public void addChild(Child ch){
this.children.add(ch);
this.setStatus("Parent");
this.hasChildren(true);
}
Now above code is just for example, setStatus or hasChildrent can simply not required as these can be calculated based on childrent.size();
Non default constructor: you can reduce number of setter method calls if you provide a constructor of your pojos, instead of creating pojo with its default constructor and then setting individual property you can provide additional constructor to set those properties in one place for convenience.
At last, enjoy coding but not typing. Think of reusability.
Comments
Post a Comment