发表文章

[C#] 将变量声明移动到第一个使用位置 Move variable declarations to first use locations[jemalloc]

jasone 2017-10-9 41

采用 C99's 支持交错变量声明与 non-declarative 语句, 并重构代码的相关代码清晰, 使声明发生在第一次使用。 例如, 许多代码块都是语句的线性序列, 其中的一部分只分配给新声明的变量, 而这种代码可以更清晰、更紧凑地表示为交错声明的辅助。

此转换包括移动迭代计数变量声明, 例如

int i;

for0; i < x; i++) {
        /* ... */
}

通常可以成为

for (int0; i < x; i++) {
        /* ... */
}
原文:

Adopt C99's support for interleaving variable declarations with non-declarative statements, and refactor the code where relevant to code clarity so that declarations occur at first use. For example, many blocks of code are linear sequences of statements, only some of which assign to newly declared variables, and such code can be more clearly and compactly represented with the aid of interleaved declarations.

This conversion includes moving iteration count variable declarations, e.g.

int i;

for (i = 0; i < x; i++) {
        /* ... */
}

can usually become

for (int i = 0; i < x; i++) {
        /* ... */
}
相关推荐
最新评论 (3)
jasone 2017-10-9
1

作为此更改的一部分, 请移除仅用于创建新块作用域的大括号。

原文:

As part of this change, remove braces that exist solely for the purpose of creating new block scopes.

jasone 2017-10-9
2

我开始转换代码以利用混合声明, 并得出结论, 这是我们更好地做的事情, 因为我们维护/重构代码为其他主要目的, 有两个原因:

  • 获得重构权需要的不仅仅是略读代码, 这会增加整个扫描过程中的回归风险。
  • 代码的数量将会改变是不合理的。
原文:

I started converting code to take advantage of mixed declarations, and came to the conclusion that this is something we're better off doing incrementally as we maintain/refactor code for other primary purposes, for two reasons:

  • Getting the refactor right requires more than just skimming the code, which increases risk of regressions during a full sweep.
  • The sheer volume of code that would change is unreasonable.
jasone 2017-10-9
3

顺便提一下, 我终于想起了为什么这个限制持续了这么久: MSVC + +。 此限制在至少 Visual Studio 2015 中不再存在 (在打开此问题之前我已检查过), 但这肯定是前几年的问题, @glandium在添加停止痛苦之前修复了多个生成回归 -Werror=declaration-after-statement

原文:

Incidentally, I finally remembered why this restriction persisted for so long: MSVC++. This restriction no longer exists in at least Visual Studio 2015 (I checked prior to opening this issue), but it was certainly an issue in previous years, and @glandium fixed multiple build regressions before he added -Werror=declaration-after-statement to stop the suffering.

返回
发表文章
jasone
文章数
51
评论数
231
注册排名
5189