最佳做法 [英] Best practices
问题描述
一个关于所谓的最佳实践的快速问题,我对
感兴趣的人们会选择两个例子的A / B,以及为什么。 />
[A]
public enum MyEnum
{
Option1 = 0,
选项2 = 1,
选项3 = 2,
选项4 = 3
}
[b]
public enum MyEnum
{
Option1,
Option2,
Option3,
Option4
}
第二个:
[A]
公共字符串Foo
{
get
{
return this.foo;
}
set
{
if(this.foo!= value)
返回this.foo
}
}
[b]
公共字符串Foo
{
get
{
返回this.foo;
}
套装
{
返回this.foo
}
}
我也对
方法和其他最佳实践中的参数空检查的最佳实践感兴趣。微软网站在其最佳实践页面中有一些
(尝试/捕获,粗略调用等),但是在更全面的列表之后,我是
。 br />
谢谢
使用第二个示例显示错误/拼写错误,该示例应为:
[A]
公共字符串Foo
{
get
{
返回this.foo;
}
set
{
if(this.foo!= value)
this.foo = value;
}
}
[b]
公共字符串Foo
{
get
{
返回this.foo;
}
设定
{
this.foo = value;
}
}
在消息< 11 ********* *************@o13g2000cwo.googlegroups .com> ;, john
doe< sl ******** @ gmail.com>写道设置
{
if(this.foo!= value)
this.foo = value;
}
除了不必要的字符串比较外,这实现了什么?如果他们在操作之前等于
,那么在foo == value之后,如果他们不是,那么
foo == value。如果你不做比较,结果相同。
-
Steve Walker
< blockquote>
" Steve Walker" < ST *** @ otolith.demon.co.uk>在消息中写道
news:qT ************** @ otolith.demon.co.uk ...
在消息< ; 11 ********************** @ o13g2000cwo.googlegroups .com> ;, john
doe< sl ******** @ gmail.com>写道设置
{
if(this.foo!= value)
this.foo = value;
}
除了不必要的字符串比较之外,它实现了什么?如果他们在操作之前是平等的,那么在foo == value之后,如果他们不是,那就foo
== value。如果你不做比较,结果相同。
- 史蒂夫沃克
我想它取决于这是更昂贵的操作:不必要的比较或不必要的分配(即如果字符串是相等的)。我不确定,但是我通常会做这项任务。我只会在需要验证时将
插入新值。
A quick question, about so-called ''best practices'', I''m interested in
which of A/B of the two examples people would choose, and why.
[A]
public enum MyEnum
{
Option1 = 0,
Option2 = 1,
Option3 = 2,
Option4 = 3
}
[b]
public enum MyEnum
{
Option1,
Option2,
Option3,
Option4
}
The second one:
[A]
public string Foo
{
get
{
return this.foo;
}
set
{
if ( this.foo != value )
return this.foo
}
}
[b]
public string Foo
{
get
{
return this.foo;
}
set
{
return this.foo
}
}
I''m also interested in best practices for null checks for paramters in
methods and other best practices. The Microsoft website has a few
(try/catch, chunky calls etc.) in its best practices page, but I''m
after a more comprehensive list.
Thanks
Glaring error/typo with the second example, which should read:
[A]
public string Foo
{
get
{
return this.foo;
}
set
{
if ( this.foo != value )
this.foo = value;
}
}
[b]
public string Foo
{
get
{
return this.foo;
}
set
{
this.foo = value;
}
}
In message <11**********************@o13g2000cwo.googlegroups .com>, john
doe <sl********@gmail.com> writesset
{
if ( this.foo != value )
this.foo = value;
}
What does this achieve, other than a needless string comparison? If they
are equal before the operation, after it foo == value, if they aren''t,
foo == value. If you don''t do the compare, same result.
--
Steve Walker
"Steve Walker" <st***@otolith.demon.co.uk> wrote in message
news:qT**************@otolith.demon.co.uk...In message <11**********************@o13g2000cwo.googlegroups .com>, john
doe <sl********@gmail.com> writesset
{
if ( this.foo != value )
this.foo = value;
}
What does this achieve, other than a needless string comparison? If they
are equal before the operation, after it foo == value, if they aren''t, foo
== value. If you don''t do the compare, same result.
--
Steve Walker
I guess it depends on which is the more expensive operation: a needless
comparison or a needless assignment (i.e. if the strings are equal). I''m
not sure, but I would typically just do the assignment. I would only
interogate the new value it it needed validation.
这篇关于最佳做法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!