I am pretty new at my place, so I should think twice before voicing concerns, but some of the code I have seen ...
When I try to bring up readability, I am told that there is not always time for that, that efficiency is far more important.
But then I see variable redeclaration inside of different types of loops, sometimes down to two levels. Part of me thinks - do not ever do that! But the other part says - this complicated function should be broken down into several functions anyway. Those smaller functions can have temp variables, and a compiler should be able to take care of them.
Then the function calls add some additional cost to it. Let me try to come up with 2 examples:
Class1::Do1()
{
for (int i = 0; i < 100; i++)
{
bool x = GetSomeValue();
...
if (x)
{
...
}
}
}
vs
Class1::Do1()
{
bool x = false;
for (int i = 0; i < 100; i++)
{
x = GetSomeValue();
...
if (x)
{
...
}
}
}
vs
Class1::Do1()
{
for (int i = 0; i < 100; i++)
{
Do2();
}
}
Class1::Do2()
{
bool x = GetSomeValue();
...
if (x)
{
...
}
}
The first way looks wrong to me, I always prefer second or even third when I write the code myself. I think that the third way might be even slower due to extra function calls. The first way might even look sketchy at times - in case the function is long, the declaration will be a number of lines away from where it is used. The other thing is that my example is too simple - the compiler could probably figure out how to simplify, and perhaps inline all 3. Unfortunately right now I cannot recall other examples of what I think is sloppiness, just want to mention that some variables are redeclared n*m times, because they are two levels deep (within 2 loops).
The devil's advocate says - how do you know 100% that this might not be efficient? The purist (my version of it) in me thinks that it is stupid to redeclare the same variable over and over and over - at the very least it throws one off when reading the code.
Thoughts? Questions?