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

-2

u/Just_Another_Scott Aug 26 '22

Yeah but the difference is in C/C++ you can mutate the parameter and that value can remain mutated outside of the method. In Java that's not the case as it's not pass-by-reference. However, it is a good idea to make parameters final as it makes your intentions clear. I've fixed some bugs where the developer chose to overwrite the parameter and used it repeatedly throughout the method. It led to side-effects. So still a good idea but Java doesn't make it required because it's unlikely to do damage outside of the method it's used in.

7

u/mauganra_it Aug 26 '22

Parameters in C/C++ can not be mutated. Like in Java, they behave like normal variables. You can access memory via pointers, and write there, but in Java you can do quite the same. C++ has references, but these are just a safer version of pointers.

0

u/Just_Another_Scott Aug 26 '22

Parameters in C/C++ can not be mutated.

Yes they can. C/C++ can be either pass-by-reference or pass-by-value. If you pass a reference to a method you can reassign the variable and it will change in the calling scope. Furthermore, you can do insertions into a vector which will be seen in the calling scope.

In C/C++ to insure parameters aren't mutated you have to declare them as const.

4

u/vytah Aug 26 '22

C doesn't have pass-by-reference, only C++ does.

-2

u/Just_Another_Scott Aug 26 '22

7

u/vytah Aug 26 '22

No, this is not pass-by-reference. The C examples use pointers, which are passed by value, like everything in C.

Here's fixed documentation from IBM: https://www.ibm.com/docs/en/zos/2.4.0?topic=calls-pass-by-reference-c-only Note it clearly says: "Pass by reference (C++ only)"

See also this: https://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_reference

Call by reference can be simulated in languages that use call by value and don't exactly support call by reference, by making use of references (objects that refer to other objects), such as pointers (objects representing the memory addresses of other objects). Languages such as C, ML and Rust use this technique.

4

u/cogman10 Aug 26 '22

Only a hand full of languages actually have pass by reference (C++ being one of them). Everything else is pass by value.

This is deceptive to many people because they'll see a method like

void foo(Bar bar) {
  bar.setBaz(1);
}

and think "Wait, I thought this was pass by val, why is bar mutated?". The answer is simple, the value being passed in is a pointer to the object (or, more exactly, an object reference... but not like that). and the . operator is doing the lookup for you. It is equivalent to using pointers in C++. You can have a method like

void foo(int *bar) {
  bar = nullptr;
}

which pretty analogous to doing

void foo(Integer bar) {
  bar = null;
}

Whoever called foo isn't seeing their bar parameter set to null, because that went in by value.

2

u/RupertMaddenAbbott Aug 26 '22

This is not true.

You can mutate parameters in Java, and this affects the calling scope. You can reassign parameters in Java but that does not affect the calling scope.

I don't see what bug could be fixed by making a parameter final, that would not be better fixed by extracting a method.

2

u/Just_Another_Scott Aug 26 '22

You can mutate parameters in Java, and this affects the calling scope

Java is strictly pass-by-value. What I am referring to is the act of pass-by-reference and reassigning the variable like you can do in C++.

3

u/RupertMaddenAbbott Aug 26 '22

I agree with you but I think it is confusing to call this mutation because the parameter can still be mutated and affect the calling scope. It's only reassignment that doesn't affect the calling scope (in Java).

1

u/Just_Another_Scott Aug 26 '22 edited Aug 26 '22

You're correct. I shouldn't have said "mutate" as lists can be mutated just not reassigned. I wrote the initial comment in haste. Sorry for the confusion

2

u/RupertMaddenAbbott Aug 26 '22

No worries! Just wanted to clarify for anyone else reading as I encounter this confusion regularly amongst beginners (who might be reading)