News for hackers. Stuff that matters.
Re:Push Ifs Up and Fors Down by andyg_blog on Saturday May 17, 2025 @10:40AM (#44014665)
A more general rule is to push ifs close to the source of input: https://gieseanw.wordpress.com/2024/06/24/dont-push-ifs-up-p...

It's really about finding the entry points into your program from the outside (including data you fetch from another service), and then massaging in such a way that you make as many guarantees as possible (preferably encoded into your types) by the time it reaches any core logic, especially the resource heavy parts.

Re:Push Ifs Up and Fors Down by dataflow on Saturday May 17, 2025 @11:35AM (#44015047)
Doesn't this obfuscate what assumptions you can make when trying to understand the core logic? You prefer to examine all the call chains everywhere?
Re:Push Ifs Up and Fors Down by fmbb on Saturday May 17, 2025 @12:13PM (#44015283)
The ”core logic” of a program is what output it yields for a given input.

If you find a bug, you find it because you discover that a given input does not lead to the expected output.

You have to find all those ifs in your code because one of them is wrong (probably in combination with a couple of others).

If you push all your conditionals up as close to the input as possible, your hunt will be shorter, and fixing will be easier.

Re:Push Ifs Up and Fors Down by setr on Saturday May 17, 2025 @12:10PM (#44015266)
If you’ve massaged and normalized the data at entry, then the assumptions at core logic should be well defined — it’s whatever the rules of the normalized output are.

You don’t need to know all of the call chains because you’ve established a “narrow waist” where ideally all things have been made clear, and errors have been handled or scoped. So you only need to know the call chain from entry point to narrow waist, and separately narrow waist till end.

Re:Push Ifs Up and Fors Down by avianlyric on Saturday May 17, 2025 @11:51AM (#44015146)
This is why we invented type systems. No need to examine call chains, just examine input types. The types will not only tell you what assumptions you can make, but the compiler will even tell you if you make an invalid assumption!
Re:Push Ifs Up and Fors Down by dataflow on Saturday May 17, 2025 @12:01PM (#44015211)
You can't shove every single assumption into the type system...
Re:Push Ifs Up and Fors Down by Nevermark on Saturday May 17, 2025 @12:10PM (#44015267)
Assumptions are like di**os. You can shove them anywhere if you are brave enough. [0]

[0] Source: Abraham Lincoln. (At least I have heard he said that.)

Re:Push Ifs Up and Fors Down by badc0ffee on Saturday May 17, 2025 @12:22PM (#44015339)
Leave the dingos alone
Re:Push Ifs Up and Fors Down by kevindamm on Saturday May 17, 2025 @02:17PM (#44016017)

  > with admirable tunnel vision, bullheadedness and mission for maximally general algebraic and arbitrary constraint type systems.
I believe they're called keyhole optimizations, greedy search, and "the customer is always right..."
Re:Push Ifs Up and Fors Down by layer8 on Saturday May 17, 2025 @12:18PM (#44015312)
True, but there are still documented interface contracts you can program against. The compiler won’t catch violations of the non-type parts, but the requirements are still well-defined with a proper interface contract. It is a trade-off, but so is repeating the same case distinction in multiple parts of the program, or having to pass around the context needed to make the case distinction.
Re:Push Ifs Up and Fors Down by treyd on Saturday May 17, 2025 @12:20PM (#44015324)
You can express a lot of concepts just through types in languages with richer type systems.
Re:Push Ifs Up and Fors Down by shiandow on Sunday May 18, 2025 @05:17AM (#44020063)
Even without a rich type system you can express a lot of things just through naming.

You just can't enforce those assumptions.

Re:Push Ifs Up and Fors Down by eyelidlessness on Sunday May 18, 2025 @04:49PM (#44024226)
You can enforce them (statically) by other means if you’re determined enough, eg by using lint rules which enforce type-like semantics which the type system itself doesn’t express.
Re:Push Ifs Up and Fors Down by treyd on Sunday May 18, 2025 @05:40PM (#44024504)
This does rely on the language having a sophisticated-enough type system to be able to extract enough type information for the rules to work in the first place.
Re:Push Ifs Up and Fors Down by knome on Saturday May 17, 2025 @12:21PM (#44015330)
You can and should put as many as you can there

https://lexi-lambda.github.io/blog/2019/11/05/parse-don-t-va...

If instead of validating that someone has sent you a phone number in one spot and then passing along a string, you can as easily have a function construct an UncheckedPhoneNumber. You can choose to only construct VerifiedPhoneNumbers if the user has gone through a code check. Both would allow you to pluck a PhoneNumber out of them for where you need to have generic calling code.

You can use this sort of pattern to encode anything into the type system. Takes a little more upfront typing than all of those being strings, but your program will be sure of what it actually has at every point. It's pretty nice.

Re:Push Ifs Up and Fors Down by alfiedotwtf on Sunday May 18, 2025 @03:25AM (#44019624)
Yep! I have seen so much pushed into a type system that in the end there was hardly any code needed to do validation or scaffolding… to the point where it felt magical
Re:Push Ifs Up and Fors Down by sn9 on Saturday May 17, 2025 @02:31PM (#44016098)
You can at least shove them into the constructors.
Re:Push Ifs Up and Fors Down by furyofantares on Saturday May 17, 2025 @12:20PM (#44015323)
The idea and examples are that the type system takes care of it. The rule of thumb is worded overly generally, it's more just about stuff like null checks if you have non-nullable types available.
Re:Push Ifs Up and Fors Down by geysersam on Saturday May 17, 2025 @07:55PM (#44017899)
No I don't think so because if you make your assumptions early then the same assumptions exist in the entire program and that makes them easy to reason about
Re:Push Ifs Up and Fors Down by sharno on Tuesday May 20, 2025 @01:58PM (#44044167)
That's almost the same thing as parse don't validate: https://lexi-lambda.github.io/blog/2019/11/05/parse-don-t-va...