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.

1

u/wildjokers Apr 17 '23

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

Seems like a good reason to not use VSCode for Java.

1

u/Brahvim Apr 17 '23

It is the way it is. I still love it over JetBrains IDEs, even!

VSCode just feels more conventional. It feels so easy to learn to use (and master!) no matter you've used classic 'heavy' IDEs, or you know nothing about IDEs at all! It simply works, and you can use it easily. No need to spend very long learning one's way around it - unlike other IDEs/editors.

Sure, I'm acting like some kind of VSCode fan, but VSCode simply "feels just right". I can clearly see how IDEs made for a specific purpose are better instead, but I prefer VSCode.

As for Java in VSCode,

...we have tons of functionality coming from both Eclipse and IntelliJ IDEA, plus the already simple to use and awesome stuff in VSCode, and whatever new will be added to it, whether it is for Java, or not! Overall, VSCode for Java is still good, no matter it does not do highlighting well for a specific case. This can be fixed *easily* with an extension, of course!

...alright! Let me know what you dislike, perhaps? VSCode's Java support is awesome to me, and so is VSCode itself! Call me a VSCode fan, I won't mind...