Fle*_*ore 11
如果您无法在一个屏幕上阅读您的算法,那么您有99.9%的可能需要重构代码以提高可读性.
更改
if ($isHappening) {
// ... millions of lines of code
} else {
// .. another million lines of code
}
Run Code Online (Sandbox Code Playgroud)
成
if ($isHappening) {
happen();
} else {
didntHappen();
}
function happen() {
// millions of lines of code
}
function didntHappen() {
// another million lines of code
}
Run Code Online (Sandbox Code Playgroud)
那里真的没有魔法锤.让它们易于管理的最好方法是将嵌套的ifs分解为它们自己的函数,以使它们更具可读性.
另外,不要忘记array_filter.这可以使您不必编写for循环来过滤掉项目.
此外,您可以使用guard语句来消除嵌套.你基本上反转你的if而return不是(将条件分解为函数的另一个原因).
| 归档时间: |
|
| 查看次数: |
1675 次 |
| 最近记录: |