A
Alex Hunsley
Oliver said:I think one reason Chris suggested the q3 = q1.add(q2) syntax is that
it's pretty idiomatic of Java. Strings and BigInteger, for example, both
follow that pattern. So a Java programmer whose is used to using Strings and
BigInteger can quickly guess what the syntax is for your Quaternion classes.
I've always found that idiom a little annoying in a purist sense,
although you're right in that it is a standard. I think that on first
look, when you're not used to this standard, you might expect an
instance method like q1.add(q2) to actually have an effect on the value
of q1 - but it doesn't (it just returns the result). And I know that
many people do trip up over this assumption, initially.
To me, Quaternion.add(q1, q2) is clearer - a result will be returned.
That said, notice that Chris suggested a way so that both formulations
are available (i.e. q3 = q1.add(q2) would be equivalent to q3 =
Quaternion.add(q1, q2)) with minimal hassle.
Yup, if space isn't a big limit, I prefer implementing both forms when
writing such a class, and giving the user the choice.
lex