3 ms·
I agree with this too. Pulling constants out to the top a source file is often a mistake. It's useful if someone is looking at the source file to find the cons
by AndrewDMcG 11y ago
I agree with this too. Pulling constants out to the top a source file is often a mistake. It's useful if someone is looking at the source file to find the constant, but usually it's more likely that they're reading the code to understand it, and then need to know the value of the constant. In these cases it can be better to have a constant but define it close to where it is used.
- falcolas 11y agoPersonally, if I'm reading the code, seeing `maxRetries` is more meaningful than 5. `maxRetries` both abstracts away the 5 (which could change at whim), and makes it so I don't have to parse the surrounding code to identify exactly what that loop is doing (unless it's not working, in which case I have to parse that loop anyways). It also encourages you (for better or worse) to use `maxRetries` in multiple places. The value of 5 is, in the long run, meaningless. It could have easily been 3 or 10.
- bryanlarsen 11y agoBut what if maxRetries is 0 or 1000? (I've seen both). That really changes the meaning of the code you're reading.
- falcolas 11y ago1000 doesn't change the meaning at all - it's still retrying a block of code some finite number of times. 0 does change it a bit, but only in that the code is not being executed, not that there is suddenly no retry logic which exists in the code.
- ajanuary 11y agoIDEs make it trivial to find the value of a constant. IDEs like IntelliJ and Chrome developer tools will even show you them inline while debugging.