Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

petdance (2468)

petdance
  andy@petdance.com
http://www.perlbuzz.com/
AOL IM: petdance (Add Buddy, Send Message)
Yahoo! ID: petdance (Add User, Send Message)
Jabber: petdance@gmail.com

I'm Andy Lester, and I like to test stuff. I also write for the Perl Journal, and do tech edits on books. Sometimes I write code, too.

Journal of petdance (2468)

Friday April 01, 2005
11:38 AM

No cuddling

[ #23974 ]
I'm finally convinced that cuddled elses are bad. Here's a script I wrote to uncuddle them in all our Perl and PHP:

#!/usr/bin/perl -w -i -p

if ( s/^ (\s*) } \s* (els(e|if|eif))\b \s* /$1}\n$1$2 /mxs ) {
    s/\s+$//; # Strip trailing whitespace while we're at it
    $_ = "$_\n";
}

so now you can just say "./uncuddle `find . -name '*.pm'`"

To be clear what cuddled elses are, it's this:

if ( ) {
    ...
} else {
    ...
}

instead of

if ( ) {
    ...
}
else {
    ...
}

The key is that having the else outdented shows that it's a point of the flow of control. It all the more important if you get into elsifs.

if ( A ) {
    ...
} elsif ( B ) {
    ...
} elsif ( C ) {
    ...
} else ( D ) {
    ...
}

instead of the more clear

if ( A ) {
    ...
}
elsif ( B ) {
    ...
}
elsif ( C ) {
    ...
}
else ( D ) {
    ...
}

Yes, it adds one more line to the screen. I'm OK with that.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • If you don't mind using a sledgehammer to pound this nail, you can always use perltidy, which lets you do it either way (your way by default, I IRC).
  • I don't cuddle them myself, but it always struck me as one of those silly stylistic things that you have to be consistent on, but which choice you make doesn't really matter.

    But I'm interested in cases where someone gave up a stylistic choice like that, because it may be that there really is a reason to prefer one over the other.

    Thanks,
    Ben
  • The key is that having the else outdented shows that it's a point of the flow of control. It all the more important if you get into elsifs.

    But the code following the condition is indented so I don't see what uncuddling would gain you when it comes to visual clarity. Furthermore, by cuddling them it becomes obvious that the various if/elseif/.../else cases are really part of one bigger statement where only one of the indented code-blocks is ever executed.

    Finally, the lines you save by cuddling could make
  • OT, but I'm curious - does anyone know where/when the use of "cuddle" as used here (i.e. with respect to brace positioning) originated?
    I couldn't find any reference to this use on Wikipedia, M-W or dictionary.com .
    • Probably (in this case, anyway) from the recommendation in Damian Conway's recent book, Perl Best Practices, in which Damian advises against 'cuddled' else's