const and Visual Noise
Learn how const brings tons of visual noise.
Description
If we convert all unchanging variables to const
variables, we will pollute the code. Not only will our code be less readable, we will also need to type more.
This is a common argument. To be fair, this argument is right to a certain extent.
auto numberOfDoors{2u};
is indeed shorter than
const auto numberOfDoors{2u};
However, typing const
or typing almost anything is not a major issue. Creating a meaningful application does not depend on our typing speed.
Can we consider const
as visual noise?
Of course we can! But it is not actually visual noise…
Examples of visual noise
Let’s see some examples of visual noise.
Comments
Imagine these lines of code:
struct Car {
// ...
int m_horsePower; // Performance in horse power
};
or
/*
* Gets the performance in horsepower
*/
std::string getHorsepower() {
// ...
}
Comments that are similar to those above are definitely visual noise… They don’t give any new information. They represent the original sin of comments; they merely repeat what the code does instead of explaining the intention behind the code.
Get hands-on with 1400+ tech skills courses.