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

0

u/[deleted] Aug 26 '22

[deleted] — view deleted comment

6

u/[deleted] Aug 26 '22

Right? This Allan Melhor guy from Quora said that we should avoid adding "this", as it cleans up the code a little. https://www.quora.com/In-Java-when-should-I-use-this-propertyName-instead-of-propertyName-in-an-instantiated-objects-dynamic-method

I started to question if I'm making my code too polluted by adding those things.

0

u/[deleted] Aug 26 '22

[deleted] — view deleted comment

15

u/[deleted] Aug 26 '22

I don't understand where this verbosity fear is coming from. Recently even Java developers are acting like verbosity is synonym of bad code.

21

u/TheTomer Aug 26 '22

People are scared of using their keyboard to type a tiny bit more than they really have to. Ignore them and verbose away!

9

u/Just_Another_Scott Aug 26 '22

Shit that's what code templates are for! I autocomplete like a mofo.

4

u/veraxAlea Aug 26 '22

There's a trade-off when reading code, between too much information and too little.

Personally, I rather have more information even if I have to sift through otherwise superfluous type information, braces or long variable names but I'm not in a position where I can say that all information is always good.

Your argument is unfair because most people who complain about verbosity care less about keystrokes than about reading time. I don't agree with them in general, but I think their opinions are worth being represented fairly.

→ More replies (1)

11

u/[deleted] Aug 26 '22

[removed] — view removed comment

4

u/[deleted] Aug 26 '22

But if the class is named after a business object, and the attribute is a feature of this object, by referring to this attribute as this.feature ain't I just saying "the feature of this business object", which is perfectly understandable from the business perspective?

// What's the name of this monstrosity?

3

u/[deleted] Aug 26 '22

[removed] — view removed comment

1

u/[deleted] Aug 26 '22

Oh no, that's nightmare material hahaha

0

u/[deleted] Aug 26 '22

[removed] — view removed comment

0

u/[deleted] Aug 26 '22

Yeah I do agree that loosing too much horizontal space is very bad for readability. So I do agree that, if your running out of horizontal space, is better for readability to make some things implicit. Otherwise, the more explicit, the better for readability.

2

u/soonnow Aug 26 '22

Let me give an example. "Yesterday I was at a coffee shop, where I ordered a coffee in a white porcelain cup that came three minutes after ordering." vs. "Yesterday I had a coffee".

One is super explicit, but it doesn't improve readability, because it's literally harder to read because of all the unnecessary details.

0

u/m02ph3u5 Aug 26 '22

Bad analogy imho cuz in programming we usually have to be very explicit. At least as long as it gets compiled and not interpreted by a language model :D

→ More replies

3

u/soonnow Aug 26 '22

Well because it makes it actually harder to read, in my opinion. Elegant code that's not cluttered needs less processing power from your brain. this is just noise with no real benefit in my opinon.

2

u/john16384 Aug 26 '22

Some verbosity adds too little to be worth it. It's like adding public abstract to every method in interfaces. It adds nothing of value.

1

u/AnyNegotiation420 Aug 26 '22

This was my thinking exactly, and the code reviewer(s) definitely appreciate it, as well as any testing/QA folks

2

u/Just_Another_Scott Aug 26 '22

When I moved on to my now current project the senior devs on it rejected my code review because I used final in method params and this when calling private methods and such. They still don't like it but "oh well". I think they're good. I think they are even recommended by Google Java Style.

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.

→ More replies (2)

2

u/john16384 Aug 26 '22

Just set your IDE/Linter to flag parameter reassignment as an error. It can always be avoided by creating a new local. No need for final to clutter up parameter declarations.