jQuery in Action, 3rd edition
The moose likes Beginning Java and the fly likes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark ""chained" assignments" Watch ""chained" assignments" New topic

"chained" assignments

Timothy Stone
Ranch Hand

Joined: Aug 01, 2001
Posts: 71
Settle a debate. In a code review, I asked that chained assignments be avoided for readability.


What makes line 7 more effective than 8 and 9? Brevity only? I think the long form is more readable and understandable. Does this boil down to preference? Eventually the style guide will need amending, but thought I would post to the board for thoughts.

[ May 25, 2004: Message edited by: Timothy Stone ]

Timothy Stone, MIT, SCJP
"This Satan's drink [coffee] is so delicious, we shall cheat Satan and baptize it." --Pope Clement the VIII (1592-1605)
Jeffrey Hunter
Ranch Hand

Joined: Apr 16, 2004
Posts: 305
Personally, I go for the long form, as it is easier for me to read. I believe any performance issues are moot, since the assembly code most likely requires the same number of instructions to execute the assignments regardless of how you write them.
Paul Bourdeaux
Ranch Hand

Joined: May 24, 2004
Posts: 783
As long as the statements are being set to null, or if the variable is a primitive type, I think the only advantage to chaining is brevity. Putting the statements on seperate lines is less confusing, and accomplishes the exact same task.

Of course, if you are creating new objects, chaining them has a very different meaning than assigning on seperate lines. I.E.:

does NOT do the same thing as

(Please correct me if I am mistaken in this!)

“Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.” - Rich Cook
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
Paul you are not mistaken.

Concerning Timothy's question, it's merely a matter of style preference. I think it's a good idea to pick the style that you find more readable.

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
I agree. Here's the link: http://aspose.com/file-tools
subject: "chained" assignments
It's not a secret anymore!