风格:这两个代码块是否相同? [英] Style: Are these two blocks of code equivalent?

查看:51
本文介绍了风格:这两个代码块是否相同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



此代码应该从流

对象创建一个streamreader对象,我想如果请求使用编码(enc isn''t null)...

第二位是别人的代码...


///代码块#1

if(enc!= null)

{

reader = new StreamReader(s,enc);

}

else

{

reader = new StreamReader(s);

}


///代码块#2

reader =(enc!= null)?新的StreamReader(s,enc):新的StreamReader(s);


好​​的,如果它们是相同的,为什么你会使用#2?要可爱吗?什么是

这是一个旧的C ++黑客编码器伦理?


感谢任何信息。


M 〜

解决方案

为什么不简单地调用


reader = new StreamReader(s,enc)?


如果编码为空,则ctor的行为就像你要调用new

StreamReader(s)一样。


至于你的原始问题:样式#2仅供新手使用

试图证明他们能够理解复杂的语法规则但没有

专业程序员会编写这样的代码,因为这样的代码是不可维护的。

Michael McCarthy <无******** @ nomail.com> schrieb im Newsbeitrag

新闻:OM ************** @ TK2MSFTNGP09.phx.gbl ...


这个代码应该从流对象创建一个streamreader对象
我想如果请求使用编码(enc isn''t null)......第二位
是别人的代码...

///代码块#1
if(enc!= null)
{
reader = new StreamReader(s,enc);
}

{
读者=新的StreamReader;
}
///代码块#2
reader =(enc!=空值) ?新的StreamReader(s,enc):新的StreamReader;

好的,如果它们是相同的,为什么你会使用#2?要可爱吗?这是什么,来自旧的C ++黑客编码器伦理?

感谢任何信息。

M~



两者之间真的没什么区别..第二个是第一个更短的

版本。


迈克尔Cierkowski

" Michael McCarthy" <无******** @ nomail.com>在留言中写道

新闻:OM ************** @ TK2MSFTNGP09.phx.gbl ...


这代码应该从流对象创建一个streamreader对象
我想如果请求使用编码(enc isn''t null)......第二位
是别人的代码...

///代码块#1
if(enc!= null)
{
reader = new StreamReader(s,enc);
}

{
读者=新的StreamReader;
}
///代码块#2
reader =(enc!=空值) ?新的StreamReader(s,enc):新的StreamReader;

好的,如果它们是相同的,为什么你会使用#2?要可爱吗?这是什么,来自旧的C ++黑客编码器伦理?

感谢任何信息。

M~






Michael McCarthy写道:

好的,如果它们是相同的,为什么要使用#2 ?要可爱吗?什么是来自旧的C ++黑客编码器伦理?




这是一个偏好问题。这些位完全相同。


*警告*,下面有很高的个人观点,他们很可能会开始巨大的

火焰战争:)


我个人不喜欢版本1,因为所有的空白行都是

" {&#;'s浪费我的屏幕空间,让人更难查看更多内容一次只需几行代码就可以了。


我不喜欢第2版,因为对于很多人来说它真的有点密码。 />
它有点短,因为它允许你在

相同的行上声明和初始化,并将分支压缩成表达式。你是否可以使用它取决于观众。一些

的人读过这个以及if ... else,其他人不会:


StreamReader reader =

(enc == null)?新的StreamReader:新的StreamReader(s,enc);


无论如何,我倾向于选择:


StreamReader阅读器;

if(enc == null)

reader = new StreamReader(s);

else

reader =新的StreamReader(s,enc);


这对大多数人来说都是可读的,并且有一个很好的结构化布局,

而没有占用整个屏幕一个简单的选择。


我也想避免测试!= null(如果它没有打扰

程序流程)。我读了!= null作为未定义,这是一个双重的b / b
否定,并且没有理由使这些分支难以理解

与那些;)


但这里的*真实问题是,你为什么要接受一对

(Stream,Reader)?你能不能只接受一个StreamReader实例

?您的功能调用者可能*知道*他是否有编码或不编码,并且可以自己创建正确的StreamReader。


