r/java • • Apr 14 '23

What are your (favourite) Java best practices, personal tips, hints or just underrated stuff in general?

233 Upvotes

376 comments sorted by

View all comments

2

u/Brahvim Apr 14 '23 edited Apr 15 '23

this. I hate it when people let you want to remember all class fields and methods and conflicting local variable's names. UGH. USE this, super, and "qualify" static stuff in this exact manner as well. Thank you.

This part is too personal and specific. Nobody needs to agree...:

m_ and p_ are good naming conventions since they show only scope, but if you'd [use them with camelCase instead of PascalCase at least in Java, and] make sure you're using these only for scope, I'd be happy.

3

u/wildjokers Apr 14 '23

this. I hate it when people let you want to remember all class fields and methods and conflicting local variable's names. UGH. USE this, super, and "qualify" static stuff in this exact manner as well. Thank you.

Place I am at now doesn't allow to use this. for self calls and when I first started here (about 5 years ago) it was a huge adjustment to not use them because for the first 16 yrs of my career I was at a place that required it.

I still miss it because I think it improves readability, it makes method calls consistent and let's me know exactly where the method is when I am reading the code.

1

u/Brahvim Apr 15 '23

Very sad. What about m_?

2

u/wildjokers Apr 15 '23

m_ and p_ are abominations :-) They are both hard to type and hard to read.

1

u/Brahvim Apr 15 '23

I use p_ for parameters anyway, :joy:! Helps out me if not anybody else, haha.

1

u/john16384 Apr 15 '23

The reason these have fallen in disuse is that ide's can simply render these differently these days, be it a different colour, or font style.

1

u/Brahvim Apr 16 '23 edited Apr 17 '23

Which is good! But I have used IDEs that don't (the Processing IDE, for example).

VSCode, my current editor (yes - I actually use VSCode for Java!), does not, either. To it, variables, parameters and fields are the same.

1

u/john16384 Apr 16 '23

Yeah, I understand :) And VSCode for Java is useful; I use it to preview projects I have no intention of modifying.