Monday, November 8, 2010

One Exit Per Function

Today's conversation begins with this article about multiple code exit points.

Narrator: What makes you think that spaghetti code is the problem? And if it's not, then why would a rule of one exit point per function help?

Think about it this way - when maintaining or debugging, you're looking how to reach a certain block of code. Early exits reverse that logic. Your brain has to stop and change direction. So that's an interruption.

On the other hand, nesting if's can get hairy. But your mind stays focused on what it takes to reach a block of code. No interruptions. It feels better looking at that code.

I use multiple exits when appropriate - usually a block of if/elseif returning a value.

Exception handling is great. Exceptions don't break the flow of the source code. Again, ignore execution efficiency. Focus on how the source reads to a human being.

No comments:

Post a Comment