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 […]
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: int f1(int a); int f2(int b, int c); int f3(int d); int f4(int e); These functions should be called in […]
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 […]
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 […]
Summer Is Coming. With it comes the sea, the sun, the beach, or the mountain or perhaps your family house. But there is also a great thing that comes with summer: more time. Maybe you’re taking some time off, or maybe this is just because work is less intensive during this period. In all cases, summer is […]
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 […]
If you’re like the majority of software developers working for a company, you probably have to deal with legacy code, at least sometimes. Well maybe you don’t because you’re working on a brand new project with few people. But chances are you do. I for sure have faced legacy code. Many times, and coming in […]
“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, […]
C++14 brought a interesting feature on associative containers that was cruelly missing for certain use cases: the ability to search an associative container with something that is semantically a key, even if it is not technically a key. This fills a very similar need as the one in Functors are not dead: the double functor trick, but […]
“Give me a string representation of this object.” This is a fairly ubiquitous sentence in programming, that many languages express in one brief statement: Java has .toString(), Python has str and Haskell has show, to cite just a few. My goal here is to propose a concise way to also express this in C++. Note: after I wrote […]