Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
Schroedinger's results. You need to measure the exam to know whether you failed.
-
@Marethyun
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:
https://medium.com/better-programmi... -
@bittersweet
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. -
@SortOfTested
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. -
@bittersweet
Given how many languages have introduced pattern matching and stream abstractions, I'd actually probably say that description is accurate :) -
@PonySlaystation in two alternative universes you passed and failed lol. The many world's test.
Related Rants
Failed!
joke/meme
epicfail