Stylistic issues
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).
Comments (16)
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.
Respectfully respected? Beautiful :)
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.
"note that I respectfully respected your insane indentation scheme" Really insane.
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 ;)
You guys haven't got anything better to do?
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.
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
I followed the link to all your ads Nice to get your information that's what I want to tell
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
Could it be that conservatives tend to spend more time at home? Search Google http://www.google.com/
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/
Merci pour l'information ici
just lunch new site
You guys haven't got anything better to do?
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.