Wednesday, July 29, 2015

Best programming practice

Today, I was going through the code of someone and found it difficult for myself to read it. And the reason behind that was too many If's and too many switch cases. Each method was written with more then 30 line of code. I wasn't sure that why he has choose Object Oriented Programming when he had to do that.

Every developer who is willing to improve his programming skills he must need to keep one rule in his mind before writing code:

Someone will read your code someday

I think few just leave their code without method level or class level comments; so that no one get to know that who wrote that code. But, those stupids forget that subversion do this too.

Anyway few of the things I think should be simple in code:


  1. Class level comments
  2. Method level comments
  3. Method should be well-commented
  4. Atomic
  5. NO to Conditional Programming
I am not going to debate on comments because you can get this information from a lot of blogs. So, I will start this blog from the 4th Point:

Atomic:

Forget it that you will get 100+ methods in class or not. But, try to keep every single method for one task not for more then one task.

Example
public void calculateAndPrint(int i, int j){
    System.out.print(i+j);
    System.out.print(i/j);
    System.out.print(i*j);
    System.out.print(i-j);
}
Correct Way:
public void printSum(int i, int j){
    System.out.println(i+j);
}
public void printMultiply(int i, int j){
    System.out.println(i*j);
}
public void printDivide(int i, int j){
    System.out.println(i/j);
}
public void printSubtract(int i, int j){
    System.out.println(i-j);
}
Don't jump into OOP for the above example as it's just an example and I know it could be implemented well with OOP.

NO to Conditionals Programming

The worst scenario comes when someone try to put his brain in coding. And a big web of If-else or switch-case comes into the code:


An example where someone is trying to handle the SOAP response:
if(eventType == XmlPullParser.START_TAG) {
            soapResponse= xpp.getName().toString();

            if (soapResponse.equals("EditorialOffice")){  
                eventType = xpp.next();
                if (xpp.getText()!=null){
                editorialOffice += xpp.getText();
                }
            }   
            else if (soapResponse.equals("EditorialBoard")){  
                eventType = xpp.next();
                if (xpp.getText()!=null){
                editorialBoard += xpp.getText();
                }
            }
            else if (soapResponse.equals("AdvisoryBoard")){  
                eventType = xpp.next();
                if (xpp.getText()!=null){
                advisoryBoard += xpp.getText();
                }
            }   
        }
        eventType = xpp.next();
     }
Instead of doing above tricky code which is difficult to read could be done in a better way by simply implementing Strategy Pattern:

// Interface
public interface IResponseHandler {
   public void handleResponse(XmlPullParser xxp);

}

// Concrete class for EditorialOffice response
private class EditorialOfficeHandler implements IResponseHandler {
   public void handleResponse(XmlPullParser xxp) {
       // Do something to handle Editorial Office response
   }
}

// Concrete class for EditorialBoard response
private class EditorialBoardHandler implements IResponseHandler {
   public void handleResponse(XmlPullParser xxp) {
       // Do something to handle Editorial Board response
   }
}
// Concrete class for AdvisoryBoard response
private class AdivsoryBoardHandler implements IResponseHandler {
   public void handleResponse(XmlPullParser xxp) {
       // Do something to handle Advisory Board response
   }
}
At a place in your code create something like this

Map<String, IResponseHandler> strategyHandlers = new HashMap<String,IResponseHandler>();
strategyHandlers.put("EditorialOffice", new EditorialOfficeHandler());
strategyHandlers.put("EditorialBoard", new EditorialBoardHandler());
strategyHandlers.put("AdivsoryBoard", new AdvisoryBoardHandler());

And when you get the response just call the handler to do the rest of the things for you:

ResponseHandler responseHandler = strategyHandlers.get(soapResponse);
responseHandler.HandleResponse(xxp);

I hope it will be helpful for the java programmers of 1-3 years of experience.  

No comments:

Post a Comment