5 ms·
Functions do the same thing, but are more reaadable due to explicit naming. It can be great though as an intermediate step to extracting functions.
by xiphias2 4y ago
Functions do the same thing, but are more reaadable due to explicit naming.
It can be great though as an intermediate step to extracting functions.
- a_humean 4y agoThese 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.
- wongarsu 4y agoFunctions fulfill the same function, but they move the code to a different place. Sometimes that's desirable for readability, sometimes it's more readable to keep things inline. It's nice to have both options
- arp242 4y agoI like this for some things where the logic is somewhat large-ish, but also strongly coupled. A good example is setting up state for integration tests; I'm not going to re-use any functions I create for that. It's not bad to use functions, I just find it more convenient to keep it all in one function, but split out a little bit in these faux-subfunctions. I don't use it often, but when I do, I find it convenient.
- jerf 4y ago"A good example is setting up state for integration tests; I'm not going to re-use any functions I create for that." And if it turns out I'm wrong about that, the braces provide a very nice, guaranteed cutting point in the future that anyone can understand without having to load all the context of the function. This isn't the sort of thing I want splattered all over a code base, but it is a nice niche tool.