August 28, 2003

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:

  1. 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.
     
  2. 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).

Posted by cedric at August 28, 2003 09:41 AM
Comments

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.

Posted by: Cameron at August 28, 2003 10:36 AM

Respectfully respected? Beautiful :)

Posted by: at August 28, 2003 03:30 PM

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.


Posted by: Olivier at August 29, 2003 12:06 AM

"note that I respectfully respected your insane indentation scheme"

Really insane.

Posted by: iwaku at August 30, 2003 02:58 AM

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 ;)

Posted by: Daniel Farinha at August 30, 2003 02:19 PM

You guys haven't got anything better to do?

Posted by: at September 1, 2003 03:16 AM

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.

Posted by: Richard at September 1, 2003 03:32 PM

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

Posted by: Das at September 4, 2003 11:37 AM

I followed the link to all your ads
Nice to get your information
that's what I want to tell

Posted by: baliku at February 10, 2004 07:54 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

Posted by: lose weight with zone diet at November 4, 2004 01:11 PM

Could it be that conservatives tend to spend more time at home?
Search Google http://www.google.com/

Posted by: Google at November 14, 2004 02:13 PM

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/

Posted by: Google at November 17, 2004 02:24 AM

Merci pour l'information ici

Posted by: yaoi at December 10, 2004 01:27 PM

just lunch new site

Posted by: kayuar at October 17, 2005 11:36 PM

You guys haven't got anything better to do?

Posted by: turnkey business at January 8, 2006 04:31 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.

Posted by: Bo Wen at May 15, 2006 10:49 PM
Post a comment






Remember personal info?