0

I just finished my Master degree, and I had a really technical teacher which always told us:

The oracle should always be at the left of equalities and comparisons

As I am French, the exact sentence would be:

L'oracle doit toujours être à gauche des égalités et comparaisons

The word 'oracle' refers here to the constant, so instead of writing:

mystring.equals("foo"); we should write "foo".equals(mystring);

I never had the cleverness to ask him why, as I just listened to him about technical advices. I feel quite dumb right now, so there is my question:

Aside for a style point of view, is there any technical reason, such as optimization or performance or anything like that, to write the constants at the left of equalities/comparisons ?

Thank you in advance !

Yassine Badache
  • 1,810
  • 1
  • 19
  • 38

1 Answers1

0

There are two reasons in "constant to the left of a comparison."

s.equals("x")

Can throw a NullPointerException

And in other languages using == one cannot mix it up with assignment =

if (x = 3) {

Java already is guarded against the second case, and the general practice nowadays is to avoid null values. So the argument is weak. However in my current job we indeed follow that rule.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138