Comments on a few stylistic issues I noticed these past days.
Ted talks about "cuddled
else’s" (what a cute name):
} else {
AKA, the "cuddled else". I never heard of a name for this before. I use
this style extensively when I’m programming in Java, but it and all the
close brackets use up a lot of whitespace.
This convention is recommended by Sun, but I usually prefer to
have the closing brace and the next instruction on separate lines. Nothing
religious or artistic about this preference, the reason is pragmatic:
easier copy and paste.
For example, suppose you need to move the catch block to a different place in
your source. Compare the copy/paste operation between the following two
formattings:
try {
// ...
} catch {
//...
}
and
try {
// ...
}
catch {
//...
}
Cameron posted the
following code snippet, that I would also write slightly
differently:
public Object[] toArray()
{
int cItems = size();
if (cItems == 0)
{
return EMPTY_ARRAY;
}
Object[] aoItem = new Object[cItems];
// ...
return aoItem;
}
I have two remarks about this code:
- I tend to avoid multiple returns in the same method. They make it
harder to follow the logic behind the flow and it’s also inconvenient to
break at the end of the method in order to introspect the returned value.
- I always store my returned values in a variable called "result" (one of
the few things I liked about Eiffel). This way, I can highlight this
variable and pinpoint immediately where it is assigned.
Therefore, I would rewrite Cameron’s code this way:
public Object[] toArray()
{
Object result = EMPTY_ARRAY;
int cItems = size();
if (cItems >= 0)
{
result = new Object[cItems];
// ...
}return result;
}
(Cameron, note that I respectfully respected your insane indentation scheme).
#1 by Cameron on August 28, 2003 - 10:36 am
Yeah, your code is arguably better in this particular example. Except it should have been ‘aoResult’ (result is an array of objects).
Smartypants.
However, having more than one return doesn’t bother me in Java quite as much as it did in C++. It’s not something that I avoid religiously.
#2 by Anonymous on August 28, 2003 - 3:30 pm
Respectfully respected? Beautiful 🙂
#3 by Olivier on August 29, 2003 - 12:06 am
The “Only one result” rule is good, but it oftens goes agains the “Deeply nested if statements are hard to read” rule. Then, you start having to comment your closing brackets, …
Besides, I find it elegant to treat these “weaker” post-conditions at the beginning of the method and once for all.
#4 by iwaku on August 30, 2003 - 2:58 am
“note that I respectfully respected your insane indentation scheme”
Really insane.
#5 by Daniel Farinha on August 30, 2003 - 2:19 pm
Well I’m glad I’m not the only one using the ‘insane’ indentation 🙂
Hang in there Cameron, we’re a dying breed but still alive and kicking 😉
#6 by Anonymous on September 1, 2003 - 3:16 am
You guys haven’t got anything better to do?
#7 by Richard on September 1, 2003 - 3:32 pm
In the end – who cares? It’s really a matter of personal taste and so you set up your IDE to format it the way you like it and if someone else has to work with it, they can just format it the way they like it.
#8 by Das on September 4, 2003 - 11:37 am
Well, I have no problems “cutting and pasting” the cuddled-catches. You just have to paste *over* the closing brace of try. The closing brace will be replaced by the catch’s first brace.
–Das
#9 by baliku on February 10, 2004 - 7:54 pm
I followed the link to all your ads
Nice to get your information
that’s what I want to tell
#10 by lose weight with zone diet on November 4, 2004 - 1:11 pm
Hi – I was looking for some political sites with articles on the recent US election and found your nice site. The comments from others on here are pretty good so I just thought I’d add my thoughts also!
Elaine Cooper
#11 by Google on November 14, 2004 - 2:13 pm
Could it be that conservatives tend to spend more time at home?
Search Google http://www.google.com/
#12 by Google on November 17, 2004 - 2:24 am
Now that these (fairly damning) facts have been documented by records obtained under the FOIA (AFTER the 2000 election by the way), we should focus on the even more damning issues:
Search Google http://www.google.com/
#13 by yaoi on December 10, 2004 - 1:27 pm
Merci pour l’information ici
#14 by kayuar on October 17, 2005 - 11:36 pm
just lunch new site
#15 by turnkey business on January 8, 2006 - 4:31 pm
You guys haven’t got anything better to do?
#16 by Bo Wen on May 15, 2006 - 10:49 pm
On the contrast, I think multiple returns is easier to understand the flow. If you have many if-else, then single return assignment may soon be buried. But if a return can be made immediately after a condition is met, it is a clear flag that its flow ends there and it is easier to track.
Anyway, this is really kind of personal preference.