Coding style 函数内部或调用前的条件检查?

Coding style 函数内部或调用前的条件检查?,coding-style,Coding Style,您喜欢这两种编程风格中的哪一种?为什么?一个比另一个有特别的优势吗 // Style 1 if (doBorder) doTheBorder(); if (doFrame) doTheFrame(); if (doDraw) doTheDraw(); void doTheBorder() { // ... } void doTheFrame() { // ... } void doTheDraw() { // ... } // Style 2 doTh

您喜欢这两种编程风格中的哪一种?为什么?一个比另一个有特别的优势吗

// 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;
  // ...
}

第一个。第二个似乎是。。。缺乏信心。如果您甚至不知道是否要完成边界,为什么要调用
doTheBorder()
?在我看来,您应该断言边界确实需要执行,然后满怀信心地调用
doTheBorder()

…另外,从更技术的角度来看:如果
doTheBorder()
在一个封闭的API中,那么在遥远的将来,开发人员可能会调用它,如果使用第二种样式,他们可能会想,尽管调用了
doTheBorder()
,但为什么边界没有完成。当然,有时某些情况或限制可能决定使用第二种风格,但我会尽可能避免使用