评论风格 [英] Comment on style

查看:63
本文介绍了评论风格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我在5分钟前编程时出现了一个反复出现的问题,这次我想听听一些意见在风格上。虽然它们通常是个人的b $ b,但我认为在这种情况下也与制作

代码更容易阅读。


想象一个函数返回void(例如),如果有很多特殊情况,它的身体是一个很大的




void foo( const MyClass& c){

if(c.bar()){

x();

}否则if(c.foobar ()){

y();

}否则if(c.mybar()){

z();

}

}


问题是你是否愿意看到我写过它或者用

返回;在调用x()之后,调用y()之后的另一个等等

等...?


干杯,

Paulo Matos

Hi all,

While I was programming 5 minutes ago a recurring issue came up and
this time I''d like to hear some opinions on style. Although they are
usually personal I do think that in this case as also to do with making
the code easier to read.

Imagine a function returning void (for example) and it''s body is a big
if with lots of special cases:

void foo(const MyClass &c) {
if(c.bar()) {
x();
} else if(c.foobar()) {
y();
} else if(c.mybar()) {
z();
}
}

The question is if you prefer to see like I''ve wrote it or with a
return; after a call to x(), and another one after a call to y(), etc
etc... ?

Cheers,

Paulo Matos

推荐答案

po ****** @ gmail.com 写道:
大家好,

虽然我在5分钟前编程了反复出现问题,这次我想听听一些关于风格的意见。虽然它们通常是个人的,但我认为在这种情况下也可以使代码更容易阅读。

想象一个函数返回void(例如)和如果有很多特殊情况,它的身体是很大的:

void foo(const MyClass& c){
if(c.bar()){
x();
} if if(c.foobar()){
y();
} else if(c.mybar()){
z();
}


问题是,如果你喜欢看到我已经写过它或带着
返回;在调用x()之后,调用y()之后的另一个等等等等......?
Hi all,

While I was programming 5 minutes ago a recurring issue came up and
this time I''d like to hear some opinions on style. Although they are
usually personal I do think that in this case as also to do with making
the code easier to read.

Imagine a function returning void (for example) and it''s body is a big
if with lots of special cases:

void foo(const MyClass &c) {
if(c.bar()) {
x();
} else if(c.foobar()) {
y();
} else if(c.mybar()) {
z();
}
}

The question is if you prefer to see like I''ve wrote it or with a
return; after a call to x(), and another one after a call to y(), etc
etc... ?



返回的目的是什么?你已经在if else

结构中了。


-

Ian Collins。


What purpose would the returns serve? You are already in an if else
structure.

--
Ian Collins.


* po******@gmail.com

想象一个函数返回void(例如),如果有很多特殊情况,它的身体是很大的:

void foo(const MyClass& c){
if(c.bar()){
x();
} if if(c.foobar()){
y();
} else if(c.mybar()){
z();
}
}

问题是你是否愿意看到我喜欢ve写了


不,你应该总是使用适当的缩进,比如


void foo(const MyClass& c){

if(c.bar()){

x();

}否则if(c.foobar()){

y();

}否则if(c.mybar()){

z();

}

}


返回;在调用x()之后,调用y()之后的另一个等等等等......?

Imagine a function returning void (for example) and it''s body is a big
if with lots of special cases:

void foo(const MyClass &c) {
if(c.bar()) {
x();
} else if(c.foobar()) {
y();
} else if(c.mybar()) {
z();
}
}

The question is if you prefer to see like I''ve wrote it
No, you should always use proper indentation, like

void foo(const MyClass &c) {
if(c.bar()) {
x();
} else if(c.foobar()) {
y();
} else if(c.mybar()) {
z();
}
}
or with a
return; after a call to x(), and another one after a call to y(), etc
etc... ?




使用异常安全代码在技​​术上并不重要。但是

相对于清晰度而言,它仅适用于前置条件检查。在

范围内它可以隐藏代码结构的功能(

当然假定适当的缩进开始),它可以使

维护更加困难,例如你想在最后添加一些

的常用动作,所以这是一个判断电话。


对于C语言来说,故事是不同的,我认为我在这里给新手的最好建议是:如果你不明白差异

之间的C和C ++ wrt。早期退货,然后绝对不要使用它们;

否则,使用清晰的代码作为主要指导。


-

答:因为它弄乱了人们通常阅读文本的顺序。

问:为什么这么糟糕?

A:热门帖子。

问:usenet和电子邮件中最烦人的事情是什么?



With exception safe code it doesn''t matter technically. But with
respect to clarity it''s proper only for precondition checking. Within
the meat of the function it can hide the structure of the code (of
course that assumes proper indentation to begin with), and it can make
maintenance more difficult, for example where you want to add some
common action at the end, so there it''s a judgement call.

For the C language the story is different, and I think the best advice I
can give to a novice here is: if you do not understand the difference
between C and C++ wrt. early returns, then absolutely don''t use them;
otherwise, use clarity of code as the main guideline.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


po ****** @ gmail.com 写道:
po******@gmail.com wrote:
大家好,

虽然我在5分钟前编程时出现了一个反复出现的问题。
这次我想听听一些关于风格的意见。虽然它们通常是个人的,但我认为在这种情况下也可以使代码更容易阅读。

想象一个函数返回void(例如)和如果有很多特殊情况,它的身体是很大的:

void foo(const MyClass& c){
if(c.bar()){
x();
} if if(c.foobar()){
y();
} else if(c.mybar()){
z();
}


问题是,如果你喜欢看到我已经写过它或带着
返回;在打电话给x()之后,另一个人打电话给y()之后等等......?

干杯,

Paulo Matos
Hi all,

While I was programming 5 minutes ago a recurring issue came up and
this time I''d like to hear some opinions on style. Although they are
usually personal I do think that in this case as also to do with making
the code easier to read.

Imagine a function returning void (for example) and it''s body is a big
if with lots of special cases:

void foo(const MyClass &c) {
if(c.bar()) {
x();
} else if(c.foobar()) {
y();
} else if(c.mybar()) {
z();
}
}

The question is if you prefer to see like I''ve wrote it or with a
return; after a call to x(), and another one after a call to y(), etc
etc... ?

Cheers,

Paulo Matos




是的,风格绝对是个人风格。


Ian Collins已经询问退货的目的是什么? >
已经在if / else块中。我同意他的问题,并指出

你已经说过这个功能很大,所以你需要考虑是否

它会对另一个开发者有帮助以后在尝试解决

例程中的错误时有多个退出点来处理多个退出点,我认为这不是。

Alf P. Steinbach表示你应该再次使用适当的缩进

我同意,但是适当的缩进是否需要2个空格,4

空格或制表符对个人解释持开放态度。我自己,我更喜欢标签。


现在我与大多数遵循K& R风格的人不同,因为我会

更喜欢代码写为


void foo(const MyClass& c)

{


if(c.bar())

{

x();

}

else if(c.foobar())

{

y();

}

else if(c.mybar())

{

z();

}


}


我发现哪一个更具可读性事情并发现更容易注释掉

代码块,因为


}否则if(c.foobar()){

y();

}否则if(c.mybar()){


要求你编辑} else if(c.foobar) ()){"行将else

语句放在另一行,以便注释掉该行或插入

" / *"在else行的中间,而


}

