Wednesday, March 6, 2013

coding style

I only took 1.5 coding classes in college, and I don't remember them going into great length about code readability, other than it being a good idea. I don't even remember code commenting being very important (of course, our programs were really short and simple).

I find that as I code in Hugo, I am adopting Kent's coding style more and more. I mean, for a long time, I've understood the importance of indentation, but even getting that right has been a bit tricky. My old code was probably over-indented, but now I've picked up one more rule from Kent's style:

Brackets are not indented

If you think about it, the following object definition:
 already establishes that brackets aren't indented, so not indenting with them is nice from a consistency viewpoint.

Now, whatever is in the bracket is indented, of course. Also,

Brackets get their own line

The other thing I do for readability these days is always give brackets their own line. This makes it easier to keep things aligned.

Now, of course, both of these things are up to personal taste, but I thought I'd share my reasonings for other Hugo authors who haven't put much thought into it.

1 comment:

  1. As a non-programmer, I pretty much emulate the example code, except in Hugo I think I indent and insert line breaks more often -- and even use brackets where I don't have to, such as around the contents of long_desc -- because I want to be careful to be very clear. I've found out that I can confuse myself easily. ;)

    When I used Inform 6, I put the opening brackets on the same line as the if statement or routine definition, rather than on their own line. For some reason, most of the I6 code I've seen was like that.