发布于 2026-01-06 10 阅读
0

我始终不太相信 switch 语句比 `if else if else if else if else if else` 语句更简洁。

我始终不太相信 switch 语句比 `if else if else if else if else if else` 语句更简洁。

我大致知道,在串联多个else if语句时,使用 switch 语句通常被认为是个好主意。我并不反对这次重构,但我也从未觉得它特别重要。

与 switch 语句相比,我发现无论使用何种语言,`and` 的行为和语法ifelse相当一致,因此if更容易使用,即使链式使用,似乎也并不比 switch 语句逊色多少

我完全同意应该重构代码,去掉if...链elseelse但我还没有发现 switch 语句能真正显著地提高代码质量。

有什么想法?

文章来源:https://dev.to/ben/i-ve-never-become-overly-convinced-that-switch-statements-are-that-much-cleaner-than-if-else-if-else-if-else-if-else-81l