Therefore, I'm usually a Ruby programmer, so my understanding of Java conventions is unstable at best. If I have class A and to define a method to add two examples of that class, then what is the convention on behavior and return type?
public class A {// ... public What should I - Prompt a boolean breakthrough and modify the target, or
- Return a revised copy of the goal and throw an exception on the error
Which method fits with this kind of normal Java tradition?
Everyone here is thinking about the collection. Add () - Type method; But I suspect what you are thinking is in line, say, vector 2D.add ()
which connects the X and Y component of vector 2D?
In Java, as far as I can tell, the collection usually modifies itself (and does so, Collections.add).
However, the non-collection object (for example, vector 2D) is much different.
-
Cls add (Cls b)
that returns a new object and does not modify existing instances -
Add zero (CLS B)
which modifies this
and returns nothing (i.e. zero returns), it should not modify b
. There is no point in returning bool
because this type of excess should never fail (and if it happens anyway, exceptions would be appropriate). -
Cls add (CLS A, CLS B)
which returns a new object, neither modifies A and B. Cls.add () class is a static method in class.
Personally, I like the first style for arithmetic-style addition (); Ok because we can a.add (b) .add (c) .add (d) that looks like "a + b + c + d" (I usually do not do that if a
is a collection, because the serial for the collection object looks extra awkward.)
Comments
Post a Comment