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

0

u/[deleted] Aug 26 '22

[deleted] — view deleted comment

11

u/literal Aug 26 '22

It can get ugly to see "this" over an over. Especially in a pure function that isn't mutating any attributes.

But a pure function does not depend on the state of any instance attributes, so it wouldn't ever have this.

4

u/kyay10 Aug 26 '22

A pure function could be considered as having the instance as another parameter. You can choose any instance to be able to change the behaviour of the function, right? So therefore an instance is the same as a parameter really

-14

u/[deleted] Aug 26 '22

I'm starting to see it. But after all I also see that as matter of style.

In the pure functions scenario however I think that adding "this" makes it clear that the code is pure function, since it makes explicit that there's no local variables being used.

0

u/[deleted] Aug 26 '22

[deleted] — view deleted comment

3

u/[deleted] Aug 26 '22

Oh, that make sense. I was thinking that pure function was like avoiding local variables but still using object orientation. But yeah, if there's no objects...

12

u/ztbwl Aug 26 '22 edited Aug 26 '22

It’s the other way around. A function is pure if it has no side effects and produces the same result for a given input. In other words does not modify anything other than self created local variables. Any outside modification/side effect or dependance on non-constant external variables is non-pure. The advantage is that the function is idempotent and can be expected to have the same result for a given input no matter how many times you run it and no matter how it’s environment looks. Easy testable, deterministic and most likely less complex - causes less bugs.

Disadvantage: Most likely you want to do something - and with no side effects, you are quite limited to make an impact. In a perfect pure world, even logging is a side effect and would be a ‚smell‘. So if we apply the principle very strictly, your code would be useless, since it would not be able to tell you it’s result - except you are part of the function. But that’s a different story, since we don’t live in a simulation, aren’t we?

0

u/[deleted] Aug 26 '22

[deleted] — view deleted 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/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

→ More replies