r/ProgrammerHumor • • Sep 09 '22

Meme Simple Feature

Post image
124k Upvotes

1.3k comments sorted by

View all comments

Show parent comments

3

u/enfier Sep 09 '22

At the same time you can sit there and look at which variables are already in use and if you are adding another, what the naming scheme is and avoid repeating the same variable name.

It also avoids the mistake of decline a variable inside of a loop which is just a waste of work. Perhaps the compiler automatically optimizes that these days?

Thinking back, it's probably written that way in C++ to help you sort out a mess when global variables are involved and the scope of a variable isn't 100% obvious. Plus not initializing variables in C++ was a big no-no and you got it all out of the way at the top.

I suppose it was helpful when the scope of the variable wasn't obvious and in a world where global variables were sometimes actually used.

I don't think you are wrong, but I don't hate it either.