top | item 45703367

(no title)

augustk | 4 months ago

Better stick to a single point of exit.

https://news.ycombinator.com/item?id=20311080

discuss

order

foofoo12|4 months ago

There is nothing wrong with early exit, but you have to be sensible.

Just like there's nothing wrong with the ternary operator if you are sensible. I've seen nested ?: abominations that would make Jesus give you a funny look.

SAI_Peregrinus|4 months ago

I disagree, I think returns should either be at the very start (checking inputs should be able to return before anything happens based on invalid inputs) or at the very end. Proceeding to do a bunch of stuff & then try to undo it because an input was invalid is error-prone.

augustk|4 months ago

If the input parameters are invalid there is an error in the program so it makes more sense to use the assert function. Why would you need to undo something due to the restriction of a single point of exit?

loeg|4 months ago

Well, maybe. Could also just use psrc directly instead of doing the manipulation in src.

1718627440|4 months ago

The discussion you linked to convinces me of the opposite.