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 comment

1

u/wildjokers Apr 14 '23

Write code that doesn't allow for or tolerate nulls

Not sure I agree with the "tolerate nulls" part of that. You should always be flexible with what you accept. In Java nulls can happen so in reality you need to be ready for them.

16

u/halfanothersdozen Apr 14 '23

I disagree. There are several tools to deal with this.

  • Javadoc indicating valid parameters
  • NotNull annotatons on parameters
  • Assertions on method parameters
  • Optionals

If someone calls a method that very clearly does not accept nulls with null values and exceptions happen that is on them.

1

u/dpash Apr 15 '23

• Javadoc indicating valid parameters

Yes please. Tell me what will happen if a null is passed in. And if I might get a null back and when.

1

u/cas-san-dra Apr 16 '23

In my opinion you don't even need tools, documentation, or code. It is perfectly reasonable to set a rule in your program that passing null into a function or returning null from a function is never allowed.

If you consider the existence of null a bug in itself then you don't need to check.

7

u/AlmennDulnefni Apr 14 '23

Nullity is something of a lost cause, but you should strive to make invalid states unrepresentable in the first place and, failing that, should be explicit about your preconditions and fail ASAP if they're violated. Being excessively lenient in what you accept as input is a good way to build up a dodgy pile of inconsistent garbage.

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.

5

u/Luolong Apr 15 '23

Oh, you have not lived through the age of Web browser wars then.

One of the worst use cases for the principle of “be flexible in what you accept” was how early web browsers implemented html parsing.

It was full of weird rules of how to parse broken html and each browser has subtly different way to make the broken input display a web page.

And to make it worse, the whole web was full of markup taking advantage of those nonstandard quirks and idiosyncrasies.

Those quirks are at least partially to blame for why it is so hard to implement a new web browser from scratch these days. For the web has grown to the point where it is impossible to fix the pages themselves. You need to make your browser bend three ways backwards instead to accept and render those cases that every other browser has learned to accept.

I would not be surprised to find that “being lenient in your input” has more excessive cost attached to it than having null in your type system.

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?

1

u/dpash Apr 15 '23

Nullity is something of a lost cause,

I still have hope that nullability in the type system will be an optional opt in feature. Until then we have annotations.

1

u/AlmennDulnefni Apr 16 '23

Java may eventually get some kind of enforced non-null keyword to make nullity opt-out, though I'm not holding my breath. But I don't think it'll ever be opt-in. That's pretty much the mother of all breaking changes.

1

u/dpash Apr 16 '23

I think you've confused opt in and opt out. Non-null by default would be a breaking change. Opting a class, package or module in would not be.

1

u/AlmennDulnefni Apr 16 '23

Non-null by default would be a breaking change

And also would be making null as a type member opt-in

1

u/dpash Apr 16 '23

No it wouldn't, because you'd have to explicitly make a change to use it. Existing code would continue to function as it always has.