Methods

Rule 3:Design Method signature carefully

1.Names of methods as per convention for reference take already named methods in Class
2.Parameter Sequence should not be more than 4,
Long Parameters sequence to method with same type make readlibility erroraneous.
Follwing are the way arounds:
a. break method with long parameter sequence into many methods with lesser parameters.
Example:
List interface doesnot have a methos that gives you first or last index of a sublist Instead it has one method that gives you subList and then there is another that gives you the index i.e firstIndexOf() or lastIndexOf()
b.replace the long sequence of parameters with a HELPER CLASS TO HOLD A GROUP OF PARAMETERS when the sequence represnts an entity like ranks in card suit.
c. use builder pattern
3.use interfacce instead of class as parameter type ,It allows the client/caller to pass various implmention of that type
Example: Passing Map as parameter type it lets you pass HashMap ,TreeMap ,ConcurrentHashMap etc. or any Map implementation yet to be written.
By using a class instead of an interface, you restrict your client to a particular implementation and force an unnecessary and potentially expensive copy operation if the input data happens to exist in some other form.
4.Prefer two-element enum types to boolean parameters – enumsm make code easier to read and write.
example:
public enum TemperatureScale { FAHRENHEIT, CELSIUS }
Not only does Thermometer.newInstance(TemperatureScale.CELSIUS) make a lot more sense than Thermometer.newInstance(true),
but you can add KELVIN to TemperatureScale in a future release

Rule 4:Use method overloading Judiciously

the choice of which overloading to invoke is made at compile time. A safe, conservative policy is never to export two overloadings with the same number of parameters. you can always give methods different names instead of overloading them.

For example,
consider the ObjectOutputStream class. It has a variant of its write method for every primitive type and for several reference types. Rather than overloading the write method, these variants all have different names, such as
writeBoolean(boolean), writeInt(int), and writeLong(long).

selection among overloaded methods is static, while selection among overridden methods is dynamic. The correct version of an overridden method is chosen at runtime,

Published by

Unknown's avatar

sevanand yadav

software engineer working as web developer having specialization in spring MVC with mysql,hibernate

Leave a comment