All posts in "Expressive code"

How to Make If Statements More Understandable

Published July 21, 2017 - 10 Comments

If statements are necessary to build our code. Granted, some ifs are bad, like those that try to emulate polymorphism by testing a series of types. Those, you want to stay away from. But the ones that implement domain rules are good, and even an opportunity to make your code more expressive by showing how […]

The Vector Monad in C++, Without the Ugly Stuff

Published July 14, 2017 - 3 Comments

Now that we’ve got our feet wet and have a feeling of the vector monad in C++, let’s use modern C++ to make a more elaborate implementation of the vector monad, but that leads to cleaner code. You’ll note that the way of thinking here has a lot in common with the optional monad in […]

Dealing with Multiple Paths with the Vector Monad in C++

Published July 11, 2017 - 2 Comments

After having explored how to deal with multiple error handling with the optional monad in C++, let’s take inspiration again from the functional programming world, and see our familiar std::vector from a very unusal perspective. Although this is an application of the concept of monads, we will focus on how to write code in C++, […]

The Optional Monad In C++, Without the Ugly Stuff

Published July 7, 2017 - 3 Comments

The last post on Fluent C++ showed how several functions that could fail could be chained together by encapsulating the checks into an optional monad, so that the calling code doesn’t have to worry about checking each function call. That post stirred up a lot of reactions. Some people found it interesting and inspiring. Other […]

Multiple error handling with the optional monad in C++

Published July 4, 2017 - 16 Comments

Error handling is a vast topic in programming, and in C++ in particular. The aspect I would like to cover with you today is how to deal with multiple errors. Let’s consider the following 4 functions:

These functions should be called in turn: the result of f1 (called twice) is passed to f2, then the […]

How to Insulate a Toxic Api from the Rest of Your Code

Published June 30, 2017 - 2 Comments

Sometimes in our quest to writing expressive code we encounter dragons on our way. They can take the form of an old API, that seems to have been designed to make developers suffer, or even to have no design at all. You probably have already come across such APIs, haven’t you? Some of these dragons we can slay by refactoring, but some […]

How to Flatten Out A Nested Switch Statement

Published June 27, 2017 - 1 Comment
Flatten switch case nested collapse expressive C++

With my team we’ve recently come across an annoying switch nested in another switch statement, and I want to show a solution for flattening out this sort of structure. Motivation Let’s consider two enums representing the size and color of a shirt. While I don’t work in the clothing industry, using a simple example by stripping […]

The Interface Principle in C++

Published June 20, 2017 - 12 Comments

The Interface Principle in C++ encompasses a specific combination of features and ways of considering what an interface is, that allows to write expressive C++ code that preserves encapsulation. It has been around for a while, is still currently used, and may be enriched in the future versions of the language. So it’s worth being aware of. Note that […]

The real difference between struct and class

Published June 13, 2017 - 10 Comments

“Should I use a struct or a class?” Such is the question many C++ programmers ask themselves, or ask around to more experienced co-workers, when designing their code. There is sometimes a cloud of misconception about what the difference between struct and class technically is, particularly amongst the youngest developers. And once we get to understand the technical difference, […]