views:

221

answers:

7

To check against null we generally do if (object == null). Instead, would it be nicer to have

if (object is null) {
   // Do something
}

Or

if (object is not null) {
   // Do something
}

It would be more intuitive, i.e. reads more like english.

EDIT : I got lot of answers. Thanks for the people who up voted, although down votes were overwhelming. That's fine, I don't ask questions to increase my points but rather to gain insight into language design, algorithm, etc. They always try to make languages more intuitive and easy to read. How is it perfectly fine in sql, but really ugly in java? Some people thought I wanted to have for all equality checks, e.g. if (a is 5). No, I wanted to see only for nulls. If getting rid 'BEGIN' and END made java so elegant, then I don't know what to say. Java put extends in place of : for sub class. Did that make it more readable, I would think so. Was getting rid of ; in groovy a good idea? I would say yes. Have fun.

+8  A: 

No, it wouldn't be nicer IMO.

Java has its roots in C-like languages - we have braces rather than BEGIN/END, we have == instead of "EQUALS" etc.

If you really find

if (x == null)

and

if (x != null)

to be hard to read, even in the slightest, I would suggest that Java may not be the right language for you. There are more "word-based" languages around, but I see no reason to turn Java into one.

Jon Skeet
-1 for personal attack "Java may not be the right language for you".
fastcodejava
@fastcodejava: That's not a personal attack. It's suggesting that you might be happier on something else. If I posted a suggestion that x86 assembly language should have polymorphism, it would be entirely appropriate for someone to suggest I'd be happier in a much higher level language... this is the same sort of suggestion.
Jon Skeet
כִי נר מצוה ותורה אורודרך חַיים תוכחות מוסרFor conventions are as candle and teachings as light, and the reproof of customary practices a way of life - Proverbs 6:23.אל תוכח לֵץ פן ישנאך הוכח לחכם ויאהבReprove not a scorner/joker as he might hate you, reprove a wise person and he will love you - Proverbs 9:8.
Blessed Geek
+4  A: 

No.

Programmers read != like "is not", and == - "is (equal to)"

The whole question could be rephrased to whether programming languages should use English as their syntax, or should they rely on symbols. I vote for the symbols:

  • there is no problem in interpreting them
  • English isn't (yet) the "language of the world"
Bozho
A: 

If you think = or != are hard to read, what about BrainFuck or Whitespace. Be happy with it, symbols are easy to read for every language.

Bright010957
+1 for mentioning Brainfuck. I was not aware of that.
fastcodejava
A: 

Wouldn't that be introducing a keyword is, which seems to be equivalent to '=='?
Wouldn't that open a syntax hole in Java because people will be asking why can't we do

  1. if (a is b) for int a, int b
  2. if (s is t) for String s, String t

?

If we are allowed to use is in place of equals() or '==', wouldn't that be confusing by providing more than one possible syntax token that can be used for an operation? And then it would further obfuscate matters, as we would not know whether it meant equals() or '=='.

You should use Cobol.

Blessed Geek
Thanks for the suggestions : "You should use Cobol".
fastcodejava
A: 

Java is already verbose enough. Needing to type is not instead of != would be gratuitous. In any case, when testing a reference for nullity, what you are really testing is the value of the reference. This is precisely what == and != are for. The language does not need another construct for this.

uckelman
+1  A: 

No, Java doesn't need this. in Java, == null and != null are unambigous due to the lack of operator overloading.

On the other hand, when you have a language with operator overloading and in which classes are references, you need a separate construct that always means "see whether what these point to is the same". That's why D, for example, has the is operator that compares the addresses that references point to and is not overloadable, and the == operator, which computes a user defined definition of equality.

dsimcha
+3  A: 

Regarding the second part of your question: SQL uses a different operator for checking null-ness because SQL null values behave very differently than Java (or .Net) nulls. While Java holds that null == null, in SQL NULL != NULL therefore you need a separate operator.

David Schmitt