Wednesday, January 30, 2013

Removing unnecessary methods with inline code

Note: This holds only for smaller or restricted applications wherein the method logic is called at exactly one place or so. This is strict violation of object-oriented programming principles which advocate that each task or purpose should be served upon by an individual method as it warrants easy extensibility, and maintenance, apart from giving a clean picture of its job.
So, in any application, if we see that there might be future implementation change possibilities or the method is being called or may be called from multiple places in the application, then, even though if the current implementation has it that the method will be called from only one place, we have to put the logic in a separate method with an appropriate name and also mention in comment about the usage, if any.

Objective: "Put a method's logic into the body of its caller and remove the method”.
Why: Too many methods with out any real purpose, and/or many small methods, but not used in multiple places. 
// Following code returns rating for an website
// "A" for more than 1000 visits
// "B" for less than 1000 visits
String getSiteRating(){
 return moreThan1000Visits()?"A":"B";

boolean moreThan1000Visits(){
// totalVisits is a static visit counter defined
// in some other part of the program
 return totalVisits > 1000;
Now the above code has been modified as follows to remove unnecessary method moreThan1000Visits() requirement for one time task:
String getSiteRating(){
 return (totalVisits > 1000)?"A":"B";

This style of code writing not only saves time and code, but also allows the JVM to use resources in other parts of the application rather than executing an avoidable method stack.


  1. Hello, sir i would like to ask that what is the scope of java training, what all topics should be covered and it is kinda bothering me … and has anyone studies from this course of core and advance java online ?? or tell me any other guidance...
    would really appreciate help… and Also i would like to thank for all the information you are providing on java concepts.

  2. Hello Christi, thanks for registering the first comment. If you have a little knowledge about programming (even though very little), then there is no need for any paying classes. You can kick-start your Java learning from FREE online sources.

    The learning route that I'd recommend is:
    1st : Start with core Java.
    2nd : Start with advanced Java.

    The fundamental and most important topics in Core Java syllabus are enlisted here:

    Note: You can start advanced Java once you are comfortable with Core Java concepts like methods, inheritance and other Object oriented programming (OOP) concepts.

    It will be great if we could know which background you come from and for what reasons you'd like to learn Java. Thanks.


Liked or hated the post? Leave your words of wisdom! Thank you :)