aspose file tools*
The moose likes Java in General and the fly likes null == SomeObject ?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "null == SomeObject ??" Watch "null == SomeObject ??" New topic
Author

null == SomeObject ??

Raj Chila
Ranch Hand

Joined: Mar 18, 2004
Posts: 128

if(null == aSomeObject)
//throw some Exception...

I never did such coding, but while going through some source code I have seen this.

pros and cons?
zmarak ahmad djan
Greenhorn

Joined: Sep 02, 2004
Posts: 20

[ September 10, 2004: Message edited by: zmarak ahmad djan ]

zmarak (publican)
Sheldon Fernandes
Ranch Hand

Joined: Aug 18, 2004
Posts: 157
You typically find such checks done at the beginning of public methods to validate the arguments passed to the method (pre-conditions that need to be fulfilled in order that the method may execute meaningfully). Usually the method throws an IllegalArgumentException if the arguments are not as expected.

I think this approach is called 'Programming by Contract'. The users of the method are expected to know what are 'valid' arguments (usually specified in the javadoc) and since a RuntimeException is thrown the caller does not need to handle it.

Sheldon
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Originally posted by RajaniKanth Bhargava:
if(null == aSomeObject)
//throw some Exception...

I never did such coding, but while going through some source code I have seen this.



Have you done this before



it is something like same
Raj Chila
Ranch Hand

Joined: Mar 18, 2004
Posts: 128

Hi,

nice to see the inputs, but I pretty well know the usage of it....but I was rather looking for some answers that would highlight the need for such syntax rather than the standard human readable coding? as suggested by adeel that is
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Are you just asking why someone would put the "null" first, instead of the variable? There's no technical reason at all for doing this. Someone may have an argument for why they like it stylistically, but I think most folks would agree with you that putting the variable first is nicer.


[Jess in Action][AskingGoodQuestions]
Julian Kennedy
Ranch Hand

Joined: Aug 02, 2004
Posts: 823
The style where a null or literal is put first in an expression is a C/C++ idiom. Its purpose is to avoid subtle logic errors involving the incorrect use of the = operator where use of == is intended. For example:

This comes about as, in C/C++, the result of an expression is treated as false if it evaluates to zero and as true if non-zero. In Java the result of such an expression must be a boolean or the code will not compile. This type of compilation error is a right royal PITA as I have found to my cost on more than one occasion in the past! Happily not since making the transition to Java.

An analogue of this type of style in Java is the comparison of a string object to a literal with the equals() method:

Jules
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874

i have never thought if it. its really simple it never comes into my mind buddy. wht i used to do is make a utility function like,

String getAbsoluteString(String string);

which returns me empty string when there is null.

thanx again jules

[ September 13, 2004: Message edited by: adeel ansari ]
[ September 13, 2004: Message edited by: adeel ansari ]
Raj Chila
Ranch Hand

Joined: Mar 18, 2004
Posts: 128

Hi all,

Thanks a lot for your valuable input julian and adeel.

Adeel : what you said also makes sense, but what if I really want to check if a String is null? your String.equals says true or false if you are checking the Absolute Value....
More often than not I would check if the input Variable is null or not could have a greater impact on the overall system. Your suggestion is best if you are bothered if the input String is "something"...but imagine if there are more strings to be compared (which is generally the case)....then you would probably run the whole routine before you go ahead with the reporting of that result rather at the first instance of checking if the string is null and immediately returning.

But never the less, Thanks a lot for your inputs.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: null == SomeObject ??