Method names for Valhalla value types
Stephen Colebourne
scolebourne at joda.org
Wed Sep 10 14:52:33 UTC 2025
At some point, hopefully relatively soon, we will see new value types
appear, especially numeric ones. I'd like to talk about the method
naming conventions that should be used for these classes, given that
(a) the classes are immutable, (b) they are likely to be well used,
and (c) the methods will be unaffected by operator overloading AFAICT.
Broadly speaking, there are two naming conventions for immutable
classes in Java (JDK and beyond):
1) Basic - add/subtract/multiply/divide/negate
Used by BigDecimal/BigInteger
2) Past Tense - plus/minus/multipliedBy/dividedBy/negated
Used by Java-Time
It won't surprise anyone if I say I prefer option 2 - I also think it
has better readability:
1) var result = a.add(b).divide(c.subtract(d));
2) var result = a.plus(b).dividedBy(c.minus(d));
Away from numeric methods, the classic example is sort() vs sorted().
sort() is generally assumed to mutate the collection, whereas sorted()
would be assumed to return a sorted version of the original.
>From what I can see, the prototype Float16 class is using option 1. Is
this a firm decision?
thanks
Stephen
More information about the valhalla-dev
mailing list