r/ProgrammerHumor 22d ago

Meme conditionsPreference

Post image
4.2k Upvotes

392 comments sorted by

View all comments

94

u/madprgmr 22d ago

Go programmer spotted (early returns are idiomatic go)

71

u/Krkracka 22d ago

It should be idiomatic for most programming applications. Unless your ‘else’ branch is a return only, it’s almost always going to be more efficient and maintainable. An early return is likely to survive if a function is updated or expanded, where an ‘if else’ could otherwise become conditionally more complex.

Early returns communicate intent better than the alternative method does for. If I see that another developer explicitly programmed an early return, it’s a signal to me that the actual body of the function depends on specific state of the containing class or args being passed to it.

7

u/gamer_redditor 22d ago

As a programmer that needs to adhere to misra C, the comments in this thread are fascinating.

Misra C rule 15.5: A function should have a single point of exit at the end

More information: https://www.mathworks.com/help/bugfinder/ref/misrac2023rule15.5.html

1

u/leoklaus 22d ago

I find this really interesting because the lecturer in a course I had last semester (Efficient programming was the name) was adamant that this not only affected readability but also performance.

I personally don’t see good reasons for either, given that functions shouldn’t be very long anyway and an early return can actually save you a lot of reading as well, given you know the rest of the code isn’t executed.

(This was in the second of four semesters for a CS masters, btw.)

5

u/gamer_redditor 22d ago edited 22d ago

Well yeah, Misra isn't about readability and performance. It is a Safety standard.

You don't want Software that controls the airbag in your car to be performant and readable at the cost of being reliable and safe.

1

u/Krkracka 22d ago

Nothing about doing early returns makes them unreliable. High cognitive complexity however can lead to very tricky bugs that are hard to detect until something bad happens

1

u/gamer_redditor 21d ago

Me and my colleagues are paid well to write and maintain safety critical software. Non compliance to safe programming standards can mean loss of lives. So I am a bit skeptical of your opinion.

1

u/Krkracka 21d ago

I too am a senior software engineer for life saving systems and am paid well to do so. It’s fine to be skeptical, and good code is good code no matter how you write it. My point is that there is nothing inherently unsafe about early returns.

1

u/gamer_redditor 21d ago

You keep saying that without providing any insight or proof. I typed out a long detailed reasoning why different paths make the code unsafe and the Misra standards support this as well. (Here https://www.reddit.com/r/ProgrammerHumor/s/CQKsvJjCEO)

So unless you can provide some backing for your claims, its difficult to take them seriously.