Lig*_*uzz 8 c++ code-formatting
这段代码看起来很脏,我无法弄清楚如何格式化它,以便我可以阅读,理解它,同时看起来干净.
if(center==2 && ((((y-height/2)==j) && ((x+width/2)==i)) || (((y+height/2)==j) && ((x+width/2)==i))))
regenerateDot(i+1, j, dots);
Run Code Online (Sandbox Code Playgroud)
有什么建议?
cdm*_*kay 17
我会将布尔表达式分解为以可读性命名的变量.就像是:
bool isCentered = center == 2;
bool inLowerRegion = (y-height/2) == j && (x+width/2) == i;
bool inUpperRegion = (y+height/2) == j && (x+width/2) == i;
bool inEitherRegion = inLowerRegion || inUpperRegion;
if (isCentered && inEitherRegion) {
regenerateDot(i+1, j, dots);
}
Run Code Online (Sandbox Code Playgroud)
考虑重构.您可以将子表达式放入它们自己的函数中,从而命名它们的用途.
例如:
if (IsCentered(center) && IsInsideLower(y, j, i) && IsInsideUpper(y, j, i))
regenerateDot(i + 1, j, dots);
Run Code Online (Sandbox Code Playgroud)
请注意,在上面的示例中,函数名称可能是伪造的(我还没有真正试图理解代码的用途是什么),但是你应该得到图片.
| 归档时间: |
|
| 查看次数: |
1113 次 |
| 最近记录: |