Sorry, I should have meant "handle the error case first and exit as soon as possible", even if this "as soon as possible" will only occur in the middle of the function. Usually I don't have problems with multiple exit points in a function. Those are particularly useful, for example, when implementing a syntax parser from a LL(1) grammar. Another situation may happen, for example, if you are in the middle of a very nested loop and would like to exit them all. Refactoring the loop into its own function and using a return
is often one of the clearest ways to quit all loops at once, specially in comparison with the alternative which is to maintain quite a number of done
variables guarding the loop exits as in
for (int i = 0; i < n && !done; i++)
By the way, another sensible alternative for this case is also to use a goto
statement, this being one of the few justified cases for using it. I know this doesn't have anything to do with your question either, but I am just keeping up the discussion :)
Interested in Machine Learning in .NET? Check the Accord.NET Framework. See also Haar-feature Object Detection (With The Viola-Jones Framework) in C#