以下 2 个代码片段之间是否有任何功能差异

Is there any functional difference between the following 2 code snippets?

本文关键字:任何 功能 是否 之间 代码 片段 以下      更新时间:2023-10-16

以下 2 个代码片段之间有什么功能差异吗?

bool ColorClass::setTo(int inRed, int inGreen, int inBlue)   
{
  amountRed = inRed;                     
  amountGreen = inGreen;
  amountBlue = inBlue;
  return clipColor(amountRed, amountGreen, amountBlue);
}

bool ColorClass::setTo(int inRed, int inGreen, int inBlue)   
{
  amountRed = inRed;                     
  amountGreen = inGreen;
  amountBlue = inBlue;
  if (clipColor(amountRed, amountGreen, amountBlue))
  {
    return true;
  }
  else 
  {
    return false;
  }
}

上述代码调用的函数定义如下:

bool ColorClass::clipColor(int &checkRed, int &checkGreen, int &checkBlue) 
{
  int numClips = 0;     //numClips is used to counter number of clips made
  checkColorBounds(checkRed, numClips);
  checkColorBounds(checkGreen, numClips );
  checkColorBounds(checkBlue, numClips);
  return (numClips != 0);
}
void ColorClass::checkColorBounds(int &color, int &clipCounter)
{
  if(color > MAXCOLOR)
  {
    color = MAXCOLOR; 
    clipCounter++;
  }
  else if (color < MINCOLOR)
  {
    color = MINCOLOR;
    clipCounter ++;
  }
}

测试了两者并经历了两者,我似乎没有注意到任何功能上的差异。

我更喜欢第一个,因为它更简洁,更高效(避免if-else(

完全没有任何功能差异。然后使用第一个。