3 ms·
As with anything else, a balance of these is most useful. If there is nothing to clean up, error checks and early exit. But only if it is one nested level deep
by bcl 14y ago
As with anything else, a balance of these is most useful.
If there is nothing to clean up, error checks and early exit. But only if it is one nested level deep. Any more and someone reading your code may miss it.
When there is a stack of things to be cleaned up use multiple goto targets at the end and fall through them when everything is ok.
Try to keep the nesting to a minimum. I used to indent my C with 2 spaces. Now that I use mainly use Python I have switched to 4 spaces in C and it helps keep things readable and reminds me not to get too deeply nested.
Mix and match as needed. But I think the most important thing is to keep the nesting to a minimum, that makes it much easier for those who come after you to maintain things. If you must have deep nesting or complicated logic at least make sure you do a good job of commenting what it is supposed to be doing.