3 ms·
These blocks have other advantages such as having access to the outer scope. Yes those could be passed in as arugments to a function, but I think there are many
by a_humean 4y ago
These blocks have other advantages such as having access to the outer scope. Yes those could be passed in as arugments to a function, but I think there are many cases where that extra overhead feels unreasonable given you have already gathered all of the values here for this purpose anyway.
- epidemian 4y agoYep. And they also keep single-use code local to the only place it's being used at the moment, which i think can help the readability and maintainability of said code :) Functions do have a place too of course; even single-use ones. Especially when you can give them a clear purpose and name.
- Pulcinella 4y agoA sort of reverse or corollary to DRY, “don’t be unintentionally repeatable.”
- Jtsummers 4y agoIt's more of an extension to "Don't use globals", but at a more fine-grained level. Code using this can still be DRY, if you understand DRY to be "don't repeat the same information excessively". Reusing the same variable name in multiple (similar or dissimilar) contexts is not a DRY violation in that sense if the names are more a coincidence than actual shared information. Most programmers have no problem, for instance, with seeing multiple loops declared like: for(int i = ...; ...; ...) { ... } `i` is "repeated", but there's no objection in having multiple declarations since each has a meaning dependent on its local context.