为什么在构造标准中使用前缀增量被认为比后缀增量更好

发布于 2025-01-01 00:53:27 字数 670 浏览 2 评论 0 原文

我最近为 Checkstyle 插件rel="noreferrer">Eclipse 个人认为非常棒。但它给我的警告之一有点晦涩难懂。确切的警告是“不允许使用 ++”。它是关于某些行中的后缀 ++ ,比如

for(int i = 0; i < SOMETHING; i++)

Ok,我知道 foreach 是更好的迭代结构,但它不能在任何地方应用,有时是旧的-school ++ 是唯一的选择。

当我将行更改为警告时,

for(int i = 0; i < SOMETHING; ++i)

警告就会消失。我知道 i++++i 之间的区别,到目前为止,我认为它们在标准 for 结构中是可以互换的。但 Checkstyle 认为 i++ 有害(或容易出错)。

问题:为什么在 for 结构中前缀增量比后缀增量更好?或者……Checkstyle 是不是错了?

I recently installed Checkstyle plugin for Eclipse and personally think that it is awesome. But one of the warnings it gives me is a bit obscure. The exact warning is "Using ++ is not allowed". It is about postfix ++ in some row like

for(int i = 0; i < SOMETHING; i++)

Ok, I 'm aware that foreach is the better construction for iteration, but it can't be applied everywhere, sometimes old-school ++ is the only alternative.

When I change the row to

for(int i = 0; i < SOMETHING; ++i)

the warning disappears. I know the difference between i++ and ++i and to this point of my life I considered them interchangeable in standard for construction. But Checkstyle considers i++ harmful (or error prone).

Question: Why prefix incrementation is better than postfix incrementation in for constructions? Or... is it Checkstyle wrong about that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

于我来说 2025-01-08 00:53:27

这是一个愚蠢的规则,恕我直言。其描述位于此处

非法令牌

检查非法令牌。

Rational:某些语言特性常常导致难以维护
代码或对于新手开发人员来说并不明显。其他功能可能是
在某些框架中不鼓励使用,例如没有本机方法
在 EJB 组件中。

默认情况下,它禁止后缀递增、后缀递减和切换。您可以安全地禁用该规则,或以其他方式配置它。

我的观点是,你的规则是标准的Java习惯用法,用++i替换i++不会有其他效果,让新手问自己为什么不使用标准习惯用法。

It's a stupid rule, IMHO. Its description is here

IllegalToken

Checks for illegal tokens.

Rational: Certain language features often lead to hard to maintain
code or are non-obvious to novice developers. Other features may be
discouraged in certain frameworks, such as not having native methods
in EJB components.

By default, it forbids postfix increments, postfix decrements, and switches. You can safely disable the rule, or configure it differently.

My opinion is that your rule is a standard Java idiom, and that replacing i++ with ++i will have no other effect that making newbies ask themselves why the standard idiom isn't used.

软糖 2025-01-08 00:53:27

仅当在需要修改之前的旧值的表达式中使用时,后缀增量才有意义。在该值被丢弃的 void 上下文中(如在 for 循环中),保存旧值没有任何意义。

换句话说:

// makes sense because you need the old value to subscript the array
stack[top++] = x;
// in a void context, the old value is discarded
top++;

特别是在 C++ 中,这两个运算符都可以重载,并且后缀运算符的实现可能效率低下,因为需要返回旧值 - 它通常涉及复制旧对象以符合原始语义后缀运算符。

对于原始类型,任何像样的编译器都会为这两种情况生成相同的代码,但从语言的语义角度来看,第二种更好。

Postfix incrementation makes sense only when used in an expression where you need the old value prior to the modification. In void contexts where that value is discarded (as is in your for loop), saving the old value makes no sense.

In other words:

// makes sense because you need the old value to subscript the array
stack[top++] = x;
// in a void context, the old value is discarded
top++;

In C++ in particular, both of these operators can be overloaded, and the implementation of the postfix one can be inefficient because of the requirement to return the old value - it usually involves copying the old object to comply with the original semantics of the postfix operator.

With primitive types, any decent compiler will produce identical code for both of the cases, but the second one is better from the semantic standpoint of the language.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文