3 ms·
pet peeve alert! I also prefer declaring all variables at the start of a function. My rationale is that, if all functions are written that way, it's less cogni
by allenbrunson 17y ago
pet peeve alert!
I also prefer declaring all variables at the start of a function. My rationale is that, if all functions are written that way, it's less cognitive load when reading them. All variables used are known at the beginning, and once I've digested that, I can think about the code that will be working with them. These days, if I'm reading a function and a new variable is declared partway in, I feel like I've been ambushed with a new thing that makes it harder to understand.
That said, I tend to write such short functions that it's never an issue. If a particular variable isn't needed until halfway into the function, that's a good sign to me that it needs to be broken into two or more smaller functions.