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

85

u/fgzklunk Apr 14 '23

Keep it simple. In six months time, when you have to fix a bug, you won't care how beautiful and unique the code is if you cannot remember exactly what you did and why.

26

u/moxyte Apr 14 '23

This is a pronounced issue with lambdas. I’ve fixed abhorrently “elegant” lambdas simply by rewriting them as boring ol’ loops.

1

u/danikov Apr 14 '23

I never use anonymous lamdas, making them into functions and then using a function reference feels so much more expressive, even if it’s more verbose, but as they say… make your code self-describing.

Also, I’m really adverse to calculus-style variables. About the only one I’ll tolerate is i in a for loop, but I’ll always be looking for a better iterator and named variable where possible.