The Elements of a Good Template in C++

By John Paul Mueller, Jeff Cogswell

The template you create has to look professional and work as expected. The first decision you have to make is what kind of template to create. You can choose among these three types:

  • Function: A function represents the simplest way to create a template and eases debugging requirements. You can group functions in libraries to make them easier to access. However, functions often lack depth and you can’t coordinate activities between them as easily as you can between the elements of an object.

  • Structure: A structure provides the best speed in many cases and can reduce the amount of system resources required, depending on how you define the structure. Remember that C++ allocates memory for the entire structure, but structures also present opportunities for optimization that you don’t get with a class.

  • Class: A class provides the greatest flexibility because you can express the template using all the elements that a class can provide — methods, properties, and events. You can inherit classes to create new classes. In short, if you have a complex idea to implement, classes are the way to do it.

The second decision you have to make is how to weight design factors when creating the template. C++ offers myriad ways to accomplish any given task. For example, you have multiple ways to create a function. However, one method is normally superior to the others simply because it offers some particular benefit. Consider these requirements when choosing the kind of template to create:

  • Security: “Simplicity” often translates into “easier to secure.” In general, functions are easier to secure than structures, which are easier to secure than classes. However, you can easily write an insecure class if you use the wrong approach. Secure templates often require additional checks that can affect reliability (the template tends not to allow specific actions when these actions affect security) and speed (additional code always slows template execution).

  • Reliability: The options you choose will affect the reliability of the template you create. A reliable template produces consistent results for any data type supplied to it. In some cases, ensuring reliability means adding checks to the template, which increases complexity. The additional code affects both the security and the speed of the template.

  • Speed: Templates save the developer time. However, if the resulting template produces slow code, you can be sure that users will complain and the developer will end up rewriting some code to improve application speed. A fast template is usually small and performs the task precisely. The additional checks required to ensure secure and reliable operation always affect speed negatively, so you must work to achieve a balance.

  • Usage: Some templates are so difficult to use that we doubt very much that even the originator uses them. If a developer can’t determine how to use your template, no one will ever use it and your effort is wasted. Consequently, you must design the template such that it meets security, reliability, and speed goals without becoming overly difficult to use.

  • Time: Every time you design a new piece of software, there is a time element involved. It’s essential to decide whether the template will ultimately save enough development time to offset the development cost of creating and testing it. A template that you only intend to use a few times may not be worth the effort.

  • Maintenance: Someone will have to maintain the code used to create the template. A good template is one in which the code is relatively straightforward. Of course, you need to add comments to the code that explain how the code works — and fully document the template design. Most templates see some level of redesign during their lifecycles — they evolve as developers use the template and discover new ways to incorporate it into applications.

The best template is the one that seems obvious. We were recently reading an article about the invention of the safety pin. The safety pin seems obvious, but someone still had to invent it because no one else had thought about it.

When you create a template and someone tells you that it seems like an obvious idea, don’t get mad. Be glad. You’ve joined the ranks of people who thought of something that fulfills an obvious need, but no one thought about it before you did.

The third decision you must make is how inclusive to make the template. In some cases, you want to create a template that can handle a range of situations. However, a template can quickly become unwieldy and difficult to manage. A good template is balanced — it includes the elements you need, but nothing beyond.