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.
To cuddle or not to cuddle
runrig on 2005-04-01T22:12:35
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).
Re:To cuddle or not to cuddle
petdance on 2005-04-01T23:00:35
I wanted a surgical approach w/o resorting to perltidy. My solution also fixes the PHP code we have that cuddles.
What convinced you?
btilly on 2005-04-01T23:34:34
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
Re:What convinced you?
petdance on 2005-04-01T23:48:24
Damian's explanation in his upcoming Perl Best Practices.
Re:What convinced you?
pudge on 2005-04-05T22:09:42
If you can't succinctly explain why they are bad, I doubt that a lengthy explanation in a book will convince me.
Re:What convinced you?
petdance on 2005-04-06T00:24:06
I did succinctly explain in the original post.
Re:What convinced you?
pudge on 2005-04-06T00:50:38
I beg to differ. You asserted that one is more clear, and gave only one reason for anyone to think so: that uncuddled "shows that it's a point of the flow of control." But so does cuddled.
Cuddling does gain you a few things
ethan on 2005-04-03T06:13:01
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 a function just fit on the screen which is a good thing as it respects the principle of locality (i.e. everything about a function can be seen without the need for scrolling).
Re:Cuddling does gain you a few things
merlyn on 2005-04-05T22:59:09
Seconded. I find "} else {" or "} elsif (COND) {" to be a natural "step" in my program. Putting that on two separate lines won't gain me any more understanding.
Re:Cuddling does gain you a few things
zatoichi on 2005-04-20T12:46:48
No but it is easier on the eyes...
Etymology?
offerk on 2005-04-20T08:09:17
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 .
Re:Etymology?
orbit on 2005-08-31T02:44:20
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