切换语句:你需要最后一次休息吗? (主要是Javascript) [英] Switch statements: do you need the last break? (Javascript mainly)

查看:80
本文介绍了切换语句:你需要最后一次休息吗? (主要是Javascript)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 switch()语句时,在单独的之间添加 break; case:声明。但是最后呢?

When using a switch() statement, you add break; in between separate case: declarations. But what about the last one?

通常情况下,我只是将其关闭,但我想知道这是否有一些我不考虑的性能影响?

Normally I just leave it off, but I'm wondering if this has some performance implication I'm not thinking about?

我一直想知道这件事有一段时间,并没有看到它在Stack-O的其他地方问过,但对不起,如果我错过了。

I've been wondering about this for a while and don't see it asked elsewhere on Stack-O, but sorry if I missed it.

我主要问这个关于Javascript的问题,虽然我猜这个答案将适用于所有 switch()语句。

I'm mainly asking this question regarding Javascript, although I'm guessing the answer will apply to all switch() statements.

推荐答案

表现不应该是一个考虑因素。即使某些语言或实现具有可在微基准测量中衡量的性能差异,它也不会导致实际的性能问题。

Performance should not be a consideration at all. Even if some language or implementation has a performance difference that's measurable in a micro-benchmark, it would never cause a real-world performance issue.

我认为最重要的问题是一致性和可维护性。如果你不用最后的中断,然后另一个开发人员添加了另一个案例陈述而没有意识到你没有离开中断,行为会改变。这是第二个开发人员的错误,因为没有注意到 break 缺失,但是可以通过保持一致并且中断案例条款的$ c>。

I think the most important issue is consistency and maintainability. If you leave off the last break and then another developer adds another case statement without realizing you left off the break, the behavior would change. It's really the second developer's fault for not noticing the break is missing, but it could have been avoided by being consistent and having the break on all case clauses.

这篇关于切换语句:你需要最后一次休息吗? (主要是Javascript)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