All posts in "Expressive code"

Minor, Major and Overarching Design Principles

Published July 1, 2021 - 0 Comments

Design principles are guidelines about how to organize and structure our code to make it manageable. They come through experience, in the general sense of the word. It can be one individual’s own trial and errors that makes them realize what options make code more simple. But in the more general sense, design principles stem […]

GRASP: 9 Must-Know Design Principles for Code

Published June 23, 2021 - 0 Comments

In order to write code that is understandable, maintainable and that stands the test of time, one of the crucial skills that we all need to have is design. What does code design mean? In my definition, doing code design means deciding which class (or more generally which component) is in charge of which responsibility. […]

The Subtle Dangers of Temporaries in for Loops

Published May 22, 2021 - 0 Comments

Even though very convenient to write concise code, temporaries are an endless source of bugs in C++. Are we allowed to use a temporary in a range based for loop? Consider the following code: std::vector<int> create_range() { return {1, 2, 3, 4, 5}; } int main() { for (auto const& value : create_range()) { std::cout […]

A Default Value to Dereference Null Pointers

Published May 14, 2021 - 0 Comments

With C++17, modern C++ has acquired a nullable object: std::optional. optional has a pretty rich interface, in particular when it comes to handling null optionals. On the other hand, the oldest nullable type in C++, pointers, doesn’t have any helper to make the handling of its nullity more expressive. Let’s see what we can do […]

Trailing Return Types

Published April 16, 2021 - 0 Comments

This article is NWH, standing for Not Written Here. The concept of NWH is inspired from the NIH (Not Invented Here) syndrome which consists in refraining from using existing code from outside the company and reinventing the wheel every time. Just like it is good practice to look out for solutions developed elsewhere, we’re going […]

What auto&& means

Published April 2, 2021 - 0 Comments

Since C++11, we have a && in the language, and it can take some time to understand its meaning and all the consequences this can have on your code. We’ve been through a detailed explanation of lvalues, rvalues and their references, which covers a lot of ground on this topic. But there is one aspect […]

What C++ Fold Expressions Can Bring to Your Code

Published March 19, 2021 - 0 Comments

In the previous post we saw how fold expressions work. We saw how to define them, how to control their associativity, and how to handle empty variadic packs in a fold expression. But all along we’ve been using an example that didn’t bring much value to code: a function that makes the sum of its parameters: template<typename… […]

C++ Fold Expressions 101

Published March 12, 2021 - 0 Comments

C++17 brought fold expressions to the language. This interesting feature allows to write expressive code, that almost seems magical. Here is a two-posts recap on how fold expressions work (this post) and how they can improve your code (the next post). Fold expressions A fold expression is an instruction for the compiler to repeat the […]

std::index_sequence and its Improvement in C++20

Published March 5, 2021 - 0 Comments

It would be great if we could iterate on the values of a std::tuple like we do for a std::vector or for other STL containers. But we can’t. To iterate on the values inside of a tuple, we need to proceed in two steps: instantiate a std::index_sequence object with std::make_index_sequence, pass it to another function that performs the […]