• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
John Digweed
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String literal on the right.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12196
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15480
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I also prefer

exactly because the null check isn't necessary in that case.
 
Srikanth Basa
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unscientific then - String literal on the right.
 
John Simpson
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"John Digweed,"

Welcome to JavaRanch! Please check your private messages by clicking on My Profile. Thanks!
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15480
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic