Chunk & Write

Coders develop a style much like writers do. I’ll compare coding to writing prose again in the future. I don’t draw much distinction between the two.

Code has agreed upon conventions. I’ve read the phrase “don’t cuddle an else”, probably in a Perl style manual — Perl coders are style mavens.

But what does it mean? It means that:


if(it's_true) {
    say "yay, true!";
} else {
    say "yay, false!";
}

is stylistically worse than:


if(it's_true) {
    say "yay, true!";
}
else {
    say "yay, false!";
}

The difference is subtle but the rule makes sense. And it’s a structural sense that it makes. Code is easier to change when your else statements ride on their own line. That’s just a simple, logistical fact, and the assumption should consistently be that code is going to change.

Prose doesn’t have as strong an adherence to logistical efficiency. Sure, there are roundabout ways to say something, but that’s poetry and doesn’t lose style points unless it sucks. There’s bad code, too.

3 Responses to “Chunk & Write”

  1. Perl Coding School » Blog Archive » perl code [2008-03-26 06:34:37] Says:

    […] Chunk & Write By mark I don’t draw much distinction between the two. Code has agreed upon conventions. I’ve read the phrase “don’t cuddle an else”, probably in a Perl style manual — Perl coders are style mavens. But what does it mean? It means that: … # half the shebang - http://www.halftheshebang.com […]

  2. gr Says:

    While I understand the logic, I hate that rule (and rampantly cuddle elses).

    I find it easier to read without the extra CRLF, because the else is syntactic clutter: everybody knows what’s going to be there, so unless it’s an elsif with a shiny new case, it should take as little screen real estate as possible.

    Oh, and if you actually think this makes things easier to edit, you obviously aren’t using % correctly in vi(1).

  3. mark Says:

    And you aren’t using o and shift-o.

Leave a Reply