Good coding style ?

Hi,

Am I too old to code ? Look at this link : http://confluence.jetbrains.com/display/ReSharper/Invert+'if'+to+reduce+nesting

When I was a developer 'student' (way back when), we were thaught that every proc / method / piece of code should have only one entry point and one exit point.  
However, looking at the example given, multiple exit points should be a norm ?!?

I feel it's rather easy to miss a return somewhere in the middle of a routine and could lead to undesired results.

Any one care to comment ?

Alex

4 comments
Comment actions Permalink

That's an old school rule related to C programming that required a function to have one exit point to clean up the stack correctly.

It isn't a development pattern that needs to be followed in todays modern languages that can handle multiple exit points.

Here is a good read on the topic.

http://programmers.stackexchange.com/questions/118703/where-did-the-notion-of-one-return-only-come-from

0
Comment actions Permalink

single entry/exit is by far the better design approach. this is one of the resharper checks that you should configure as ignore. and i wish there was a resharper to flag multiple exits as error/warnings etc.

0
Comment actions Permalink

This is like the spaces vs tabs argument - if you ask five different developers, you'll get seven different opinions.

Older developers tend to prefer a single exit point, because that's how they were taught. Almost all of the reasons for teaching it have gone away in modern languages, and it almost certainly won't have been taught to younger developers .

As mentioned on the StackExchange thread, a 200-line function with multiple exit points might be difficult to follow, but replacing it with a 250-line function with a single exit point won't make it any better.

The only real reason for advocating a single exit point in a modern language is that functions with multiple exit points are often too complex, and should be broken down into smaller functions. But this should be treated more as guidance than as a iron-clad rule that should never be broken.

0
Comment actions Permalink

"good coding style" itself is an oppion based question, and there is no correct answer. People who say "this style is best" are just trolling for replies of "no it isn't". What works for you is the correct answer.

Multiple exit points in a function is valid code in most languages, but the single exit function was a requirement of older C languages and some developers continue to follow it as a rule for today. That's fine and I don't have a problem with that.

I use to follow the single exit pattern for many years, but in the last couple of years I've switched to reduced indentation as a higher priority. Multiple exit points allows code to better follow the single purpose of a function, and exit when it can no longer continue with that purpose.

Our brains get hardwired to read source code a certain way. Multiple exit points often requires inverted logic with statements like "if(!x && !y) { return; }". These inverted rules can be difficult for programmers to read when they are not use to it. It conflicts with another popular coding pattern "always eval if statements as true". So programmers except to read things like "if(x && y) { return; }".

When I first started reducing indentations (branching of code) it was these inverted rules that were hardest to get use to. Now, it's not a problem for me.

It's strange how our brains work sometimes.

0

Please sign in to leave a comment.