3 ms·
I use both if-return and if-goto. In practice you need both, depending on circumstance. In much of my recent code I've managed to use a lot of if-return checks
by dwc 14y ago
I use both if-return and if-goto. In practice you need both, depending on circumstance. In much of my recent code I've managed to use a lot of if-return checks at the top of functions. Another nice thing about a bunch of if-returns at the beginning is it makes a nice checklist of things that will cause the function to fail, all right up front.
- huhtenberg 14y ago> a nice checklist of things that will cause the function to fail Should these be assert()s? ;)
- dwc 14y agoSometimes, yes.
- DeepDuh 14y agoNot sure if serious, but assert only makes sense in cases where you want to switch off the tests at some point right?
- huhtenberg 14y agoAsserts are for enforcing invariants in the code. If you replace an assert with a conditional that allows the execution to continue, you are concealing the problem and making it that much harder to track. Practically speaking, your function should not be failing because your other code passed it invalid parameters. That's a bug in the former. That's why parameter validity should be asserted.
- DeepDuh 14y agoI see. I usually never let execution continue and log + exit instead, but you're right, an assert is easier in that situation.