cog wheels 2125178 1280

C++ Core Guidelines: Supporting Sections

Let’s recapitulate. I have written about 100 posts on the C++ Core Guidelines in the last two years. Why? The document answers:  “This document is a set of guidelines for using C++ well. this document aims to help people to use modern C++ effectively.”. But my story does not end here. The guidelines have a supporting section.

cog wheels 2125178 1280

I know, 100 posts are quite a lot. Before I dive into the supporting sections of the guidelines, I want to give you assistance to find my existing posts to the C++ Core Guidelines.

  1. You can use the category C++ Core Guidelines and get all posts.
  2. You can directly jump to the TOC >>Start Here<<  of my blog. Here is the section to the  C++ Core Guidelines in my 300 posts.

Here is an overview of the supporting sections.

Let’s see what’s inside.

A: Architectural ideas

The first section is relatively short. It has only three rules which are a few sentences of content. Their focus is programming language agnostic.

 

Rainer D 6 P2 500x500Modernes C++ Mentoring

Be part of my mentoring programs:

  • "Fundamentals for C++ Professionals" (open)
  • "Design Patterns and Architectural Patterns with C++" (open)
  • "C++20: Get the Details" (open)
  • "Concurrency with Modern C++" (starts March 2024)
  • Do you want to stay informed: Subscribe.

     

    A.1: Separate stable code from less stable code

    Here is the sentence to the rule: “Isolating less stable code facilitates its unit testing, interface improvement, refactoring, and eventual deprecation.” Okay, what does that mean?

    Putting an interface between stable and less stable code is the way to separate it. Due to the interface, your less stable code becomes a sub-system that you can test or refactor in isolation. You can now test the sub-system and the integration of the sub-system into the application. The first test is typically called a unit test, and the second is a sub-system integration test. The sub-system has two channels to the app: the functional and the non-functional channel. Both have to be tested. The functional channel provides the functionality of the sub-system, and the non-functional channel the exceptions that can happen and to which the application may react. Thanks to the interface, the concrete sub-system implements the interface and can, therefore, be quite quickly replaced by another, maybe more stable implementation.

    A.2: Express potentially reusable parts as a library

    Okay, this is relatively easy, but there is a more difficult question two answer in this regard.

    1. When is a part of software potentially reusable?
    2. When do the costs of implementing the library pay off?
    3. What is the right kind of abstraction?

    The three questions are blurry and, therefore, difficult to answer. This holds in particular for the last question. Let me try it.

    First, don’t put too much effort into making your code reusable as a library because “You aren’t gonna need it” (YAGNI),  but write your code so it can be reusable. This means following simple guidelines such as writing your code for understandability, maintainability, testability, and other abilities because it is highly probable that you or another programmer has to work with your code in the future. Or to say it in the words of Philip Wadler: “Make your code readable. Pretend the next person who looks at your code is a psychopath, and he knows where you live.

    “Don’t repeat yourself” (DRY) when you need the same or similar functionality again. Now you should think about the very latest about abstraction. When I have two similar functions, I write a third function which stands for the implementation, and the similar functions are just wrappers for using the implementation function.  Here are my ideas, put into code to make my point.

    std::vector<void*> myAlloc;
    
    void* newImpl(std::size_t sz,char const* file, int line){           // (3)
        static int counter{};
        void* ptr= std::malloc(sz);
        std::cerr << file << ": " << line << " " <<  ptr << std::endl;
        myAlloc.push_back(ptr);
        return ptr;
    }
    
    void* operator new(std::size_t sz,char const* file, int line){      // (1)
        return newImpl(sz,file,line);
    }
    
    void* operator new [](std::size_t sz,char const* file, int line){   // (2) 
        return newImpl(sz,file,line);
    }
    

     

    The overloaded new operators in the simple form (line 1) and arrays (line 2) invoke the implementation in line (3).

    I don’t want to answer question 3 because the answer depends on many factors. It depends on the domain of the software. Does the software, for example, run on a desktop, embedded device, or a high-trading server? It depends on factors such as maintainability, testability, and scalability, … but also on performance. It depends on the skill level of the users. Maybe, your library is an infrastructure library or a library for your customers.

    Writing reusable software in the form of a library is about 3-4 times more effort than doing a one-way shoot. My rule of thumb is to think about a library when you know you will reuse the functionality. You should write a library when you reuse the functionality at least two times.

    A.4: There should be no cycles among libraries

    Cycles among libraries make your software system more complicated. First, it makes your libraries challenging to test but impossible to reuse independently. Second, your libraries become more challenging to understand, maintain, and extend. When you find such a dependency, you should break it. There are a few possibilities due to John Lakos (Large Scale C++ Software Design, p185):

    1. Repackage c1 and c2 so they are no longer mutually dependent.
    2. Physically combine c1 and c2 into a single component, c12.
    3. Think of c1 and c2 as a single component, c12.

    What’s next?

    The following supporting section to non-rules and myths has more content. I assume you already know most of the non-rules as myths. Let me demystify them in my 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, 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, Marco Parri Empoli, moon, Philipp Lenk, Hobsbawm, and Charles-Jianye Chen.

    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
    My special thanks to SHAVEDYAKS

    Seminars

    I’m happy to give online seminars or face-to-face seminars worldwide. Please call me if you have any questions.

    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++
    • C++20

    Online Seminars (German)

    Contact Me

    Modernes C++ Mentoring,

     

     

    0 replies

    Leave a Reply

    Want to join the discussion?
    Feel free to contribute!

    Leave a Reply

    Your email address will not be published. Required fields are marked *