News for hackers. Stuff that matters.
Re:Push ifs up and fors down by imron on Wednesday November 15, 2023 @05:58PM (#38283755)
> Pushing "ifs" up has the downside that the preconditions and postconditions are no longer directly visible in the definition of a function

You're missing the second part of the author's argument:

"or it could push the task of precondition checking to its caller, and enforce via types"

The precondition is therefore still directly visible in the function definition - just as part of the type signature rather than in an if statement.

The "enforce preconditions via types" is a common pattern in Rust (the language used in the article), and unlike checking with if statements, it's a strict precondition that is checked at compile time rather than at runtime and you won't even be able to compile your program if you don't meet the pre-condition.

Re:Push ifs up and fors down by nerdponx on Wednesday November 15, 2023 @11:36PM (#38285824)
[delayed]
Re:Push ifs up and fors down by scns on Thursday November 16, 2023 @05:03AM (#38287652)
> The "enforce preconditions via types" is a common pattern in Rust

Called the Type State Pattern. Here are two links from my bookmarks:

https://cliffle.com/blog/rust-typestate/

https://yoric.github.io/post/rust-typestate/

Re:Push ifs up and fors down by funcDropShadow on Thursday November 16, 2023 @07:57AM (#38288989)
> "or it could push the task of precondition checking to its caller, and enforce via types"

Or the enforcement could be done via a design-by-contract discipline. Either using classical assertions, or something like reusable specifications in Clojure's spec library.