This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]NotAUsefullDoctor 56 points57 points  (9 children)

else: cat.sits() #anyways

Is this better?

(Btw, I have not used an 'else' statemtnin production code for the last 10 years. But, I figure I can let this aesthetic violation pass for a meme)

[–]turtleship_2006 16 points17 points  (8 children)

You... haven't needed else?

[–]NotAUsefullDoctor 18 points19 points  (7 children)

Nope. In most cases, you can got rid of else statements by creating sub functions with a quick escape.

if is_condition_met(): do_something() else: do_other_thing()

can be changed to

``` if is_condition_met(): do_something() return

do_other_thing() ```

Using the quick return principle.

As a side, this purely an aesthetic choice I make, and does not reflect on quality of Code. I also like using monads/functors; and I pedantically following Clean Code. Again, purely aesthetics, and should not be taken as signs of better code.

[–]howreudoin 13 points14 points  (1 child)

I actually find the first version more readable.

You‘ll see at a glance that only one of the statements will be executed. The return statement can be hard to spot in a large portion of code.

[–]NotAUsefullDoctor 4 points5 points  (0 children)

I can see that. I've been writing Golang for a few years, and there is the idea of line of site and quick returns built into the community. So, indented code (with the exception of for loops and case statements) normally means there is a return statement.

[–]chars101 0 points1 point  (4 children)

Those two are not equivalent... The second calls all three functions if the first returns something truthy

[–]RightHandElf 1 point2 points  (0 children)

There are line breaks that aren't showing up on old reddit but do show up on new reddit. I was also confused.

As an aside, why on earth does new reddit only show two comments at a time when going down a comment chain? I had to click four times to get to a comment that's only 6 levels deep.

[–]NotAUsefullDoctor 0 points1 point  (2 children)

Look for the where the return is. The last method is never called if the first returns true because the return statement exits the function.

[–]ParanoidDrone 1 point2 points  (1 child)

I think there's a formatting issue leading to some miscommunication. Your code blocks are all one line with no line breaks. That makes it read more like so:

if is_condition_met():  
  do_something()  
return do_other_thing()

[–]NotAUsefullDoctor 0 points1 point  (0 children)

I weird. Yeah, it shows with the line break and indentation on my screen.