Condition check inside a function or before its call?

Posted by Ashwin on Stack Overflow See other posts from Stack Overflow or by Ashwin
Published on 2010-04-09T01:27:12Z Indexed on 2010/04/09 1:33 UTC
Read the original article Hit count: 260

Filed under:

Which of these 2 programming styles do you prefer? Why? Are there particular advantages to one over the other?

// Style 1
if (doBorder)
    doTheBorder();
if (doFrame)
    doTheFrame();
if (doDraw)
    doTheDraw();

void doTheBorder()
{
  // ...
}

void doTheFrame()
{
  // ...
}

void doTheDraw()
{
  // ...
}

// Style 2
doTheBorder();
doTheFrame();
doTheDraw();

void doTheBorder()
{
  if (!doBorder)
    return;
  // ...
}

void doTheFrame()
{
  if (!doFrame)
    return;
  // ...
}

void doTheDraw()
{
  if (!doDraw)
    return;
  // ...
}

© Stack Overflow or respective owner

Related posts about coding-style