关于返回void的文体问题 [英] Stylistic question concerning returning void

查看:98
本文介绍了关于返回void的文体问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下设计的示例:

  void HandleThat(){...} 

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

这段代码工作正常,我相信它的规范有效,但我(也许在我自己的)考虑这种不寻常的风格,因为调用似乎返回的函数的结果,尽管事实上,这两个函数原型为void。

$通常,我会期望看到:

  if(That){HandleThat ; return;} 

这样,我觉得对发生的事情没有歧义。

SO社区,我可以得到你对于返回无效编码风格是混乱还是有问题的意见?它有一个成语的感觉;我应该使用还是避免使用?



一般来说,我会努力澄清和使用第二种风格。

解决方案

我同意你的意见,第一种风格是混乱的,因为有一种意味着某种价值被返回。事实上,我不得不读几遍,因为。



从函数原型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.

解决方案

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;

这篇关于关于返回void的文体问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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