As a reaction to this article at Peter Ritchie’s MVP Blog, I thought I’d give my two cents on having a single exit point for methods. Here we go…

When writing code, I always create only one exit point except for the occasional throwing of an exception. Most important for me is to always know where your code (really) stops. In case of long and complex methods there’s a chance you’re going to miss a return statement when scanning through a method. That way you could end up studying code not even applicable to the situation, while missing the real point. I do think there is no real reason to hold on to creating methods with a single exit point other than personal favoritism. This feels the same like using curly brackets with an if-statement that is followed by only one statement. I always use curly brackets, but there are lots of developers who don’t.

To answer the questions at the end of Peter’s post: Yes, I do generally follow the Single Point of Exit From Methods Principle, and I do ignore it for exceptions.

BTW: the SESE version of the first method in Peter’s post could be a little less (cyclomatic) complex:

public static int CountCommas(string text)
{
    int result = 0;

    if (!string.IsNullOrEmpty(text))
    {
        int index = text.IndexOf(‘,’, 0);
        while (index > 0)
        {
            result++;
            index = text.IndexOf(‘,’, index);
        }
    }

    return result;
}