Beautiful code from the C++ perspective

Over at Kotaku, there is an article talking about Doom 3‘s source code and how beautiful it is (there has already been an in-depth article talking about the technical aspects of it).  The article is an interesting read since it focuses more on how to format source code than the actual structure of the code itself (not to say they don’t delve a little into what they’ve done technically).  John Carmack himself even chimes in on the discussion talking about the style of coding that he’s beginning to focus on, which is a more functional style of programming.

Almost all the books I’ve seen people recommend about how to code never go in-depth on the stylistic aspects of the language such as where you should put parentheses, how many spaces you should have between lines, how you tab lines and methods and the other minutiea of making readable code.  I suppose you just take up coding like the example code you see.  For me, I try to maintain white spacing and general formatting the same for every language I code in, but then default to accepted naming conventions and such when it comes down to naming variables, methods, classes, etc.

So, my loyal readers (all two of you), what makes code beautiful?

2 thoughts on “Beautiful code from the C++ perspective

  1. I think beautiful code is consistent and clean. It’s clear and concise without being overly clever. Or perhaps just code I wrote 😉 Joking aside, I generally like to follow the norms [1] of the language I’m using.

    I like airy yet tight code–three blank lines between methods is too much, but you need at least one. I like spaces after conditionals, like if (x… instead of if(x… I like spaces after opening parens and before closing parens, like void foo( bar ). I like around 120 character widths.

    See [2] for a nice talk about less-than-beautiful code.

    [1] http://learncodethehardway.org/blog/AUG_19_2012.html
    [2] http://www.infoq.com/presentations/Ugly-Code

  2. I think following the norms of the language is the first step in getting your code to look beautiful. After that, the default code formatters in IDEs are a pretty good choice on making the code look good (not beautiful, but good).

Leave a Reply

Your email address will not be published. Required fields are marked *