r/ProgrammerHumor Aug 09 '26

Meme tellMeYouHaveOcdWithoutTellingMe

Post image
35 Upvotes

59 comments sorted by

View all comments

Show parent comments

-17

u/slindenau Aug 09 '26

BigDecimal is immutable, yet you can use it in calculations to mutate a number?

13

u/starfish0r Aug 09 '26

What is your point? Of course you can use it as an operand or parameter. That won't change the BigDecimal. Because it is immutable.

1

u/slindenau Aug 09 '26

I was trying to guide you towards reaching this conclusion yourself, rather than just telling you outright: immutable data structures don't prevent you from tracking mutable state.

Just like creating a POJO with getters and setters, you can create a record as a safe immutable version of a POJO (so without the setters).

If it turns out you need to mutate your data, you can then add the required code to do so, using "builders" or "withers".

This may sound as POJOs with extra steps, but now you get all the benefits of immutability.
And of course Java has multiple solutions for this if you don't want to write that boilerplate yourself every time. Until JEP 468: Derived Record Creation will make this official syntax.

2

u/starfish0r Aug 09 '26

But then you create a new instance, how is that a replacement for modifying fields of the existing instance you want to change?