6

The masculine urge to skew a scredriver in the lungs of the very funny colleague who place several random return statements inside their function

Comments
  • 1
    I am supposed to "bring the code up to corrent standards".
    RN I'm staring at this instrument of torture with 3 different return statements (but 4 branches, so one branch DOESN'T RETURN ANYTHING AT ALL) and at the bottom of the function there's a logger which of course is almost never called since for 3 branches out of 4 it returns before even reaching it.

    The return themselves are sometimes a promise, sometimes a raw value, sometimes undefined.

    I'll start linking this shit everywhere from now on, this is getting unnerving
    https://en.wikipedia.org/wiki/...
  • 0
    Is it ok to write two return inside of if-else statement?
  • 1
    @miladiashe It's not, but it's still better than having just one return inside an if-else statement
  • 1
    @IHateForALiving thanks to you, now my code will be better
  • 0
    @miladiashe your Git says your code is already good. Wanna switch places with whoever-is-responsible-for-this-mess-I'm-fixing?
  • 2
    @miladiashe What's pretty common is the idiom of doing any sort of input or condition checking at the beginning of a function and return an error code if something isn't right. No big if-else branch, just early return.

    The rest of the function can then be written under the assumption that all that stuff has already been ruled out. So that would be two returns for that idiom.
  • 0
    @IHateForALiving wow you fixing that level of mess?
  • 1
    @miladiashe I _should_ be doing it.

    RN I'm fucking around on devrant because I hate it tho
  • 0
    @miladiashe Btw., if you start introducing artificial flag variables in loops so that you can prematurely exit the loop without using a break, your code will be so clean that you'll see why people hated Pascal. ^^
  • 0
    @Fast-Nop what is difference of break and while flag=off + flag=on
  • 2
    @miladiashe The difference is that you don't have a break. It's like putting so much lipstick on a pig that you can't see the pig anymore.
  • 1
    The first comment smells untyped..

    I don't see an issue with sensibly used multiple returns in statically typed languages. And even abused they are better than »goto exit; //but not really as there's stuff happening after the jumpmark«.
  • 1
    @saucyatom
    Yes, that is the real issue: Not using static analysis at all.
    Doesn't have to be a compiler. Even static analyzers for PHP find a lot of the common bugs.
Add Comment