Skip to main content
2 of 9
edited tags
rjzii
  • 11.3k
  • 8
  • 49
  • 72

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

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

Rachel
  • 24k
  • 16
  • 95
  • 160