Do all the things like ++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatarSign Up
Marethyun372116dLooks like a
"if (score <= 85)" and if "(score >= 85)" thing
Schroedinger's results. You need to measure the exam to know whether you failed.
gronostaj1921116dFlipping boolean expressions is hard.
volttide465116dSometimes we need to learn basic math 😂
Programming logic becomes a lot easier once you stick to the following rules of thumb:
1. Give a method a single responsibility.
2. Because of 1, nesting depth should be limited to a single level.
3. Avoid using the keywords "else"/"else if". No need to prohibit it in your code, just check if you can avoid it.
4. Use failure conditions with early returns instead.
This is a pretty solid article about it:
Didn't get much farther than guard expressions. Most of what he suggests works best in a functional approach. The imperative if/if/if/... style checking is leaning towards type guarding, which is type refinement that results in implicit type awareness of types based on compositional checking. He has almost arrived at the concept of discriminated unions and list comprehensions/pattern matching, but not quite. It's really boilerplatey without structural induction support and union types.
A more stringent/reusable/flexible approach in imperative structures would be church encoding (https://en.wikipedia.org/wiki/...).
Generally speaking, multiple return statements is the code smell for incorrect paradigm here.
I agree that it is a simulated guard pattern.
I disagree that it is a code smell to have multiple returns... unless you agree with me that any language that doesn't offer pattern matching / guards is super smelly by default 😛
For any language without pattern matching / guards, having a few "ifs" with early returns followed by the happy path code feels a lot better than having nested ifs and stacks of if/else/elseifs.
The clearest you can get with old-fashioned OOP languages is a "let's check if everything is in order, before we go on with the good stuff" pattern, with early returns.
But the whole syntax of if is a bit iffy, I do prefer Rust's pattern matching syntax.