关于返回 void 的风格问题

发布于 2024-07-30 13:16:52 字数 480 浏览 1 评论 0原文

考虑以下人为的示例:

void HandleThat() { ... }

void HandleThis()
{
    if (That) return HandleThat();
    ...
}

这段代码工作得很好,我相当确定它是规范有效的,但我(也许是我自己)考虑了这种不寻常的风格,因为调用似乎返回了函数的结果,尽管事实上,这两个函数的原型都是无效的。

通常情况下,我希望看到:

if (That) {HandleThat(); return;}

我认为这不会对正在发生的事情留下任何歧义。

那么社区,我可以听听您对 return-void 编码风格是否令人困惑或有问题的看法吗? 它有一种成语的感觉; 我应该使用这个还是避免它?

一般来说,我会力求清晰并使用第二种风格。 另一方面,第一种形式的简洁性在某种程度上吸引了我。

Consider the following contrived example:

void HandleThat() { ... }

void HandleThis()
{
    if (That) return HandleThat();
    ...
}

This code works just fine, and I'm fairly sure it's spec-valid, but I (perhaps on my own) consider this unusual style, since the call appears to return the result of the function, despite the fact that both functions are prototyped to be void.

Typically, I would expect to see:

if (That) {HandleThat(); return;}

which, I feel, leaves no ambiguity as to what's going on.

SO community, can I get your opinion on whether the returning-void coding style is confusing or problematic? It has the feel of an idiom; should I use this or avoid it?

Generally I'd strive for clarity and use the second style. On the other hand, there's a neatness to the first form that draws me to it somewhat.

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

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

发布评论

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

评论(5

一世旳自豪 2024-08-06 13:16:52

我同意你的观点,第一种风格令人困惑,因为这暗示着某种价值正在被返回。 事实上,我不得不把它读了好几遍因为

当从原型为 void 的函数返回时,应该只是 return;

I agree with you, the first style is confusing because there's the implication that some sort of value is getting returned. In fact I had to read it over a couple times because of that.

When returning from a function prototyped void, it should just be return;

横笛休吹塞上声 2024-08-06 13:16:52

这可能有点太聪明了。 如果该行最终距离函数顶部超过几行,就会令人困惑。 这也意味着查看代码的程序员需要将

return HandleThat();

与 void 返回类型相关联,并在真正理解代码之前弄清楚其中的巧妙之处。 当您在 if/else 分支中执行多项操作时,您应该真正使用大括号并将步骤放在不同的行上。 占用更多空间但更容易理解:

if (That) {
    HandleThat();
    return;
}

This is probably just a little too clever. If that line ends up more than a couple of lines away from the top of the function, it'll be confusing. It also means the programmer looking at the code will need to correlate the

return HandleThat();

with the void return type and figure out the cleverness before they really understand the code. When you're doing more than one thing in an if/else branch, you should really use braces and put the steps on different lines. Takes up more space but is easier to understand:

if (That) {
    HandleThat();
    return;
}
哀由 2024-08-06 13:16:52

C 语言规则规定,如果声明为返回 void 的函数尝试返回表达式,则不会计算该表达式。

http://c0x.coding-guidelines.com/6.8.6.4.html

The C language rules say if a function declared as returning void tries to return an expression, the expression will not be evaluated.

http://c0x.coding-guidelines.com/6.8.6.4.html

无言温柔 2024-08-06 13:16:52

以前从未见过。

它的优点是看起来像非 void 返回类型的常见习惯用法,因此它读起来非常容易......

我不会更改它,除非有人可以证明它是无效的。

Never seen that before.

It has the advantage of looking like a common idiom for non-void return types, so it reads very easily...

I wouldn't change it unless someone can show that it is invalid.

非要怀念 2024-08-06 13:16:52

我认为第一个版本主要是为了简化模板编程。 如果 HandleThat 返回一个类型 T,该类型可能为 void,也可能不是 void,则使用第一个版本会很方便。

但在“正常”情况下,第二个版本更清晰,我更喜欢这样。

I believe that the first version is mainly allowed to ease template programming. If HandleThat returned a type T which might or might not be void, it's convenient to use the first version.

But in "normal" cases, the second version is clearer and I'd prefer that.

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