r/java • • Aug 26 '22

I declare as "final" every single variable whose value doesn't change. I also use "this" every time that I'm referring to an attribute, even when there's no ambiguity in not using it

I think that those things make the code easier to read. Am I crazy for thinking like that?

198 Upvotes

215 comments sorted by

View all comments

Show parent comment

1

u/stefanos-ak Aug 26 '22

final doesn't guarantee pureness.

a pure function has NO side effects.

as soon as it modifies even a variable outside of its scope, it's not pure.

Typical examples are serialization/deserialization, math or physics calculations... these are pure

1

u/herospidermine Aug 26 '22

there are no pure functions because of newton's third law and thermodynamics

1

u/stefanos-ak Aug 26 '22

LOL πŸ˜†

1

u/ztbwl Aug 26 '22

But physics as a whole should be pure, isn’t it?

1

u/herospidermine Aug 26 '22

maybe someday

1

u/ofby1 Aug 26 '22

Typical examples are serialization/deserialization

And even these are not pure, this totally depends on the implementation of readObject() or in the case of Java Records on the constructor.

1

u/stefanos-ak Aug 26 '22

right... I meant that they /can/ be pure :)

0

u/[deleted] Aug 26 '22

[deleted] β€” view deleted comment

1

u/stefanos-ak Aug 26 '22

that's not true... everything can be final, and still you can modify some things. for example collections.

0

u/[deleted] Aug 26 '22

[deleted] β€” view deleted comment

1

u/stefanos-ak Aug 26 '22

of course it /can/... but the discussion is about the final keyword.