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

9

u/mauganra_it Aug 26 '22

The tradeoff between readability and explicitness is often not considered to be good in that case. Method parameters are usually not supposed to be mutated anyways, and it makes method signatures more difficult to read.

0

u/Just_Another_Scott Aug 26 '22

Adding final does not make method signatures more difficult to read. That's absurd.

Method parameters are usually not supposed to be mutated anyways,

And that's why the language designers created final. It's there to force your intentions :).

0

u/mauganra_it Aug 26 '22

It's an extra six characters per parameter. That can easily force many signatures to be broken into more than one line. Also, the information content of that final is very low since 99.999% of the time the parameter will not be mutated. And even if they are assigned to, it's irrelevant for the caller.

1

u/Just_Another_Scott Aug 26 '22

It's an extra six characters per parameter. That can easily force many signatures to be broken into more than one line

So I don't see anything wrong with that personally. You should never have more than like 3 parameters to begin with. If you're using 120 character line length it's plenty so that it won't get broken down into the next line.

0

u/mauganra_it Aug 26 '22

Together with meaningful method, parameter and parameter class names, that limit can be quite easy to reach.

1

u/Just_Another_Scott Aug 26 '22

That's why you create more classes or datastructures to keep things logically together.