关于回归方式的问题 [英] Question about return style
问题描述
我只是想知道是否有一个最好的选择以下
从方法中返回值的几种方法:
1)
private HashAlgorithm GetSpecificHashAlgorithm(string hashString){
if(hashString ==" MD5"){
返回System.Security.Cryptography.MD5.Create();
} else {
返回System.Security.Cryptography.SHA512.Create();
}
}
>
或
2)
private HashAlgorithm GetSpecificHashAlgorithm(string hashString){
HashAlgorithm hash;
if(hashString ==" MD5"){
hash = System.Security.Cryptography.MD5.Create();
} else {
hash = System.Security.Cryptography.SHA512.Create();
}
返回哈希值;
}
对于哪种风格更好有一般意见吗?
谢谢,
马特
I was just wondering if there is a "best" choice from the following
couple of ways of returning a value from a method:
1)
private HashAlgorithm GetSpecificHashAlgorithm(string hashString){
if (hashString == "MD5") {
return System.Security.Cryptography.MD5.Create();
} else {
return System.Security.Cryptography.SHA512.Create();
}
}
or
2)
private HashAlgorithm GetSpecificHashAlgorithm(string hashString){
HashAlgorithm hash;
if (hashString == "MD5") {
hash = System.Security.Cryptography.MD5.Create();
} else {
hash = System.Security.Cryptography.SHA512.Create();
}
return hash;
}
Is there a general opinion on which is better style?
Thanks,
Matt
推荐答案
除非你有额外的合作de可能想使用本地hash
变量,我认为它没什么区别。但是,您可能需要
来关注做什么,有人会调用您的方法并通过
" hamburger"作为参数......
彼得
"马特B" < ro ********** @ gmail.com写信息
news:29 ******************** ************** @ s21g2000 prm.googlegroups.com ...
Unless you have additional code that might want to use the local "hash"
variable, I don''t think it makes much difference. However, you might want
to pay attention to what to do is somebody calls your method and passes
"hamburger" as the parameter...
Peter
"Matt B" <ro**********@gmail.comwrote in message
news:29**********************************@s21g2000 prm.googlegroups.com...
>我只是想知道是否有"最好"选择以下
从方法中返回值的几种方法:
1)
private HashAlgorithm GetSpecificHashAlgorithm(string hashString){
if(hashString ==" MD5"){
返回System.Security.Cryptography.MD5.Create();
} else {
返回System.Security.Cryptography.SHA512.Create();
}
}
>
或
2)
private HashAlgorithm GetSpecificHashAlgorithm(string hashString){
HashAlgorithm hash;
if(hashString ==" MD5"){
hash = System.Security.Cryptography.MD5.Create();
} else {
hash = System.Security.Cryptography.SHA512.Create();
}
返回哈希值;
}
我对此有一般看法吗?更好的风格?
谢谢,
马特
>I was just wondering if there is a "best" choice from the following
couple of ways of returning a value from a method:
1)
private HashAlgorithm GetSpecificHashAlgorithm(string hashString){
if (hashString == "MD5") {
return System.Security.Cryptography.MD5.Create();
} else {
return System.Security.Cryptography.SHA512.Create();
}
}
or
2)
private HashAlgorithm GetSpecificHashAlgorithm(string hashString){
HashAlgorithm hash;
if (hashString == "MD5") {
hash = System.Security.Cryptography.MD5.Create();
} else {
hash = System.Security.Cryptography.SHA512.Create();
}
return hash;
}
Is there a general opinion on which is better style?
Thanks,
Matt
Matt B写道:
Matt B wrote:
我只是想知道是否有最佳的选择以下
从方法中返回值的几种方法:
1)
private HashAlgorithm GetSpecificHashAlgorithm(string hashString){
if(hashString ==" MD5"){
返回System.Security.Cryptography.MD5.Create();
} else {
返回System.Security.Cryptography.SHA512.Create();
}
}
>
或
2)
private HashAlgorithm GetSpecificHashAlgorithm(string hashString){
HashAlgorithm hash;
if(hashString ==" MD5"){
hash = System.Security.Cryptography.MD5.Create();
} else {
hash = System.Security.Cryptography.SHA512.Create();
}
返回哈希值;
}
是否存在一般性意见哪个风格更好?
I was just wondering if there is a "best" choice from the following
couple of ways of returning a value from a method:
1)
private HashAlgorithm GetSpecificHashAlgorithm(string hashString){
if (hashString == "MD5") {
return System.Security.Cryptography.MD5.Create();
} else {
return System.Security.Cryptography.SHA512.Create();
}
}
or
2)
private HashAlgorithm GetSpecificHashAlgorithm(string hashString){
HashAlgorithm hash;
if (hashString == "MD5") {
hash = System.Security.Cryptography.MD5.Create();
} else {
hash = System.Security.Cryptography.SHA512.Create();
}
return hash;
}
Is there a general opinion on which is better style?
不,但我很确定会有很多人提供自己的风格。
单出口与多出口的出现与返回声明一样古老。
就我而言,只要你保持你的功能小(这是一个好主意,这是一个bikeshed问题)。 http://www.bikeshed.com/
专注于风格,你甚至没有解决你的
函数的实际问题(甚至认为它们只是为了说明一个问题):
- 你不能检查参数是否为空。虽然这些函数是私有的,但我也不会省略一个简单的空检查,即使是私有函数也没有,因为它很便宜并且非常有效地及早发现问题。如果你是
不期待hashString永远都是空的,你会惊讶于
突然使用你从未想过要求的SHA512哈希。
- 你不检查如果参数是SHA512。任何不是MD5的东西都是
SHA512?那是不可扩展的。字典查找会更灵活,
并且不需要为新算法重写函数。
- 您正在复制已经可用的功能形式为
HashAlgorithm.Create()。
最后,不太重要的是,虽然你可以自由命名私人成员在
无论你喜欢什么,最好不要用
大写字母来启动它们,这样它就不会立刻变成公众成员(作为
那些*应该*以大写字母开头)。不过,这是一个意见,
因为你可以自由命名你的私人会员。
-
J.
http://symbolsprose.blogspot.com
2008年5月28日星期三10:57:15 -0700,Matt B< ro ********** @ gmail.comwrote:
On Wed, 28 May 2008 10:57:15 -0700, Matt B <ro**********@gmail.comwrote:
我只是想知道是否有最佳的选择以下
从方法返回值的几种方法:
[...]
有没有一般意见哪种风格更好?
I was just wondering if there is a "best" choice from the following
couple of ways of returning a value from a method:
[...]
Is there a general opinion on which is better style?
我怀疑你会找到共识。 :)我的偏好是#2,因为它允许我在一个地方保留任何
常见的方法结束处理。如果我从一开始就编写所有我的
方法,那么如果我必须添加类似
之类的东西,那么它已经为此设置了。
但即使有这种偏好,我也知道在方法中间放置一个return
语句。 :)
Pete
I doubt you''ll find a consensus. :) My preference is for #2, because it
allows me to keep any
common end-of-method processing all in one place. If I write all my
methods like that from the outset, then if I have to add something like
that later, it''s already set up for that.
But even with that preference, I have been known to put a "return"
statement in the middle of a method. :)
Pete
这篇关于关于回归方式的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!