Interfaces are a contract between a service provider and a service consumer. The C++ Core Guidelines have 20 rules to make them suitable because “interfaces is probably the most important single aspect of code organization”.
I wrote in my last post about the first ten rules. Today I will finish my job and write about the remaining ten rules.
- I.1: Make interfaces explicit
- I.2: Avoid global variables
- I.3: Avoid singletons
- I.4: Make interfaces precisely and strongly typed
- I.5: State preconditions (if any)
- I.6: Prefer
Expects()for expressing preconditions
- I.7: State postconditions
- I.8: Prefer
Ensures()for expressing postconditions
- I.9: If an interface is a template, document its parameters using concepts
- I.10: Use exceptions to signal a failure to perform a required task
- I.11: Never transfer ownership by a raw pointer (
- I.12: Declare a pointer that must not be null as
- I.13: Do not pass an array as a single pointer
- I.22: Avoid complex initialization of global objects
- I.23: Keep the number of function arguments low
- I.24: Avoid adjacent unrelated parameters of the same type
- I.25: Prefer abstract classes as interfaces to class hierarchies
- I.26: If you want a cross-compiler ABI, use a C-style subset
- I.27: For stable library ABI, consider the Pimpl idiom
- I.30: Encapsulate rule violations
Let’s dive directly into the details.
I.11: Never transfer ownership by a raw pointer (T*)
There is a conceptual issue with this code.
Who deletes the pointer X? There are at least three alternatives to deal with the ownership problem:
- return the value, if possible
- use a smart pointer
- use owner<X*> from the guideline support library (GSL)
I.12: Declare a pointer that must not be null as not_null
What is the semantic difference between the three variations of the following function length?
The intention of variations two and three of length is quite apparent. The second variation accepts only a non-null pointer; the third accepts a nullptr. You may have already guessed it. not_null if from the GSL.
I.13: Do not pass an array as a single pointer
Passing arrays as a single pointer is quite error-prone.
What will happen if n is too big? Right: undefined behavior. The GSL offers a solution called spans.
Spans deduce their number of arguments.
I.22: Avoid complex initialization of global objects
Global objects provide a lot of fun. For example, their initialization order is not defined if they are in different translation units. The following code snippet has undefined behavior.
I.23: Keep the number of function arguments low
There is a simple rule: one function should do exactly one job. If that is the case, the number of function arguments automatically becomes low, making the function easy to use.
The New Parallel Algorithms of Standard Template Library, such as std::transform_reduce, often break this rule.
I.24: Avoid adjacent unrelated parameters of the same type
What are the source and the destination of the following copy_n function? Any educated guess?
I often have to look for documentation.
I.25: Prefer abstract classes as interfaces to class hierarchies
Of course, that is an evident and long-established rule for object-oriented design. The guidelines provide two reasons for this rule.
- abstract classes are more likely to be stable than base classes
- bases classes with state and non-abstract methods put more constraints on derived classes
I.26: If you want a cross-compiler ABI, use a C-style subset
ABI stands for Application Binary Interface.
This is a strange rule in C++ guidelines. The reason is that “Different compilers implement different binary layouts for classes, exception handling, function names, and other implementation details.”. On some platforms, common ABIs are emerging. Using a single compiler, you can stick to the full C++ interface. In this case, you have to recompile the code.
I.27: For stable library ABI, consider the Pimpl idiom
Pimpl stands for a pointer to implementation and is the C++ variation of the bridge pattern. The idea is that a non-polymorphic interface holds the pointer to its implementation. Therefore, modification of the implementation doesn’t require recompilation of the interface.
Here is an example from the C++ Core Guidelines:
The pimpl is the pointer that holds the handle to the implementation.
For an in-depth discussion of this C++ idiom, read the GOTW #100 article by Herb Sutter. GotW stands für Guro of the Week.
I.30: Encapsulate rule violations
Sometimes code is ugly, unsafe, or error-prone because of various reasons. Put the code in one place and encapsulate it with an easy-to-use interface. That is called abstraction, which you sometimes have to do. I have no problem with that code if the internal code is stable and the interface only lets you use it correctly.
I often mentioned the guideline support library in the last posts, including the current one. Now it’s time to look at insight, and I will write about it in the next post.
Thanks a lot to my Patreon Supporters: Matt Braun, Roman Postanciuc, Tobias Zindl, G Prvulovic, Reinhold Dröge, Abernitzke, Frank Grimm, Sakib, Broeserl, António Pina, Sergey Agafyin, Андрей Бурмистров, Jake, GS, Lawton Shoemake, Jozo Leko, John Breland, Venkat Nandam, Jose Francisco, Douglas Tinkham, Kuchlong Kuchlong, Robert Blanch, Truels Wissneth, Kris Kafka, Mario Luoni, Friedrich Huber, lennonli, Pramod Tikare Muralidhara, Peter Ware, Daniel Hufschläger, Alessandro Pezzato, Bob Perry, Satish Vangipuram, Andi Ireland, Richard Ohnemus, Michael Dunsky, Leo Goodstadt, John Wiederhirn, Yacob Cohen-Arazi, Florian Tischler, Robin Furness, Michael Young, Holger Detering, Bernd Mühlhaus, Matthieu Bolt, Stephen Kelley, Kyle Dean, Tusar Palauri, Dmitry Farberov, Juan Dent, George Liao, Daniel Ceperley, Jon T Hess, Stephen Totten, Wolfgang Fütterer, Matthias Grün, Phillip Diekmann, Ben Atakora, Ann Shatoff, Rob North, Bhavith C Achar, and Marco Parri Empoli.
Thanks, in particular, to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, Dendi Suhubdy, Sudhakar Belagurusamy, Richard Sargeant, Rusty Fleming, John Nebel, Mipko, Alicja Kaminska, Slavko Radman, and David Poole.
|My special thanks to Embarcadero|
|My special thanks to PVS-Studio|
|My special thanks to Tipi.build|
|My special thanks to Take Up Code|
I’m happy to give online seminars or face-to-face seminars worldwide. Please call me if you have any questions.
- Embedded Programmierung mit modernem C++ 12.12.2023 – 14.12.2023 (Präsenzschulung, Termingarantie)
Standard Seminars (English/German)
Here is a compilation of my standard seminars. These seminars are only meant to give you a first orientation.
- C++ – The Core Language
- C++ – The Standard Library
- C++ – Compact
- C++11 and C++14
- Concurrency with Modern C++
- Design Pattern and Architectural Pattern with C++
- Embedded Programming with Modern C++
- Generic Programming (Templates) with C++
- Clean Code with Modern C++
- Phone: +49 7472 917441
- Mobil:: +49 176 5506 5086
- Mail: schulung@ModernesCpp.de
- German Seminar Page: www.ModernesCpp.de
- Mentoring Page: www.ModernesCpp.org
Modernes C++ Mentoring,