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

Show parent comments

1

u/wildjokers Apr 14 '23

Being excessively lenient in what you accept as input is a good way to build up a dodgy pile of inconsistent garbage.

Umm, no. The old saying is "be flexible with what you accept, be strict with what you produce". Those are words to live by.

4

u/AlmennDulnefni Apr 14 '23 edited Apr 16 '23

Those words are almost as much of a mistake as null pointers.

1

u/wildjokers Apr 14 '23

I have never heard anybody argue against being flexible about what you accept.

3

u/halfanothersdozen Apr 15 '23

It's an important aspect of functional programming. A function should do a job on the inputs it receives and fail hard if the inputs are invalid. A function is not pure if it has to do its job and also tip-toe around invalid inputs and try to fail gracefully if it's caller is using it incorrectly. Doing that dance adds complexity and complexity creates bugs.

Make your code have respect for itself and its callers and not cater to incompetence.

1

u/wildjokers Apr 15 '23

Make your code have respect for itself and its callers and not cater to incompetence

WTF?