There is a reason I really do not like chaining calls together when writing Java code. Here's why.
This statement:
object.setValue(rs.getString("value"), otherObject.geCode());
Becomes this nightmare when trying to step into the setValue method:
step Step completed: thread="ExecuteThread: '494' for queue: 'default'", weblogic.jdbc.pool.ResultSet.getString(), line=267, bci=0step up
Step completed: thread="ExecuteThread: '494' for queue: 'default'", weblogic.jdbc.pool.ResultSet.getString(), line=269, bci=24step
Step completed: thread="ExecuteThread: '494' for queue: 'default'", class.method(), line=561, bci=366step
Step completed: thread="ExecuteThread: '494' for queue: 'default'", otherClass.getCode(), line=316, bci=0step up
Step completed: thread="ExecuteThread: '494' for queue: 'default'", class.method(), line=561, bci=373step
Step completed: thread="ExecuteThread: '494' for queue: 'default'", class.setValue(), line=782, bci=0
If the code was written this way:
String value = rs.getString("value");
String code = otherObject.getCode();
object.setValue(value, code);
The debugging process would have consisted of:
step Step completed: thread="ExecuteThread: '494' for queue: 'default'", class.setValue(), line=782, bci=0
That saves quite a few steps, typing and aggrevatioin time during debugging. Chaining is sometimes appropriate, often not... while this example may seem innocuous (it wasn't, but I can't publish code from work!) it illustrates just why chaining may become a pain really quickly when debugging.
TrackBack URL for this entry: http://www.unix-girl.com/mt/mt-tb.cgi/475
Thank you for writing that. I used to battle with one of my colleagues about this all the time. During code reviews, he would comment that the extra lines were not needed. My response was that it was much easier to debug code like this and the compiler is clever enough to do the optimisation.
Well named temporaries, like the ones you demonstrate, in many cases alleviate the need for comments and are so much more debugger friendly.
I think it's possible to go way overboard in this thing - as often as not "chaining" is really just a symptom of "lousy coder", but it's easier to address a typographical problem than a personnel one.
I've worked with people who never chained anything, and found it very annoying. But then I have had to single-step my own code maybe twice in the last ten years.
Oh well.
#