> If there’s an if condition inside a function, consider if it could be moved to the caller instead This idle conjecture is too rife with counterexamples. - If the function is called from 37 places, should they all repeat the if statement? - What if the function is getaddrinfo , or EnterCriticalSection ; do we push an if out to the users of the API? I think that we can only think about this transformation for interna…
Push Ifs Up and Fors Down
171–180 of 209 posts
Re: Push Ifs Up and Fors Down
#172These are extremely opinionated, and shouldn't be treated as a rule of thumb. As somebody else said, there isn't a rule of thumb here at all, but if I was to make up one, I would probably tell you the opposite: - You have to push ifs down, because of DRY. - If performance allows, you should consider pushing fors up, because then you have the power of using filter/map/reduce and function compositions to choose what ac…
Pushing ifs down usually prevents vectorization and the cases article mentions are those non-dry where a similar branch has to be multiplied on a ton of functions down in the stack, often because the type is internally tagged.
Re: Push Ifs Up and Fors Down
#173In my field (server programming) readability trumps them all. Nested g() and h() can be much better if they are even just 1% easier to understand. No one cares about a few extra CPU cycles, because we don't write system or database code.
I will admit it is probably just some internal biasing from some unknown origin, but I always tend to think of server programming as a part of systems programming. To your mind, what keeps it out of the systems programming umbrella? I am not super certain about how programmers in your area break up the constituent parts of a server’s code, so maybe I’m thinking more along the lines of the ‘framework’/libraries your c…
Maybe I'm using the terminology wrong, and it's actually Applications Programming, but it's easy to confuse with mobile/desktop applications, where RAM does matter. In servers we pay for RAM/CPUs ourselves.
Re: Push Ifs Up and Fors Down
#174Within a function, I'm a fan of early bail out.
While this goes against the usual advice of having the positive branch first, if the positive branch is sufficiently large you avoid having most of the function indented.
Re: Push Ifs Up and Fors Down
#175Earlier quoted context omitted.
One possibility is a file.py that is called by your framework. The interface could be something like def doth_match(*args): return True # the predicate def doeth_thou(*args): # processing things return {} # status object for example The framework loops and checks the first function; if true, then execute the second function. And then break or continue for other rule files (or objects). There could be multiple files r…
I think the parent's argument is that wherever in your framework you're calling `doth_match` and then `doeth_thou`, you have a single function that's both deciding and acting. There has to be a function in your program that's responsible for doing both.
Asking for absolutes is something journeymen developers need to grow out of.
The principle of the excluded middle applies to Boolean logic and bits of set theory and belongs basically nowhere else in software development. But it’s a one trick pony that many like to ride into the ground.
Re: Push Ifs Up and Fors Down
#176Earlier quoted context omitted.
> Because then you are calling middleware_caching_auth_broker() from 37 places No you aren't. You aren't really calling it from anywhere. The framework you're using, which you aren't writing, is calling the registered middleware. The topic here is complexity for the code structure because it's called from 37 different places. A registered middleware doesn't run into that issue because it doesn't get called anywhere t…
That sounds like the programming equivalent to thinking that food just comes from the grocery store.
Re: Push Ifs Up and Fors Down
#177Related: Within a function, I'm a fan of early bail out. While this goes against the usual advice of having the positive branch first, if the positive branch is sufficiently large you avoid having most of the function indented.
This is advice I've never seen or received. It's always been the latter, exit early, etc. Languages like Swift even encode this into a feature, a la if guards.
Re: Push Ifs Up and Fors Down
#178Earlier quoted context omitted.
You can't shove every single assumption into the type system...
You can express a lot of concepts just through types in languages with richer type systems.
You just can't enforce those assumptions.
Re: Push Ifs Up and Fors Down
#179These are extremely opinionated, and shouldn't be treated as a rule of thumb. As somebody else said, there isn't a rule of thumb here at all, but if I was to make up one, I would probably tell you the opposite: - You have to push ifs down, because of DRY. - If performance allows, you should consider pushing fors up, because then you have the power of using filter/map/reduce and function compositions to choose what ac…
I feel like you either flipped the naming or the reasons you cite don't support the conclusion. Pushing ifs down usually prevents vectorization and the cases article mentions are those non-dry where a similar branch has to be multiplied on a ton of functions down in the stack, often because the type is internally tagged.
Re: Push Ifs Up and Fors Down
#180Earlier quoted context omitted.
Why? Does it interpret the code?
Kind of, it’s a declarative framework like React & co. Under the hood it maps to either UIKit components or GPU (Metal) rendering. And view identity is very important for change detection. AFAICT, putting a branch in a ForEach invalidates all elements in that ForEach whenever one branch changes, because its whole identity changes.