C++ Core Guidelines: Supporting Sections

Contents[Show]

Let's recapitulate. In the last two years, I have written about 100 posts to the C++ Core Guidelines. Why? The document answers:  "This document is a set of guidelines for using C++ well. The aim of this document is 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 quite short. It has only three rules which a few sentences of content. Their focus is programming language agnostic.

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 kind of a sub-system, which you can test or refactor in isolation. You can now not only test the sub-system but also the integration of the sub-system into the application. The first kind of tests is typically called unit-test and the second 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 is an implementation of the interface and can, therefore, quite quickly replaced by another, maybe more stable implementation.

 

Rainer D 6 P2 540x540Modernes C++ Mentoring

Stay informed about my mentoring programs.

 

 

Subscribe via E-Mail.

A.2: Express potentially reusable parts as a library

Okay, this is quite easy, but there are 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 quite blurry and are, therefore, difficult to answer. This holds in particular for the last question. Let me try it.

First of all, don't put too much effort into your code to make it reusable as a library because "You aren't gonna need it" (YAGNI),  but write your code that it could be reusable. This means, follow simple guidelines such as writing your code for understandability, maintainability, testability and other abilities because it's is highly probable, that you or another programmer has to work with your code in the future. Or to say it with 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 once more. Now you should think 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 for 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, 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: You should 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 difficult to test but impossible to reuse independently. Second, your libraries become more difficult 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 if they were a single component, c12.

What's next?

The next 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, Animus24, 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, and Dominik Vošček.

 

Thanks, in particular, to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, Dendi Suhubdy, Sudhakar Belagurusamy, Richard Sargeant, Rusty Fleming, John Nebel, Mipko, Alicja Kaminska, and Slavko Radman.

 

 

My special thanks to Embarcadero CBUIDER STUDIO FINAL ICONS 1024 Small

 

My special thanks to PVS-Studio PVC Logo

 

My special thanks to Tipi.build tipi.build logo

Seminars

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

Bookable (Online)

German

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++

New

  • Clean Code with Modern C++
  • C++20

Contact Me

Modernes C++,

RainerGrimmDunkelBlauSmall

Mentoring

Stay Informed about my Mentoring

 

English Books

Course: Modern C++ Concurrency in Practice

Course: C++ Standard Library including C++14 & C++17

Course: Embedded Programming with Modern C++

Course: Generic Programming (Templates)

Course: C++ Fundamentals for Professionals

Interactive Course: The All-in-One Guide to C++20

Subscribe to the newsletter (+ pdf bundle)

All tags

Blog archive

Source Code

Visitors

Today 4502

Yesterday 7411

Week 27730

Month 171901

All 11653055

Currently are 823 guests and no members online

Kubik-Rubik Joomla! Extensions

Latest comments