使用out参数时,什么是不好的做法? [英] What is bad practice when using out parameters?
问题描述
有什么原则使用了参数时要记住?或者,我可以把他们看成只是一个很好的方式,让一个方法返回多个值?
Are there any principles to keep in mind when using out parameters? Or can I look at them as just a good way to let a method return multiple values?
做什么语言设计者心目中的时候被指定输出参数?
What did the language designers have in mind when they were specifying the out parameter?
经过一番思考编辑:
因为我现在考虑这个问题,我会倾向于说,过度使用out参数可能是一个indicitive code-气味。如果一个方法需要返回多于1特定类型的情况下,它意味着,该方法具有超过1的关注,这是违反SRP的
As i'm thinking about it now, I would be inclined to say that excessive use of out parameters could be indicitive of a code-smell. If a method needs to return instances of more than 1 specific type it implies that the method has more than 1 concern, which is a violation of SRP.
推荐答案
就个人而言,我对不太热衷了
超越既定的的TryParse参数
约定。我preFER返回一个封装不同的参数,因为它使code更容易阅读海事组织特定类型的实例。
Personally, I am not too keen on out
parameters beyond the established TryParse
convention. I prefer to return an instance of a specific type that encapsulates the different parameters, as it makes the code much easier to read imo.
如果您不想定义特定的类型,你可以使用的在即将到来的.NET架构元组类型。元组基本上都是一堆包裹在一个类型的值。它们都采用了大量的例如F#。
If you don't want to define specific types for this, you can use the Tuple type in the upcoming .NET framework. Tuples are basically a bunch of values wrapped in a type. They are used a lot in e.g. F#.
这篇关于使用out参数时,什么是不好的做法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!