When we saw How to Assign Derived Classes in C++, we came up with a technique involving runtime polymorphism mixed with CRTP. This allowed derived classes to benefit from a polymorphic assignment operator without implementing it themselves, thus reducing boilerplate code. But assignment is just a special case of a polymorphic function to implement on a […]
A classical problem in object-oriented programming, and with polymorphism in general, is to handle multiple polymorphic objects at the same time. In other terms, multiple dispatch. An associated problem with object-oriented programming is that many languages, including C++, don’t implement multiple dispatch. One case comes up often: handling the behaviour of two objects of the […]
When thinking of polymorphism, and in particular of runtime polymorphism, the first thing that comes to mind is virtual functions. Virtual functions are very powerful, and fit for some use cases. But before using them, it’s a good thing to consider our exact need for polymorphism, and look around if there are other, more adapted […]
This is a guest post by Marin Peko. Marin is a Software Engineer working at Cellusys, a company providing telecommunication solutions, and follower of Fluent C++. You can find him on LinkedIn and Github. Logical expressions are probably one of the most used concepts in computer science and certainly a large part of each code […]
Imagine yourself discovering a part of your codebase and, in the midst of your exploration, you’re coming across an if statement of an honourable stature, featuring an `if` branch, an `else if` branch, and an `else`. As you’re approaching it with a mix of suspicion and curiosity, the if statement presents you its foremost part: […]
Clean code guidelines recommend keeping functions short, because long functions are difficult to understand and maintain. However, there is legacy code out there were functions span across hundreds, or even thousands of lines. There is now way someone can keep so many lines of code in their mind. By scrolling through such a function, we […]
Filtering lines based on a certain pattern is a common task in the everyday life of a programmer. For example we saw in a recent post the technique taken from The Legacy Code Programmer’s Toolbox that consists in filtering code on control flow keywords in order to get an overview of its structure. We’re going to write a […]
Long functions are hard to understand, and to write expressive code we generally try to keep functions short enough to get an overview of what they’re doing. The exact threshold over which a function becomes too long has been debated and is not clear today (see Code Complete, section 7.4 for a discussion about this), […]
When writing unit tests, we get to write a lot of code to compare observed results with expected results. Try this case, get the results, compare them with the expected values. Then try this other case, get the results, and check with their expected value. Then try with this third case, and so on and […]
Since C++11, the C++ standard library contains the <regex> header, that allows to compare string against regular expressions (regexes). This greatly simplifies the code when we need to perform such operations. The <regex> header comes with a lot of features, and it might not be easy to know where to start. The first time I used it, […]