else if(c.foobar())

{

y();

}

否则if(c.mybar())


只需要你在/ else if(c.foobar())之前放置一个/ *行

和一个* /关闭支撑后。


同样我更愿意看到


else if(c.foobar())

{

y();

}


而不是


else if(c.foobar())

y();


如你所说,风格是个人的。


最后我讨厌单个字符变量名,即使在示例代码中,甚至是

虽然我有时(很少)自己使用它们。


干杯,

Pep。



Yep, style is definitely personal.

Ian Collins has already asked what purpose would the returns serve as you
are already in an if/else block. I agree with his question and would point
out that you have said this a big function, so you need to consider whether
it would be helpful for another developer to come at a later time and have
to deal with multiple exit points when trying to solve a bug in the
routine, which I would say it is not.
Alf P. Steinbach has stated that you should use proper indentation which
again I agree with but as to whether proper indentation demands 2 spaces, 4
spaces or tabs is open to personal interpretation. Myself, I prefer tabs.

Now I differ from most people that follow the K&R style because I would
prefer the code writtens as

void foo(const MyClass &c)
{

if(c.bar())
{
x();
}
else if(c.foobar())
{
y();
}
else if(c.mybar())
{
z();
}

}

Which I find more readable foe one thing and find it easier to comment out a
block of code because

} else if(c.foobar()) {
y();
} else if(c.mybar()) {

requires you to edit the "} else if(c.foobar()) {" line to place the "else"
statement on a separate line in order to comment out the line or insert a
"/*" in the middle of the else line, whereas

}
else if(c.foobar())
{
y();
}
else if(c.mybar())

simply requires you to place a /* ahead of the "else if(c.foobar())" line
and a "*/" after the closing brace.

Similarly I prefer to see

else if(c.foobar())
{
y();
}

as opposed to

else if(c.foobar())
y();

As you say, style is personal.

Finally I hate single character variable names, even in example code, even
though I sometimes (rarely) use them myself :)

Cheers,
Pep.


这篇关于评论风格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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