A nice post by Tom McQueeney on the new varargs feature in JDK5 marred by one point:
if (params.length > 0) lastName = params[0]; if (params.length > 1) firstName = params[1]; if (params.length > 2) middleName = params[2]; if (params.length > 3) nickName = params[3]; if (params.length > 4) { throw new IllegalArgumentException("Constructor called with too many arguments"); }
Why the series of if
statements? A simple switch
statement with fall-through is more clear:
switch (params.length) { case 4: nickName = params[3]; case 3: middleName = params[2]; case 2: firstName = params[1]; case 1: lastName = params[0]; break; default: throw new IllegalArgumentException("Constructor called with too many arguments"); }
No comments:
Post a Comment