Sunday, 12 August 2012

Coding styles.

As I was looking at my mate's C++ code I spotted the following curious lines:
  TreeDataCIterator it  ( m_treeDataList.begin() ), 
                    end ( m_treeDataList.end() );
Don't you think it's rather unusual for C++? Myself would rather writhe something more mundane like:
  TreeDataCIterator it = m_treeDataList.begin(); 
  TreeDataCIterator end = m_treeDataList.end();
complying with some old coding guidelines of the dark ages (aka 90-ties) which were imposed on the unhappy programmers in some big, iternational, waterfall project. But, you know, habit is the second nature... So I was somehow perplexed by the originality of the code, as I'd rather expected more dull style (like mine) considered C++'s strong C-legacy. But then I rememebred that my mate was originally a LISP programmer (but then his LISP-based company didn't really take off) and everything became clear! Look at this snippet uf LISP code from the SICP book:
  (define (count-leaves x)
      (cond ((null? x) 0)
            ((not (pair? x)) 1)
            (else (+ (count-leaves (car x))
                     (count-leaves (cdr x))))))
isn't it similiar? The love of parenthesis cannot be cured of easily ;-).

On the other hand, this could be as well an exmple if the C++ initalization syntax, and in C++11 we could write as well:
  TreeDataCIterator it  { m_treeDataList.begin() }, 
                    end { m_treeDataList.end() };
Not that LISPy at all (instead maybe a little confusing)! But seriously, don't you think arguing about this is a loss of time? Well, not really, good code should be readable, and if we care about our code, it's does matter. But then I find myself increasingly obsessed with code formatting and visual appeal of my code. It's certainly not wrong, but it robs my brain of its preciuos ressources. Remember? Your brain can hold in memory only 6-7 items at the same time, so decreasing mental load is a blessing for it. Not mentioning that formatting and reformatting your code takes time.

And the worst is, that your favourite formatting style at the moment won't be that in the next year. Myself, I was in projects where the coding styles looken at first insane to me! And I arguend and ranted but complied to them for the greater (i.e. project's) good. An guess what, now I'm using elements of these coding styles in my own code! Ok, not everything, but some parts weren't so nutty at all. What does that mean? Yes, you guessed it: coding styles doen't matter at all. But switching between them does!

So why we just don't stop doing this? Why don't let wo the computer do this and concertrate on more important aspects oif code? There are so many of them! As I first read about Go's policy about code formatting I wasn't too enthusiastic about it. But meanwhile I find it a good idea: only one prescribed layout style automatically imposed on your source code! Let's forget about coding standards and concentrate on problem solving!

2 comments:

  1. I'm a Lisp programmer, not a C++ programmer, but isn't your mate's version potentially more efficient?

    My (decade-old) knowledge of C++ is that "Classname varname(arg);" calls an initializer (?), while "Classname varname = arg;" calls an initializer and then calls operator=.

    ReplyDelete
  2. @Anonymous
    Yes, in principle you are right. But personally I'd rely on the compiler to optimize such cases. Yes, I know, you shouldn't set your bets on the compiler, but I opt more for the readability than for micro-optimizations. Maybe I'm not in the "spirit of C(++)", or maybe it's just old habits, but writing int i = 1 pleases me more than int i(1) - it just looks more C-ish. On the other hand the post says that this all doesn't matter ;)

    ReplyDelete