1. 12
  1.  

  2. 7

    I wish they wrote more about ways to return values and report errors:

    • return errors (bool? error code? error object?)
    • out parameters
    • exceptions
    • return std::variant (or std::optional)
    • errno

    Bonus points if all these things clash in a single code base. Error design is super hard in C++. It’s much easier in Rust thanks to Result, but it’s still hard to get right in Rust!

    For example, one of our C++ guidelines is to only allow a single ‪return statement to exist in a function. A danger with having multiple ‪return statements is that adding new ‪return statements to an existing function can potentially miss a required piece of logic that was setup further down in the function.

    I believe that this rule is misguided. I much prefer the opposite: All resources are managed by RAII. Cleanup jobs are managed in the destructor of local objects on the stack (you can also create your own defer call with a few lines of C++). Write your code such that you can return or throw exceptions whenever you like and everything is cleaned up automatically, as it should be. The reliability of code written like this is amazing and it’s very easy to read, much easier than if every function is always traversed all the way to the end. This reduces cognitive load. My functions are riddled with return statements and have a huge cyclomatic complexity, but they are very easy to understand and modify. You can add a throw or return statement wherever you like and nothing breaks.

    1. 2

      Resources in C++ are usually managed by RAII, they’re probably referring to other state changes, such as message broadcasts, etc. which often occur conditionally in game code. I’m amazed they don’t allow an exception for guard clauses.

      1. 1

        It doesn’t matter if it’s a resource or a necessary state change that has to happen before returning. You wrap it in the destructor of a local object on the stack (defer it) to make sure it’s always executed no matter how you leave the function.

      2. 1

        Single return also was a standout for me, but I wasnt sure if it was a result of me not using C++. Thanks for making remarks about this!

      3. 2

        I wonder if they have some automated validators for their custom code formatting (i.e. I hope they don’t rely only on code reviews to correct mistakes). I’ve never encountered the “don’t put space before = so it won’t be confused with ==”, so I’m wondering if they some automated way of validating code of e.g. new employees. I would use such tool myself ;)