Every Programmer should be familiar of passing a parameter to a method since this is the most basic thing to do in Java. The important main method (public static void main(String… args) to which a class can run itself is a example of a static method that accept a string array object as argument. Java makes it too too easy to add a parameter to a method and even more so with introduction of variable arguments language enhancement which allow one to pass variable list of parameters without first defining them in a argument array.
Prior to Java 1.5
public void execute(String[] args)
eg execute(new String[]{“p1″,”p2″,”p3″,”p4”});
Variable Arguments in Java 1.5 onwards
public void execute(String… args)
eg execute(“p1″,”p2″,”p3″,”p4”);
Essentially, Variable Arguments improves the convenience of passing multiple parameters without defining a ‘dummy’ array essentially.
Still passing multiple parameters in a method signature despite the common way may not be the most elegant way. A method can be made more readable and ‘beautiful’ by consolidating related parameters as flags into a object parameter or as primitive parameter. Not only it will make method manageable, it will also often slight performance improvement (since explicit parameter need metadata eg Reflection).
In this post, I’m going to explore the two popular ways of accomplishing this.
Taking the following as a example.
A method signature which is defined as
public static Lettering makeLettering(String _name, int _size, boolean _isPlain, boolean _isBold, boolean _isItalic)
Yes, the above method may not make sense since if isPlain is true, it will supersede isBold and isItalic and vice versa. However this is just a example to demonstrate parameter passing.
1) As a Object Parameter
a) The related characteristics are consolidated into a class.
public class Characteristics
{
public boolean isPlain, isBold, isItalic;
}
b) and pass as argument to the modified method
public static Lettering makeLettering(String _name, int _size, Characteristics _c)
{ // The body will contain the code to test the condition.
if (_c.isPlain)
…
if (_c.isBold)
…
if (_c.isItalicl)
…
return …
}
2) As a Primitive Parameter
a) Variable is defined as numeric value with power of 2. Why to the power of 2 ? It is because computer only understand binary not true numeric. As the binary will be AND with another binary to produce a result correspondent to the AND’s truth table.
Do not use 0 value since AND anything with 0 will yield zero.
public static final int PLAIN = 2, BOLD = 4, ITALIC = 8;
b) and pass as argument to the modified method
eg Lettering makeLettering(“name”, 23, BOLD + ITALIC);
public static Lettering makeLettering(String _name, int _size, int _characteristics)
{ // The body will contain the code to test the condition.
// By ANDing, it can check if characteristics has the flag set.
if ((characteristics & PLAIN) != 0 ) // or if (characteristics == PLAIN ) // If PLAIN is zero.
…
if ((characteristics & BOLD ) != 0)
…
if ((characteristics & ITALIC ) != 0)
…
return …
}
Leave a Reply