在方法中传递数据对象或数据对象的一部分?
public class DataObject{
public int Value1;
public int Value2;
public int Value3;
public int Value4;
public int Value5;
public DataObject(){}
}
public class SomeClass{
public SomeClass(){}
public void MultiplyFirstThreeValues(int Value1, int Value2, int Value3){
return Value1*Value2*Value3;
}
public void MultiplyFirstThreeValues(DataObject d){
return d.Value1*d.Value2*d.Value3;
}
}
将所需的每个变量发送到方法或整个数据对象(在并非使用数据对象中的每个变量的情况下),哪种做法更好?这可能是一个“艺术大于科学”的问题,但如果你不能给出一个硬性规定,那么我有兴趣听听做一个而不是另一个的动机是什么(在这种情况下,如果不该方法中使用了数据对象的所有值)。
谢谢,我期待了解更多相关信息。
public class DataObject{
public int Value1;
public int Value2;
public int Value3;
public int Value4;
public int Value5;
public DataObject(){}
}
public class SomeClass{
public SomeClass(){}
public void MultiplyFirstThreeValues(int Value1, int Value2, int Value3){
return Value1*Value2*Value3;
}
public void MultiplyFirstThreeValues(DataObject d){
return d.Value1*d.Value2*d.Value3;
}
}
Which is a better practice, to send each variable needed to a method or an entire data object (in the case where not every variable in the data object is used)? This may be an "art more than science" question, but if you can't give a hard and fast rule, then I'd be interested in hearing what would be the motivation to do one over the other (in this case where not all of the data object's values are used in the method).
Thanks, I'm looking forward to learning more about this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我会选择选项C将逻辑保留在对象本身内:
编辑:
为了回应评论 - 我将添加一句话:
在我看来,这只是一种更好的设计方法,因为它可以让您隐藏实际获取结果/执行实现的方式。您当前的设置方式更多地反映了访问者模式,它可能很强大,但也由于类的接口现在拥有所有这些公共成员,因此增加了更多的复杂性。如果您的计算跨越多个 DataObject 和/或其他类型,这实际上可能有意义,但如果我能摆脱它,我肯定会使用封装。
I would opt for option C leave the logic within the object itself:
Edit:
In response to the comment - I'll add a quote:
It's just a better design approach imo since it lets you hide the way you actually get your results / do your implementation. The way you have it currently set up more reflects the Visitor pattern, which can be powerful, but also adds more complexity since the interface to your class has now all these public members. If your calculation would span multiple
DataObject
's and/or other types this actually might make sense though but I would definitely use encapsulation if I can get away with it.经验法则:如果您有超过 7 个参数(尽管有些人会说超过 3 个),请考虑使用 引入参数对象重构。
一般来说,如果参数数量较少,请传递对象的部分内容,特别是如果您不希望参数数量及其类型发生变化。
如果上述情况并非如此(您期望参数的数量/类型发生变化和/或您有很多参数),请使用该对象。
注意:
有很多参数是一种代码味道,可能表明您的方法/类做得太多,应该分解。
Rule of thumb: if you have more than 7 parameters (though some would say more than 3), consider using an Introduce parameters object refactoring.
In general, if you have a small number of parameters, pass parts of the object, particularly if you don't expect the number of parameters and their types to change.
If the above is not the case (you expect changes in the numbers/types of the parameters and/or you have many of them), use the object.
Note:
Having many parameters is a code smell that can indicate that your method/class is doing too much and should be broken up.
具有许多输入参数的方法签名通常被认为表明缺少一个类。有多少是多少?很难说,但三个对我来说并不特别多。
思考过程中需要考虑的另一件事是这些值是否总是以相同的方式来自同一个对象。如果该方法可以用于其他输入组合,也许保留多参数输入会更好。当然,如果参数确实很多,你可以为参数集提供一个专门设计的类型,然后使用它。
A method signature with many input parameters is often said to be an indication that there is a class missing. How many are many? Impossible to say, but three does not strike me as particularly many.
Another thing to put into the thought process is whether the values always come from the same object, in the same manner. If it could be that the method could be used for other combinations of input, perhaps keeping a multi-parameter input is better. Of course, if there are really many parameters, you could provide a specially designed type for the parameter set, and use that instead.
就个人而言,我倾向于仅传递对象,尽管不使用所有属性,因为这会导致更小的方法签名,我觉得在读取代码时比许多参数更容易在心理上解析
Personaly I would favour passing the object only, despite not using all proerties as this leads to smaller method signatures which I feel are easier to mentally parse when code reading than many parameters