From 3779f868ad10de5a1821a1e7fdf326b04fce6d21 Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Fri, 1 Sep 2017 09:31:04 -0600 Subject: [PATCH] Add note on `assert` --- 05-Considering_Maintainability.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/05-Considering_Maintainability.md b/05-Considering_Maintainability.md index 402a08a..2844116 100644 --- a/05-Considering_Maintainability.md +++ b/05-Considering_Maintainability.md @@ -31,6 +31,22 @@ See http://mortoray.com/2015/06/15/get-rid-of-those-boolean-function-parameters/ Know and understand the existing C++ standard algorithms and put them to use. See [C++ Seasoning](https://www.youtube.com/watch?v=qH6sSOr-yk8) for more details. +## Never Use `assert` With Side Effects + +```cpp +// Bad Idea +assert(set_value(something)); + +// Better Idea +[[maybe_unused]] const auto success = set_value(something); +assert(success); +``` + +The `assert()` will be removed in release builds which will prevent the `set_value` call from every happening. + +So while the second version is uglier, the first version is simply not correct. + + ## Properly Utilize 'override' and 'final' These keywords make it clear to other developers how virtual functions are being utilized, can catch potential errors if the signature of a virtual function changes, and can possibly [hint to the compiler](http://stackoverflow.com/questions/7538820/how-does-the-compiler-benefit-from-cs-new-final-keyword) of optimizations that can be performed.