The obvious thing I should've learned 5 years ago

Programming, for all ages and all languages.
Synon
Member
Member
Posts: 169
Joined: Sun Sep 06, 2009 3:54 am
Location: Brighton, United Kingdom

Re: The obvious thing I should've learned 5 years ago

Post by Synon »

earlz wrote:
JamesM wrote:Personally I use "early-exit" operators as much as possible in order to reduce the amount of nesting needed and therefore make code easier to read. That means break, continue, return.

They don't impact performance and it's blatantly obvious what the intention is. If it improves my code's readability, it's worth it. YMMV.
Yup, I believe Linus or some famous coder put it something like "Using break and continue is bad cause it's basically a disguised goto. But if you have over 3 levels of indentation, your screwed anyway"
It wasn't Linus; it was the guy who wrote the kernel coding style file (linux-XX.XX[.XX[-ABC]]/Documentation/CodingStyle). Here's an online version: http://lxr.linux.no/#linux+v2.6.33/Docu ... odingStyle
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: The obvious thing I should've learned 5 years ago

Post by Solar »

"Early return" from a function is different. It is well-defined and completely obvious what is being returned from, and where it returns to.

My personal litmus test is how an NSD of the code would look. Try it yourself; you immediately realize how disruptive "break" and "goto" are to the control flow.
Every good solution is obvious once you've found it.
Post Reply