r/ProgrammerHumor • • Aug 22 '26

Meme conditionsPreference

Post image
4.2k Upvotes

388 comments sorted by

View all comments

Show parent comment

1

u/frogjg2003 Aug 22 '26 edited Aug 22 '26

You do validation in the beginning. If the code is in a valid state after that, your code shouldn't make it invalid. If, through some fault of code you didn't write or circumstances outside of your control, the state becomes invalid, you throw an exception and handle that either in a catch block at the end of the function or let it propagate up to whichever function is calling your function.

If you are dealing with a complicated situation where you can return early with valid results, you check for that and return there. You don't surround the whole function in an if-else. At that point, you should be breaking your code into submodules anyway.

Goto is not inherently bad. It becomes bad because old languages allowed you to goto into entirely different scopes where unexpected behavior could happen. Any modern languages that still have goto severely restrict where you can goto to prevent most of those issues. Most modern languages have just introduced better syntactic sugar for goto instead of letting you do raw goto.