Code Style - Do you prefer to return from a function early or just use an IF statement?

Posted by Rachel on Programmers See other posts from Programmers or by Rachel
Published on 2010-11-11T19:20:34Z Indexed on 2011/02/18 15:33 UTC
Read the original article Hit count: 382

Filed under:
|

I've often written this sort of function in both formats, and I was wondering if one format is preferred over another, and why.

public void SomeFunction(bool someCondition)
{
    if (someCondition)
    {
        // Do Something
    }
}

or

public void SomeFunction(bool someCondition)
{
    if (!someCondition)
        return;

    // Do Something
}

I usually code with the first one since that is the way my brain works while coding, although I think I prefer the 2nd one since it takes care of any error handling right away and I find it easier to read

© Programmers or respective owner

Related posts about holy-war

Related posts about style-guide