Solid C++ Code by Example

Sometimes I see code that is perfectly OK according to the definition of the language but which is flawed because it breaks too many established idioms and conventions of the language. I just gave a 90 minute workshop about Solid C++ Code at the ACCU 2010 conference in Oxford.

When discussing solid code it is important to work on “real” problems, not just toy examples and coding katas because they lack the required complexity to make discussions interesting. So, as a preparation I had developed, from scratch, an NTLM Authentication Library (pal) that can be used by a client to do NTLM authentication when retrieving a protected webpage on an IIS server. Then I picked out a few files, the encoding and decoding of NTLM messages, and tried to write it as solid as possible after useful discussions with ACCU friends and some top coders within my company. Then I “doped” the code, I injected impurities and bad stuff into the code, to produce these handouts. At the ACCU talk/workshop the audience read through the “doped” code and came up with things that could be improved while I did online coding (in Emacs of course) fixing the issues as they popped up. With loads of solid C++ coders in the room, I think we found most of the issues worth caring about, and we ended up with something that can be considered to be solid C++, something that appears to have been developed by somebody who cares about high quality code. Here are the slides that I used to summarize our findings. Feel free to use these slides for whatever you want. Perhaps you would like to run a similar talk in your development team? Contact me if you want the complete source code for the authentication library, or if you want to discuss ideas for running a similar talk yourself. I plan to publish the code on githup soon – so stay tuned.

UPDATE June 2010: The PAL library is now published on github. A much improved slide set is also available on slideshare.

4 Responses to Solid C++ Code by Example

  1. Yechiel Kimchi says:

    “Regarding syntax, it is my opinion that knowing the syntax of a programming language is just about knowing 5% of what is to be learnt about it.”
    Taken from the page above, written about 10 years ago.
    Over time, my perspective has changed and I’d considerably reduce the percentage.
    The page itself makes IMHO an interesting reading, although in many cases it points at syntax/semantics errors that bad books teach (but there are also plenty of idiomatic errors).
    Enjoy
    Yechiel

  2. alfps says:

    OK, I’ll post about the signed/unsigned issue… :-)

  3. … and here’s the posting: http://alfps.wordpress.com/2010/05/10/how-to-avoid-disastrous-integer-wrap-around/

    Cheers,

    - Alf
    PS: Good presentation today at the Oslo C++ User Group meeting.

  4. ruwani says:

    Your slides were a real eye-opener for me. Thanks