We have all written it. That bit of source code that uniquely solved the problem in as few lines as possible. Or maybe it uniquely used a feature of the programming language that would allow for the most efficient use of the CPU or memory. This is great for the college days when you need to impress the teacher. There is no place for this type of code in the work place, most of the time.

So how do you know when to use clever code? A piece of clever code usually has a specific benefit it provides. Clever code should only be used if the design requires it. Not if it would just benefit from it, but absolutely requires it. This can also be said as, don’t pre-optimize.

A great example of a implementation of clever code is Duff’s device. The code is a form of loop unrolling. The original use of it was a serial copy, but it could be applied to other loops. If the serial copy was only used once every few minutes on small amounts of data then this type of optimization wouldn’t provide enough benefits to outweigh the increased complexity in the code.

duffs device


Another example is the Curiously recurring template pattern. The purpose of this pattern is to allow for static polymorphism. This provides decreased run-time and code size at the cost of a small amount of readability and the ability to have dynamic polymorphism.

The entire set of boost C++ libraries can fall under the category of clever code. Before selecting any of these libraries consider who else is responsible for maintaining the code and for how long. If those responsible for maintaining the code do not know boost then they will see any use of it as overcomplicated. In one sense they are right, and they may even be completely right.

In general, use the tool that is ‘just’ necessary for the job. To make an analogy, do not use a power screwdriver when a manual one will do. The screw may not need to be that tight. Also, just because you are trying to use a simpler tools doesn’t mean you should use the wrong one. To continue the analogy, do not use a hammer when a screwdriver is what is needed.

It takes practice and foresight to use the right set of tools. You need to also be prepared to be wrong. Which means making sure that the implementation is easily changed. If you figure out how to do this then please let me know. I think its good to continually remind yourself to continue to learn when to think outside the box and when not to.

thoughts by our engineer Ron – Check out his blog!!

  • Processus42

    Interesting post. I totally agree about when to optimize the code.

    I tried many times to master C++. But this language was just too heavy for me to handle. And since 2008 (The time I started C++), the language only grown in size and complexity.
    6 month ago, I discovered DLang, and with it, I discovered a new “world” of programming. I like to describe it as the child of C++ and Java, but only with the good genes of both of them. I also learnt that clever C++ coding can only be achieved by using only a subset of C++, or a “wrapping” library like Boost.
    (I’m young and unexperienced)

    The only way I can think of about easy implementation’s modification is “specification” and “modularity”.
    While specification needs a huge work before starting to code, modularity needs to write extra-code. With the time, I stopped worrying about re-writing huge portions of code.
    I also want to know the secret of easily editable implementation…

    By the way, Ron’s blog is very interesting.

Leave a Comment