re: Why you shouldn't reassign values in JavaScript VIEW POST

VIEW FULL DISCUSSION
 

WTH ? I strongly believe this promotes stupidity amongst programmers. It's verry natural for a function to be able to access global variables, both for read and write operations. It's your job as a programmer to fully understand the function you're trying to change, which variables are local and which are global. Not to mention that you could have naming convensions like "all global variables start with g_". And on top of that, it's a good practice to comment your code, for both you and others. Simply placing a comment like "// here we set value X in global variable Y" makes things crystal clear.

If we would use each variable only once, we would need to work with far more variables and our code would be alot bigger and alot messier. Not to mention we would increase memory size.

A simple example i can think of, often found in my codes, is something like this (pseudocode here, i work mostly in PHP):

var arrData = DATABASE->SomeQuery(1);
do something with arrData, from above query only,
  usually iterating through each row and calculate something,
  put final data in, let s say, variables A, B, C;

arrData = DATABASE->SomeQuery(2);
do something with arrData, from above query only,
  usually iterating through each row and calculate something,
  put final data in, let s say, variables D, E, F;

... and so on

Basically, if i would need 5 different queries, returning large amounts of data, but i only need that data for a verry short amount of time, why on earth would i want to create separate variables to load my memory more than needed ?

 

Optimization can often lead to code that is harder to read, test and maintain. You are right saying that is the task of an programmer to understand the code. But I would not like to work in such code. Moreover in times of huge in-memory-databases, memory is not as expensive as programmers maintaining and extending the code.

code of conduct - report abuse