Författare: Eric Bäckhage

  • Proper ways to use std::unique_ptr

    Smart pointers remove many of the questions regarding the responsibility to free dynamically allocated memory and remedies much of the risk with memory leaks due to missing delete calls. The default smart pointer to use in C++ is the unique pointer, std::unique_ptr. There are however some do’s and don’ts when it comes to using unique […]

  • When to add code comments

    When is the best time to add a new comment? Answer: before writing the code! You might think adding comments before code will be impossible, how can you know the details of the code before you have written it? Well, you can’t, and that is exactly why writing comments first will make your comments a […]

  • Guidelines for commenting code

    Writing good comments is hard and as developers we have come up with several excuses to not write comments at all. In this post I will try to debunk some of the most common excuses for not writing comments and give some good reasons for writing them and how to write them so that they […]

  • Spaceship simplifications

    I ended the last post by stating that I would provide an example of how you can implement the new C++ spaceship operator for your own class. In this post I will show you how that can be done and how it can simplify your implementation a lot. Assume that you have defined your own […]

  • C++ Spaceship operator usage

    C++20 introduced a new three-way comparison operator, also known as the spaceship operator. Here I explain how it works and show some simple examples. First, let’s take a look at some code, which I soon will explain: This code shows a simple three-way comparison of two integers. Since comparing the integers can have three different […]

  • Had Enough of Dvorak

    After several years with Dvorak I’m switching back to QWERTY. Actually I already have, I’m typing this text using the QWERTY keyboard layout. There are a couple of reasons why I have decided to give up on Dvorak after all these years. We do a lot of screen sharing at work and it just doesn’t […]

  • Get to know constexpr

    I did my first run with C++ before modern C++ was a thing. That is, before C++11 and C++14. Now that I’m once again developing application code using C++ I’ve learned to master great things like smart pointers, move semantics, and lambda expressions. One modern C++ concept that I haven’t put so much thought to […]

  • When an int is not an int

    The more I learn about C++ the more I understand that it is considered a difficult language to master. It has all these quirks and things that are far from obvious which may lead to unforeseen behavior. As an example, look at the following code: From the headline of this post and the introductory text […]

  • Hidden conditions

    One way of making code easy to understand and reason about is to try to ensure that it does exactly what you expect it to do. Sometimes however, there are conditions secretly hidden in places of the code that leaves you scratching your head and forces you into a quest for answers down the deepest […]

  • The art of keeping things simple

    As a software developer you go through different phases in your personal development. Many, but far from all, end up valuing simple code. Simple does not imply that the code doesn’t do what it needs to do, it means that the code is written in a way that puts minimal cognitive load on the human […]