When to create a new method

I think this is one of the question that they don't teach in training camps - when to you create a new method.

For code reuse, you should put a piece of code in a new method if some Class/method other than the method you are in is also performing that routine or if you anticipate that this piece of code will be use by other classes in the future.

Utility Class and Utility Method

Most of the application that I'm working right now has Utility Classes wherein you put your utility methods. A utility method is a method that doesn't need Class instance variable to operate and they are usually declared as public and static.

public static String
stripHeader(String pRawXML)
{
//implementation
}


Code Readability

For code readability and easy maintenance I choose to create a new method if that block of code performs a certain task even if it is not reused by my other classes.


About this entry


0 comments: