User:Bucket3432/Walls of text

This is a collection of bucket3432's walls of text in chat.

On declaring local variables at the top
Someone had posted screenshots of Lua code that had a list of local variable declarations at the top of the file and at the top of functions, separate from their definitions. Discussion ensued about why one would do that instead of combining the declaration and definition as is customary. Bucket3432's contribution:

"I'm not saying this style is necessarily wrong, but this is an outdated programming pattern. It was necessary in the old C days because of syntax constraints (the grammar was designed so that variables could only be declared at the top, presumably to make memory allocation easier on the compiler), but modern languages (including Lua) let you declare a variable anywhere. This is advantageous because it means the scope and lifetime of the variable can be as tight as necessary and no wider. Code written like that is:


 * easier to reason about because its intended use is clearer from the surrounding context and you don't have to remember what has been declared,
 * easier to maintain because it is much harder to accidentally leave hanging declarations when changing or deleting code, and
 * easier to refactor because you can pick up a chunk of code and move it anywhere without having to find and remove an additional declaration and add it back in the new place.

"Plus that not all the variables here are function."

I think this is an issue because you can't tell if a name is callable or not just by looking at its declaration. As I alluded to earlier, putting all of your declarations at the top means that the declarations are taken away from important context, making them next to useless for understanding why they exist.

Having all the declarations at the top is great for having a list of local variables, but why do you want that? What value does that give you? I can understand for global variables, but these are local variables, which means they are essentially throw-away containers that don't really matter after you're done with them, which means they shouldn't be given that much importance. Variables are a tool to achieve a goal, and declarations are an implementation detail of the language. It is much more natural to use a variable right when you need it than to have to declare it first. Implementation details should be abstracted away as much as possible because humans don't think in implementation details, and you should be writing code for humans, not machines"

- bucket3432