Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2006

Tip: Looking for answers? Try searching our database.

trivial question

Thread view: 
crash.test.dummy - 14 Jul 2006 00:40 GMT
which is more 'politically correct' (or should it be said as more
acceptable):

String myString = "something";

a) if ("something".equals(myString)) {...}
b) if (myString.equals("something")) {...}

To me, it's b) because the a) option is like saying:

int a = 4;

 if (2 > a) {...}

what do you think?
Luke Webber - 14 Jul 2006 00:44 GMT
> which is more 'politically correct' (or should it be said as more
> acceptable):
[quoted text clipped - 11 lines]
>
> what do you think?

It might look nicer, but option (a) has the advantage that it will never
throw a NullPointerException. That's why it's so often used.

Luke
Patricia Shanahan - 14 Jul 2006 01:07 GMT
>> which is more 'politically correct' (or should it be said as more
>> acceptable):
[quoted text clipped - 14 lines]
> It might look nicer, but option (a) has the advantage that it will never
> throw a NullPointerException. That's why it's so often used.

I tend to avoid option (a), for exactly that reason.

If I know null should be treated the same as a mismatch, I prefer to
explicitly test for null so that next time I look at the if, I know I
thought about it:

  if( myString != null && myString.equals("something") )

If, on the other hand, I'm not planning for null myString, I would
rather get a NullPointerException than go on computing as though it were
a string and not equal to "something".

Patricia
Stefan Ram - 14 Jul 2006 01:15 GMT
>If I know null should be treated the same as a mismatch, I
>prefer to explicitly test for null so that next time I look at
>the if, I know I thought about it:
>if( myString != null && myString.equals("something") )

 Possibly, for doubles you might also use:

if( x == x && y == y && x == y )

 so that the next time you look at it, you know that you have
 thought about the possibility for x or y to be java.lang.Double.NaN.
Chris Uppal - 14 Jul 2006 14:15 GMT
> If I know null should be treated the same as a mismatch, I prefer to
> explicitly test for null so that next time I look at the if, I know I
> thought about it:

Yes, good point.  And if I /did/ want to allow null, and for some reason wanted
to avoid the explicit null test, then I'd feel obliged to add a comment to the
"backward" equals() comparison, and so there would be no saving in typing or
readability.

   -- chris
Thomas Hawtin - 14 Jul 2006 16:46 GMT
> which is more 'politically correct' (or should it be said as more
> acceptable):
[quoted text clipped - 3 lines]
> a) if ("something".equals(myString)) {...}
> b) if (myString.equals("something")) {...}

I always go for "something".equals(myString). I don't have to consider
NPEs at that point, so there are less things to think about. I also
think it looks better, because the double quotes are not nested within
an additional set of parentheses. And if you have a number of them, you
can compare the literal strings more easily lined up along the left hand
side.

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.