doc: Add boolean simplifications guidelines to coding-style.rst

This commit is contained in:
Eduardo Almeida
2022-11-17 23:16:00 +00:00
parent fe43c82b92
commit b21189f015

View File

@@ -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
=================================