From b21189f0157df54244f62603d700618ea0966d59 Mon Sep 17 00:00:00 2001 From: Eduardo Almeida Date: Thu, 17 Nov 2022 23:16:00 +0000 Subject: [PATCH] doc: Add boolean simplifications guidelines to coding-style.rst --- doc/contributing/source/coding-style.rst | 47 ++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/doc/contributing/source/coding-style.rst b/doc/contributing/source/coding-style.rst index 64b9a3a71..734409be0 100644 --- a/doc/contributing/source/coding-style.rst +++ b/doc/contributing/source/coding-style.rst @@ -1099,6 +1099,53 @@ can be rewritten as: n += 3; return n; +Boolean Simplifications +======================= + +In order to increase readability and performance, avoid unnecessarily complex boolean +expressions in if statements and variable declarations. + +For instance, the following code: + + .. sourcecode:: cpp + + bool IsPositive(int n) + { + if (n > 0) + { + return true; + } + else + { + return false; + } + } + + void ProcessNumber(int n) + { + if (IsPositive(n) == true) + { + ... + } + } + +can be rewritten as: + + .. sourcecode:: cpp + + bool IsPositive(int n) + { + return n > 0; + } + + void ProcessNumber(int n) + { + if (IsPositive(n)) + { + ... + } + } + Smart pointer boolean comparisons =================================