All posts in "Expressive code"

A Simple Habit to Avoid Complex Names and Typos in Code

Published December 27, 2021 - 0 Comments

Don’t you find it a little unsettling when you encounter a typo in code? std::unordered_map<int, Value> MyClass::getInedxedValues() const { // … } And the code looks even more careless when that typo is repeated several times across the codebase, in code that depends on the butchered symbol: auto const table1 = x.getInedxedValues(); auto const table2 […]

The Evolutions of Lambdas in C++14, C++17 and C++20

Published December 13, 2021 - 0 Comments

Lambdas are one of the most popular features of Modern C++. Since their introduction in C++11, they’ve become ubiquitous in C++ code. But since their appearance in C++11, they have evolved and gained significant features. Some of those features help write more expressive code, and since using lambdas is so common now, it is worth […]

Design Patterns VS Design Principles: Template Method

Published November 17, 2021 - 0 Comments

In today’s episode of “Design Pattens VS Design Principles” series, we relate the Template Method design patterns to more general design principles. We’re showing how it relates to the Polymorphism design principle. The GoF Meets the GRASP If you’re just joining us in the series, here is what it’s about: we’re going over each of the […]

A Recap on User Defined Literals

Published October 8, 2021 - 0 Comments

User defined literals were introduced in C++11, evolved in C++14 and C++17, and are a nice way to write more expressive code. The general idea behind user defined literals is that they allow to write a value and tack on a term describing what this value represents. For example: auto const quantity = 42_bottles_of_water; In […]

How to Define Comparison Operators by Default in C++

Published August 23, 2021 - 0 Comments

Implementing comparison operators in C++ is easier said than done. Indeed, for most types, if we could talk to the compiler we would say something like: “to order them, use a lexicographical order on their members”. But when it comes to writing the corresponding code, things get more complicated. However, a classical technique using std::tuple makes […]

Extended Aggregate Initialisation in C++17

Published July 17, 2021 - 0 Comments

By upgrading a compiler to C++17, a certain piece of code that looked reasonable stopped compiling. This code doesn’t use any deprecated feature such as std::auto_ptr or std::bind1st that were removed in C++ 17, but it stopped compiling nonetheless. Understanding this compile error will let us better understand a new feature of C++17: extended aggregate initialisation. The […]

How to Return Several Values from a Function in C++

Published July 9, 2021 - 0 Comments

Functions should take their inputs as parameters and produce outputs with their return types. This is the basics of functions interface design. This makes functions easier to understand just by looking at their prototype. It makes functions functional. But C++ only allows to return one value out of a function. What if we’d like to […]