aspose file tools*
The moose likes Beginning Java and the fly likes Quick poll: if (str.equals( Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Quick poll: if (str.equals("mystring")) vs if ("mystring".equals(str))" Watch "Quick poll: if (str.equals("mystring")) vs if ("mystring".equals(str))" New topic
Author

Quick poll: if (str.equals("mystring")) vs if ("mystring".equals(str))

John Digweed
Greenhorn

Joined: Sep 10, 2007
Posts: 2
Quick, unscientific poll.

Which of these do people prefer?

1. Variable name on the left; string literal on the right.

2. String literal on the left; variable name on the right.

Assume the variable has already been null checked.

Thanks
John
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

String literal on the right.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11497
    
  16

my 2cents...

if by "the string is null-checked", you mean in both cases you have a null check, i prefer 1. it seems more natural to me, probably because that's how i first saw it years ago.

having said that, the whole point of #2 is you don't NEED to do the null check. So if it's a question of 1 with a null check, or 2 without it, then #2.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Hey, we forgot to throw out a "welcome to the ranch!" Welcome aboard!

I see this all the time:

and it makes my little discomfort alarms go off. I prefer

And more than that, I prefer knowing that s cannot be null. When I'm comfortable with that the more conventional s.equals("hello") reads better for most folks.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14430
    
  23

I also prefer

exactly because the null check isn't necessary in that case.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Srikanth Basa
Ranch Hand

Joined: Jun 06, 2005
Posts: 241
Unscientific then - String literal on the right.
John Simpson
Ranch Hand

Joined: Nov 28, 2005
Posts: 30
It depends.
If I'm interested in whether s is null or whether it's equal to the string literal, then I'd go for a seperate null check and the literal on the right.
If I don't care if s is null, all I'm interested in is whether it equals the literal, then string literal on the right.
On a purely personal note, I prefer the literal on the right but that's just habit.

John
Adam Nace
Ranch Hand

Joined: Jul 17, 2006
Posts: 117
Personally, I'm more in favor of variable.equals("literal"), because it seems more logical to me when I read it.

The intent is rarely to see if two strings are the same, but rather, to see if the variable holds some specific significant value (if that makes sense to any of you). To do that, we check to see if they are the same.

But to me, when I see "literal".equals(value), I think to myself, why am I checking the value of "literal". I know its value. My intention is to check the value of variable. So I prefer to see variable to the left, because it reads as 'if variable is equal to "literal"', which is what I want.

Having said that, "literal".equals(value) means we don't have to null check. Some people think this is a good thing. Personally, I'd rather see the null check, because it tells me that it is possible that this variable is null. If I don't see the null check, I don't know if there's a possibility that the variable is null.

- Adam
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Jesper Young:
...exactly because the null check isn't necessary in that case.

Yes, in the real world. But for this poll, I thought we were to assume that "the variable has already been null checked."

(Ask a silly question...)
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

"John Digweed,"

Welcome to JavaRanch! Please check your private messages by clicking on My Profile. Thanks!
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14430
    
  23

Originally posted by marc weber:

Yes, in the real world. But for this poll, I thought we were to assume that "the variable has already been null checked."

(Ask a silly question...)

Even then I would still prefer the string literal on the left version, just to make my code look consistent.
Mark Garland
Ranch Hand

Joined: Nov 11, 2006
Posts: 226
I personally prefer 1, but IDEA (my IDE) always winges and isn't happy unless I override the warning or admit defeat and use 2.


28/06/06 - SCJP - 69%, 05/06/07 - SCWCD - 92%, 28/02/08 - IBM DB2 v9 Fundamentals (Exam 730) - 87%, 18/11/08 - IBM DB2 v9 DBA (Exam 731) - 89%, 26/02/11 - SCBCD - 88%
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Quick poll: if (str.equals("mystring")) vs if ("mystring".equals(str))