Search code examples
javanaming-conventionsimmutability

How should method names change when I turn a mutable object into an immutable object?


Lets say I have a class Size.

With the mutable version it feels natural to go

size.add(2);

But the immutable feels better like this

newSize = size.plus(2);

I'm fairly new to immutable objects. What is the naming convention for this and other methods?


Solution

  • That is asking for opinions, not for facts.

    You can let yourself be guided by the BigDecimal class for the naming convention:

    http://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html

    But if you want to use "plus" in stead of "add" - why not? Its your code and if you find that readable then just do it.

    EDIT: please be aware that I did not notice your stackoverflow history before giving this answer. I would have probably chosen to shut up if I had noticed you are not a novice :)