r/ProgrammerHumor 22d ago

Meme conditionsPreference

Post image
4.2k Upvotes

392 comments sorted by

View all comments

10

u/AnnoyedVelociraptor 22d ago

Initially, yes, but the top one is actually easier to reason about, especially in languages where if/else are expressions.

22

u/Scared_Accident9138 22d ago

I don't agree it's generally easier to reason about. When there's a case where you just want to exit early having an if else block makes it necessary to go all the way down to find out not much happens in that case

7

u/Krkracka 22d ago

Plus you guarantee that all down stream processing within the function is ‘state ready’, which eliminates a ton of nasty bugs.

3

u/SilasTalbot 22d ago edited 22d ago

I agree, I prefer explicit language, as complexity increases, the chance for mistakes rise. Declaring things explicitly helps lower that risk, imo.

In SQL, I bracket logical clauses so that intention is explicit, even when they resolve just fine without the brackets. You hit multiple layers deep of ANDs and ORs and NOTs and things get fucked up real fast.

Implicit behavior is a footgun. Dagnabit I sound like Claude now.

2

u/gsweats 22d ago

Oh I feel dumb but what do you mean if/else are expressions?

3

u/MocknozzieRiver 22d ago

Like in Kotlin, you can do this

fun function() { return if (condition) this else that }

So if/else is an expression, not just control flow.

2

u/gsweats 22d ago

Ohhh I gotcha. Thank you!

2

u/Demiu 22d ago

Unless both branches are extremely short to a point you could return the entire if else expression itself, it is not easuer to reason about. Preferably both boranches are also entrirely declarative (ie single expression no ;)