• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String literal on the right.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I also prefer

exactly because the null check isn't necessary in that case.
 
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unscientific then - String literal on the right.
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic