自定义对象中的方法应该具有破坏性吗?
我必须实现 Java.Polynomial 作为学校作业。 部分方法有 add(polynomial)、multiply(polynomial) 等。
在类似的情况下
p.add(q); // computes p + q
,最好返回 void 并将多项式的总和保留在p? 或者返回一个多项式并保持 p 的前值不变更好?
我的直觉告诉我,我应该执行以下
- 实现 p.add(q) 作为“破坏性”方法...它将 q 添加到 p 的值并将总和存储在 p 中
- 还实现静态方法 Polynomial.add(p ,q) 返回多项式之和。
你怎么认为?
I have to implement Java.Polynomial as a school assignment. Part of the methods are add(polynomial), multiply(polynomial) etc.
In the case of something like
p.add(q); // computes p + q
is it better to return void and keep the sum of the polynomials in p? or is it better to return a polynomial and keep the former value of p intact?
My instinct tells me that i should do the following
- implement p.add(q) as a 'destructive' method... it adds q to the value of p and stores the sum in p
- also implement a static method Polynomial.add(p,q) which returns the sum of the polynomials.
What do you think?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
就我个人而言,我真的很喜欢不可变类型。 我会编写
p.plus(q)
而不是p.add(q)
并使其返回一个新的多项式。 不变性有很多好处,不仅在线程安全方面(这听起来在这种情况下不会成为问题),而且在推理代码方面也如此。 如果您知道一旦您存储了对某个对象的引用,就无法更改您脚下对象的内容,那么预测将要发生的事情就会容易得多。Personally I really like immutable types. I would write
p.plus(q)
instead ofp.add(q)
and make it return a new polynomial. Immutability has a lot going for it, not just in terms of thread-safety (which sounds like it won't be an issue in this case) but also in terms of reasoning about your code. It's a lot easier to predict what's going to happen if you know that nothing can change the contents of an object under your feet once you've stashed a reference to it.这是风格问题。 当对象是不可变的时,通常会更好(并且“更整洁”),因此方法不是“破坏性的”(它们不是变异器,没有副作用),而是返回具有新值的新实例。
最著名的不可变类是 String。
It's a matter of style. It's usually better (and "neater") when objects are immutable so methods are not "destructive" (they aren't mutators, they have no side effects) but return a new instance with the new value.
The most well known immutable class is String.
“最好返回一个多项式并保持 p 的前值不变”
如果你让你的数字类型表现得像其他数字一样,生活会更好。
当您必须通过减少内存分配来优化性能时,“破坏性”操作是不可避免的。
代价是,由于变量中看起来更复杂的状态变化,您的处理变得不透明。 你不想要隐藏状态的改变; 你希望你对变量的赋值是一个大的、明显的一流的、带有-an-=的东西。
"It is better to return a polynomial and keep the former value of p intact"
Life is better if you make your numeric types behave like other numbers.
The "destructive" operations are a necessary evil when you have to optimize performance by reducing memory allocations.
The tradeoff is that your processing becomes opaque because of the more complex-looking state changes in the variables. You don't want hidden state change; you want your assignments of values to variables to be a big, obvious first-class, with-an-= kind of thing.
所以每个人都说你应该使其不可变,除非有非常具体的性能原因使其可变(你将向一个神奇的多项式添加十亿个多项式并且不希望内存分配),否则不变性就是方法去。
如果您有真正的性能原因,您可能想要有破坏性和非破坏性版本,但请记住 - 这会导致混乱。 我认为你的重载 add 具有破坏性或不基于数量的想法会让事情变得非常非常混乱。 最好使所有内容都具有非破坏性,并且如果您选择的话,可以使用 addDestructive(poly p) 方法。
So everyone is saying that you should make it immutable, and unless there are very specific performance reasons to make it mutable (you'll be adding a billion polynomials to a single magic one and don't want memory allocation), immutability is the way to go.
If you have genuine performance reasons, you may want to have a destructive and a non-destructive version, but remember -- this leads to confusion. I think your idea of overloading add to be destructive or not based on the arity will make things very, very confusing. Better make everything non-destructive and make a addDestructive(poly p) method if you choose.
即使您执行“破坏性”方法,您仍然希望返回新值以允许函数链接。 而且,无论如何,当 BigInteger 和 BigDecimal 面临同样的问题时,Sun 决定采取非破坏性的方式。
Even if you do the "destructive" method, you're going to want to return the new value anyway to allow for function chaining. And, for what it's worth, when faced with the same problem for BigInteger and BigDecimal, Sun decided to go non-destructive.
使其不可变,并使接口与 BigInteger 相同(只要适用)。
Make it immutable, and make the interface the same as for BigInteger (as far as applicable).
不错,也不一定更好。
更好,更少的意外,代码更容易理解和维护。 (尽管您必须创建更多变量)
Not bad, not necessarily better either.
It is better, less surprises and code easier to understand and maintain. ( although you will have to create more variables )
多项式在其变量方面具有固定值,因此只有它不可变才真正有意义。 返回一个新的多项式用于您的操作。
A polynomial has a fixed value in terms of its variables, and thus it only really makes sense for it to be immutable. Return a new Polynomial for your operations.