Uncategorized

My unofficial coding style

This last couple of weeks has been insane for Haraka. Not only do we have some very large email consumers testing it out, but there’s been a veritable flood of patches from contributors, for which I’m immensely grateful. It means I’ve actually barely touched the code in a while, something I’m sure my current employer will be happy about :-)

One thing that came up is that I need to enforce some kind of coding style, mostly to make life easier for both contributors and myself, in terms of accepting patches.

There’s a number of what I might call “unusual” coding styles used in Javascript. One typical example is the style used by “npm” – the node package manager. The “funny” part of Isaacs’ style is that it omits semi-colons almost everywhere, and puts commas in front of list entries. I’m sure there are other odd styles (such as the use of CoffeeScript) too. Javascript is quite a flexible language in what it will let you omit.

First though the justification for my style: In my day job I’m mostly a Perl and C programmer. JavaScript has similarities to both languages, so I try and keep my style in a way that when I switch from one language to the next I don’t fuck up too easily. That’s getting especially tough going back to Perl these days – I’m starting to hate typing $ and @ everywhere :)

So here’s my basic policies:

  • 4 spaces indents. No tabs please.
  • When blocks are optional (for, while, if, etc) provide the block anyway. This keeps me from screwing up when I switch back to Perl. It’s also a lot easier to not screw up this way.
  • This format for if blocks:
    if (condition) {
        ...
    }
    else {
        ...
    }

    i.e. never cuddle up the “else” to the bracket, keep the opening bracket on the same line. I allow an exception to the latter if the condition spans multiple lines:
    if (condition1 &&
        condition2)
    {
        ...
    }

  • Use semi-colons always, even where they are optional
  • In lists where you use multiple lines, put commas at the end, even at the last element. This makes it invalid JSON, but it’s still valid JS, and makes adding and removing elements easier.
    [
        a,
        list,
        of,
        things,
    ];
  • CamelCaseForClaseNames
  • under_scores_for_variables_and_functions
  • ALL_CAPS_FOR_CONSTANTS
  • Don’t let lines get too long, but don’t get anal about 80 columns. This isn’t 1994 any more. I’m not editing in vi on SunOS-4 any more.
  • If you submit a new plugin for inclusion, create docs in docs/plugins/<plugin_name>.md too.

Of course I reserve the right to change this list at a whim.

Standard

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s