-

Helge Jensen

mailto:他********** @ slog.dk

sip:他******* ***@slog.dk

- =>塞巴斯蒂安的封面音乐: http://ungdomshus.nu < = -



This code is supposed to create a streamreader object from a stream
object and I suppose use encoding if requested (enc isn''t null)... The
second bit is someone elses code...

/// code block #1
if (enc != null)
{
reader = new StreamReader(s, enc);
}
else
{
reader = new StreamReader(s);
}

/// code block #2
reader = (enc != null) ? new StreamReader(s, enc) : new StreamReader(s);

Okay, if they are the same, why would you use #2? To be cute? What is
this from, an old C++ hacking coder ethic?

thanks for any info.

M~

解决方案

Why not simply calling

reader = new StreamReader(s, enc) ?

if encoding is null, the ctor behaves as if you would have called "new
StreamReader(s)".

As for your original question: style #2 is only used by newbies who are
trying to proof that they are able to understand complex syntax rules but no
professional programmer would write code like that because such code is
unmaintainable.
"Michael McCarthy" <no********@nomail.com> schrieb im Newsbeitrag
news:OM**************@TK2MSFTNGP09.phx.gbl...


This code is supposed to create a streamreader object from a stream object
and I suppose use encoding if requested (enc isn''t null)... The second bit
is someone elses code...

/// code block #1
if (enc != null)
{
reader = new StreamReader(s, enc);
}
else
{
reader = new StreamReader(s);
}

/// code block #2
reader = (enc != null) ? new StreamReader(s, enc) : new StreamReader(s);

Okay, if they are the same, why would you use #2? To be cute? What is this
from, an old C++ hacking coder ethic?

thanks for any info.

M~



There is really no difference between the two.. The second is just a shorter
version of the first.

Michael Cierkowski
"Michael McCarthy" <no********@nomail.com> wrote in message
news:OM**************@TK2MSFTNGP09.phx.gbl...


This code is supposed to create a streamreader object from a stream object
and I suppose use encoding if requested (enc isn''t null)... The second bit
is someone elses code...

/// code block #1
if (enc != null)
{
reader = new StreamReader(s, enc);
}
else
{
reader = new StreamReader(s);
}

/// code block #2
reader = (enc != null) ? new StreamReader(s, enc) : new StreamReader(s);

Okay, if they are the same, why would you use #2? To be cute? What is this
from, an old C++ hacking coder ethic?

thanks for any info.

M~





Michael McCarthy wrote:

Okay, if they are the same, why would you use #2? To be cute? What is
this from, an old C++ hacking coder ethic?



It''s a matter of preference. The bits are exactly equivalent.

*Warning*, higly personal views below, they are likely to start vast
flame-wars :)

I personally dislike version 1, because of all the blank lines with
"{"''s on wasting my screen-space, making it harder to view more than a
few lines of code at a time.

I dicourage version 2, since it''s really a bit cryptoc for many people.
It''s a bit shorter, since it allows you do declare and initialize on the
same line and compresses the branch into an expression. Whether you can
(in a specific setting) use it depends largely on the audience. Some
people read this as well as "if...else", others don''t:

StreamReader reader =
(enc == null) ? new StreamReader(s) : new StreamReader(s,enc);

In any case, I tend to prefer:

StreamReader reader;
if ( enc == null )
reader = new StreamReader(s);
else
reader = new StreamReader(s, enc);

Which is readable to most people, and has a nice structured layout,
while not taking up the whole screen for a simple choice.

I also like to avoid testing != null (if it doesn''t disturb the
program-flow). I read "!= null" as "not undefined", which is a double
negation and there is no reason for complicating branches unnessesarily
with those ;)

But the *real* question here is, why are you accepting a pair of
(Stream, Reader)? could you not just accept a StreamReader instance
instead? The caller of you functionality probably *knows* whether he has
an encoding or not and can just create the proper StreamReader himself.

--
Helge Jensen
mailto:he**********@slog.dk
sip:he**********@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-


这篇关于风格:这两个代码块是否相同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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