• 0 Posts
  • 64 Comments
Joined 1 year ago
cake
Cake day: July 7th, 2023

help-circle






  • I don’t think so. A half-measure using docstrings would likely take more processing power and require an ad-hoc implementation because comments are not broken down into ast components afaik. It would also be more costly in the long run if they decide to convert it into a proper syntax, as a result of docstrings not having a single standard way of being written.

    Python has introduced several syntactic changes for type annotations, this is not unreasonable.








  • Eager Eagle@lemmy.worldtoProgrammer Humor@lemmy.mlgot him
    link
    fedilink
    English
    arrow-up
    6
    ·
    edit-2
    2 months ago

    Any validation you can write with a few early returns you can write with an equivalent conditional/s followed by a single nested block under it, followed by a single return. The reader is free to leave the validation behind just the same.

    And that conditional indents your entire function one level - if you have more validation checks, that’s one level of indentation per check (or a complicated condition, depends whether you can validate it all in one place). It’s pretty much the case the other user illustrated above.

    Returns inside business logic past validation is where the problematic bugs of this class show up

    That much we agree. But again, this is not an early return issue, putting too much logic in a function is the issue. Rewriting it without early returns won’t make it much clearer. Creating other functions to handle different scenarios will.


  • Eager Eagle@lemmy.worldtoProgrammer Humor@lemmy.mlgot him
    link
    fedilink
    English
    arrow-up
    8
    arrow-down
    2
    ·
    2 months ago

    You can say any execution flow controls are like gotos - continue, break, exceptions, switch, even ifs are not much more than special cases of gotos.

    This is true regardless of the size of the function which shows that the size of the function isn’t the determinant

    Logical clarity does tend to worsen as the function grows. In general, it is easier to make sense of a shorter function than a longer one. I don’t know how you could even say otherwise.

    Early returns are still great for argument validation. The alternative means letting the function execute to the end when it shouldn’t, just guarded by if conditions - and these conditions any reader would have to keep in mind.

    When a reader comes across an early return, that’s a state they can free from their reader memory, as any code below that would be unreachable if that condition was met.


  • Eager Eagle@lemmy.worldtoProgrammer Humor@lemmy.mlgot him
    link
    fedilink
    English
    arrow-up
    7
    arrow-down
    5
    ·
    2 months ago

    I hate it when some blame early returns for the lack of maintainability.

    Early returns are a great practice when doing argument validation and some precondition checks. They also avoid nested blocks that worsen readability.

    What’s being described there is a function that tries to do too much and should be broken down. That’s the problem, not early returns.