Declaring variables behind the loop or inside the loop? - page 6

 
Georgiy Merts:

Personally, I strongly dislike having a bunch of variables scattered throughout a program and having to look for each time where a variable is created. This is why whenever possible, I try to declare variables at the beginning of a function, all together - just to see where they are created and to understand when they are about to be deleted.

What do you mean "scattered throughout the program"? A local variable is declared and applied only in the block where it is used, and if you have to search for it, it means that your code is so long that you can't find it.

Declaring all variables at the beginning of a function is just a bad, archaic style. Even old man Strastrup urged to reject such old-school relics of old C infavor of proper and reliable code: declaring a variable right where it's used. Don't you realize that your approach is fraught with lots of random mistakes. Do you initialize variables with something? Or just "take it easy" and then deal with unpredictable code behavior? ) Maybe once it was all justified due to low-powered hardware and weak compilers, but certainly not now.

 
pivalexander:

You are always poking fun at books and praising yourself, but the essence of the issue and did not say anything but theory around the bush, read it you will understand ... Memory and CPU, CPU and memory, the power supply - it's also used there!

In contrast, those who need to understand the meaning of my texts, and wretched programmers always need to spoon-feed and give a ready-made answer on a silver platter, they never want to understand anything. Therefore, my recommendations, although they are written in response to you, but most likely are not intended for you, but for those who want to learn and bring their skills to perfection.

I believe there are two types of people - those who having seen a rake on their way, only by test determine the result of stepping on it, and those who knowing elementary laws of physics, data on element weight can tell the result in advance without stepping on it.

 
Alexey Navoykov:

Declaring all variables at the beginning of a function is just a bad archaic style - even old man Strastrup urged to discard these old-school remnants of old C in favor of proper and reliable code: declaring the variable directly where it's used.

imho, bravo! - convincingly, when using variables in local scope it is less likely to hit an uninitialized variable, or rather a value that was in the previous use of the variable, the compiler will not generate a warning, and searching for such small nasties is tedious

By the subject and tests, well, about nothing, more than 5-7% of the difference is not visible, and it is not a fact that the behavior in the next builds will not change

 
Aleksandr Matveev:

Those who need - the meaning of my texts will understand, and woe programmers always need to chew and give a ready answer on a platter, they never want to understand anything. Therefore, my recommendations, although they are written in response to you, but most likely are not for you, but for those who want to learn and bring their skills to perfection.

I believe there are two types of people - those who having seen a rake on their way, only by test determine the result of stepping on it, and those who knowing elementary laws of physics, data on element weight may predetermine the result without stepping on it.

I believe there are two types of people - those with inflated loftiness, those who came to amuse your self-esteem and they have no really useful comments on the subject here, in my opinion they did not even delve into it ...

and there are those... Never mind, it's not you anyway:)

Aleksandr Matveev:

I have a suspicion that the author of this statement has no idea how the processor, memory and compiler work... I bet that any of your code can be accelerated at least ten times or even hundreds times. Because of such shitty coders most products are unbearably slow on most powerful computers with dozens of cores, while you just need to think a little... But some people think, "Why think? You have to code..."

And I have a suspicion that you're still stuck at the level of"87, starting with the EC1020 and the ZX Spectrum,"

"Shitcoder" is you, that's why you hired 600 people...

No offence

PS: speed up my code, ok, not by hundreds but at least by tens of times, the code is in the beginning of the thread, in the first post, if you're not a shitcoder of course, you can speed up any of my code by 10 or 100 times, come on...

 
pivalexander:

I think there are two types of people - those with exaggerated arrogance, those who came to amuse their ego and they haven't got a single really useful comment on the matter, in my opinion they haven't even delved into it

and there are those... Never mind, it's not you anyway :)

And I have a suspicion that you're still stuck at the level of"87 years starting with EU1020 and ZX Spectrum,"

"Shitcoder" is you, that's why you hired 600 people...

No offence

six pages of discussion of such nonsense, I'm quietly livid ))))) the degradation of the forum is obvious )))

 
Igor Makanu:

imho, bravo! - convincingly, when using variables in local scope it is less likely to hit an uninitialized variable, or rather a value that was in the previous use of the variable, the compiler will not generate a warning, and searching for such small nasties is tedious

To the subject and tests, well, about nothing, more than 5-7% difference is not visible, and it is not a fact that the behavior in the next builds will not change

Do you know that declaring a local variable without initialization does not guarantee that it has a zero value? After all, it is created on the stack and anything can be there.

void Func()
{
   int n = 0;// только так
   ....
}
P.S. I myself have fallen into the sin of discussion ))
 
Igor Makanu:

on the subject and the tests, well, it's about nothing, no more than 5-7% difference can be seen, and it's not certain that the behaviour won't change in the next builds

Yes, well, ideally there should be no difference at all because the situation is quite simple to optimize. And even if the loop body is put into a separate function, it will be the same, I'm sure. But the paranoiacs will have to drag this string by reference into a function, complicating the code )
 
Alexey Navoykov:
Yeah, well ideally there shouldn't be any difference at all since the situation is quite simple to optimize. And even if we put the loop body into a separate function, it will be the same, I'm sure. But paranoiacs will have to drag this string by reference into a function, complicating the code )

There test results should float by 5-15% from startup to startup, the wind is not a real-time OS

 
Alexey Volchanskiy:

Do you know that declaring a local variable without initialization does not guarantee that it has a zero value? After all, it's created on the stack and anything can be there.

P.S. here i fell into the sin of discussion myself )))

))))) Wow!

SZZ: initialization when declaring a variable is the basis for writing any code with minimum errors... even the compiler will tell you something ;)

 
Igor Makanu:

))))) , wow!

ZS: initialisation when declaring a variable is the basis for writing any code with a minimum of errors... even the compiler will say something here ;)

Just met a bunch of people who think the compiler zeroes in on local variables just like it does on global.

And the compiler doesn't give out any warrants

void OnTimer()
{
    string st;
    int n;
    Print(st, n);// специально печатаем неиниализированные, варнингов нет
    for (int i = 0; i < 1000; i++)
    {
        st = IntegerToString(i);
    }
}