January 07, 2003
Why chaining calls is sometimes bad

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=0

step up
Step completed: thread="ExecuteThread: '494' for queue: 'default'", weblogic.jdbc.pool.ResultSet.getString(), line=269, bci=24

step
Step completed: thread="ExecuteThread: '494' for queue: 'default'", class.method(), line=561, bci=366

step
Step completed: thread="ExecuteThread: '494' for queue: 'default'", otherClass.getCode(), line=316, bci=0

step up
Step completed: thread="ExecuteThread: '494' for queue: 'default'", class.method(), line=561, bci=373

step
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.

Posted January 07, 2003 05:53 PM in Java
TrackBack URL for this entry: http://www.unix-girl.com/mt/mt-tb.cgi/475
Comments
On January 7, 2003 11:55 PM Stuart Woodward added:

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.

#
On January 8, 2003 04:44 PM Steve added:

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.

#
Trackbacks