Re:Coding style and undoing indentation hell
by dwc on Friday June 01, 2012 @01:13PM
(#4054490)
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.
[ Parent
| on HN ]
Re:Coding style and undoing indentation hell
by huhtenberg on Friday June 01, 2012 @01:34PM
(#4054581)
> a nice checklist of things that will cause the function to fail
Should these be assert()s? ;)
Re:Coding style and undoing indentation hell
by DeepDuh on Saturday June 02, 2012 @11:04AM
(#4058207)
Not sure if serious, but assert only makes sense in cases where you want to switch off the tests at some point right?
Re:Coding style and undoing indentation hell
by huhtenberg on Saturday June 02, 2012 @11:57AM
(#4058341)
Asserts 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.