(or learn from my screw-ups)
Refactoring code is somewhat of a hobby, I suppose, since I do it as much as I possibly can (or have time for).. so seeing something like this..
String result = (someString.trim() + anotherString.trim()).toLowerCase();
Boils my blood and of course I have to rewrite it..
String result = "";
if(someString != null)
result += someString.trim();
if(anotherString != null)
result += anotherString.trim().toLowerCase();
.. and of course I completly missed the part where the entire string was set to lower case.. not just the second portion.
Stupid me (this caused real problems too).. but I say at least 20% of the blame is on the hard-to-read chained code.. (20% more on me being in a hurry and rest on me for not paying attention, but who's counting..)
TrackBack URL for this entry: http://www.unix-girl.com/mt/mt-tb.cgi/687
(watch blood pressure)
Then youll have a field day here:
I made this today:
<%=java.text.DateFormat.getDateInstance(java.text.DateFormat.MEDIUM, com.ibm.wps.engine.RunData.from(request).getLocale()).format(((java.util.GregorianCalendar) request.getSession().getAttribute(com.factoring.mfoportal.util.Constants.CLIENT_CUSTOMER_SEARCH_START_DATE)).getTime())%>
It wasnt supposed to be that long, but it works, so why take it apart now. ;)
#I don't envy the poor coder who has to maintain this...
#xOr: Bad coder! No keyboard!
Regarding your "String result" example, and depending what the result is meant to be, it could be better to have a single if(someString != null && anotherString != null) and do the whole string appending inside the if-block (using StringBuffers if you want).
This is assuming a null string is unexpected, in which case you could even add an else block throwing an exception when you get a null string.
Anyway, as far as chained calls is concerned, I many times find myself writing huge lines, because I'm probably in the zone. Then after compiling and testing, I go back and break the big lines up into readable code, normally using the print margin line as a guide.
#