Perl Style: The If / Else Statement
While reading Perl Best Practices I can across a section which began:
Don’t cuddle an else
It turns out that a cuddled else looks like
} else {
while the uncuddled else places the else on a new line
}
else {
However, I can’t agree. The way I see it, not merging the else and the closing brace of the if statement simply wastes space. And, it doesn’t hurt alignment because you can still follow the else block–it’s just two positions off what it would have been otherwise. What do you think?
This entry was posted on Wednesday, September 6th, 2006 at 10:34 pm and is tagged with alignment, best practices. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback.


on September 6th, 2006 at 11:31 pm
I have to agree with the book’s author. In my opinion, “cuddling” an else hurts readability, although I’ll admit I’m rather anal when it comes to coding style. I find it far easier to be able to look straight down the column and find my if, elseif, else statements all lined up. Sure, it takes up a little extra space, but the difference is negligible.
A person’s coding style should cater toward ease of readability for other people, not the compiler. The compiler doesn’t care if your else’s are cuddled or not; in fact in many lanuages you could write your entire script on a single line and the compiler won’t mind one bit. But I can guarantee that the programmer who comes in behind you and has to maintain your code will have a thing or two to say about it.
on September 6th, 2006 at 11:48 pm
I cuddle as well, but it really does look more readable without cuddling.
on January 22nd, 2007 at 2:36 pm
fasdffsd
on June 12th, 2007 at 1:47 am
I think there are more important things to worry about, you anal twats.
on October 31st, 2007 at 7:40 am
I think there are more important things to worry about, you anal twats.