Elliott C. Back: Internet & Technology

Perl Style: The If / Else Statement

Posted in Code, Cute by Elliott Back on September 6th, 2006.

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.

12 Responses to “Perl Style: The If / Else Statement”

  1. doc says:

    an uncuddled else looks like a “new statement”, a cuddled else implies that the else3 is part of the prior statement… which it is. perl style guides are just wrong about this… and it actually really bugs me.

    # easy to see just 2 statements
    if (...) {
    } elseif (...) {
    }
    if (...) {
    }

    # harder to see only 2 statements

    if (...) {
    }
    elsif (...) {
    }
    if (...) {
    }

  2. LordOfTheDance says:

    I think there are more important things to worry about, you anal twats.

  3. Robin says:

    I much prefer cuddled.

    I don’t think that readability is hurt one bit, it’s easy to read:

    } else {

    And it makes more sense to me. An “else” can’t exist without an “if” – and you can’t put anything in between the two.

    If you don’t cuddle the else, it looks like a statement in its own right, meaning you could do something like:

    if(something) {
    }
    die “here”;
    else {
    }

    As you all know, this is syntactically incorrect. Hence why “} else {” makes more sense.

    • SR says:

      @Robin:

      I cuddle my elses as well, and I don’t think it hurts readability at all (actually, I think it helps to have all elses and elsifs preceeded by a close brace) but there *is* something you can put between an if and an else: comments.

      ### Do such and such in some case
      if ($foo eq $bar) {

      }
      ### Do something else in another case
      elsif ($bar eq $baz) {

      }
      ### Catch other cases
      else {

      }

  4. tim says:

    I’m a little late to this conversation, but hey :)

    farker, It’s more important than you think. Once you’re required to maintain and support code you’ll understand.

    yes, un-cuddled is easier to read, but most of all it’s easier to work with when patching code.

    -Tim

  5. Dave says:

    Un-cuddled is easier to spot when quick-scanning unfamiliar code, for instance to distinguish from an elsif. And it's the same amount of text space – a bit more screen space, but sometimes that's a good thing.

  6. farker says:

    I think there are more important things to worry about, you anal twats.

  7. binner says:

    I think there are more important things to worry about, you anal twats.

  8. fasdf says:

    fasdffsd

  9. Will says:

    I cuddle as well, but it really does look more readable without cuddling.

  10. Brandon says:

    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.

Leave a Reply

Powered by WP Hashcash