Do you use StringUtils.EMPTY
instead of ""
?
I mean either as a return value or if you set a the value of a String variable. I don't mean for comparison, because there we use StringUtils.isEmpty()
Of course not. Do you really think "" is not clear enough ?
Constants have essentially 3 use cases:
None apply here.
StringUtils.EMPTY
. It makes it clear that the use of the empty String is intended, and not some sort of laziness ("Oh, this requires a String, let's pass ""
"). If someone hits this piece of code, he'll think twice before making changes. Also, if StringUtils.EMPTY
were defined as your own variable, like MyClass.EMPTY
, making changes to "that representation of emptiness" would require changing one line of code. You could, for example, change it to "<empty>"
instead of the empty String ""
. But, I think this is going a bit too far.
EMPTY
doesn’t bear any meaning which the empty string itself doesn’t already have. Most notably, it doesn’t document why you decided to use an empty string at that particular case. It’s not different to naming a constant ONE
and pretending there was a point in using that constant instead of the value.
I use StringUtils.EMPTY
, for hiding the literal and also to express that return StringUtils.EMPTY
was fully expected and there should return an empty string, ""
can lead to the assumption that ""
can be easily changed into something else and that this was maybe only a mistake. I think the EMPTY
is more expressive.
""
to be more expressive than using StringUtils.EMPTY
, and what you've said hasn't changed my mind on that. I can just about believe that developers have incorrectly written an empty string literal very, very occasionally - but I'll take the clarity of the string literal over the once-in-a-million (and easily found in testing, hopefully...) bug, personally.
Jul 27, 2018 at 18:05
No, just use ""
.
The literal ""
is clear as crystal. There is no misunderstanding as to what was meant. I wouldn't know why you would need a class constant for that. I can only assume that this constant is used throughout the package containing StringUtils
instead of ""
. That doesn't mean you should use it, though.
If there's a rock on the sidewalk, you don't have to throw it.
I'm amazed at how many people are happy to blindly assume that "" is indeed an empty string, and doesn't (accidentally?) contain any of Unicode's wonderful invisible and non-spacing characters. For the love of all that is good and decent, use EMPTY whenever you can.
I will add my two cents here because I don't see anybody talking about String
interning and Class initialization:
String
literals in Java sources are interned, making any ""
and StringUtils.EMPTY
the same objectStringUtils.EMPTY
can initialize StringUtils
class, as it accesses its static member EMPTY
only if it is not declared final
(the JLS is specific on that point). However, org.apache.commons.lang3.StringUtils.EMPTY
is final, so it won't initialize the class.See a related answer on String interning and on Class initialization, referring to the JLS 12.4.1.
StringUtils
class.
I don't really like to use it, as return "";
is shorter than return StringUtils.EMPTY
.
However, one false advantage of using it is that if you type return " ";
instead of return "";
, you may encounter different behavior (regarding if you test correctly an empty String or not).
""
also, I hate to type the same literal string more than one time, even once only sometimes. I'd rather declare constants in Constants.java, but not repeat them everywhere in source code.
return "";
is ugly, I PREFER to use return StringUtil.EMPTY
(declared in my own class StringUtil, NOT Apache's StringUtils).
If your class doesn't use anything else from commons then it'd be a pity to have this dependency just for this magic value.
The designer of the StringUtils makes heavy use of this constant, and it's the right thing to do, but that doesn't mean that you should use it as well.
I find StringUtils.EMPTY
useful in some cases for legibility. Particularly with:
Ternary operator eg.
item.getId() != null ? item.getId() : StringUtils.EMPTY;
Also by using a constant, a reference to StringUtils.EMPTY
is created. Otherwise if you try to instantiate the String literal ""
each time the JVM will have to check if it exists in the String pool already (which it likely will, so no extra instance creation overhead). Surely using StringUtils.EMPTY
avoids the need to check the String pool?
StringUtils.EMPTY
constant is resolved at compile-time.
Jan 2, 2014 at 7:46
StringUtil.EMPTY
is a compile-time constant, a reference to it gets compiled to exactly the same bytecode as using ""
directly. Further, I don’t see why the ternary operator should make any difference. It’s an expression like any other. Any reason to use or not to use a named constant applies to the ternary operator as well.
No, because I have more to write. And an empty String is plattform independent empty (in Java).
File.separator
is better than "/" or "\".
But do as you like. You can't get an typo like return " ";
someString.isEmpty()
instead.
Honestly, I don't see much use of either. If you want to compare egainst an empty string, just use StringUtils.isNotEmpty(..)
StringUtils.isNotEmpty(..)
also does a nullcheck so it's not exactly the same as comparing with an empty string.
Nov 4, 2010 at 10:08
null
? as a 2nd argument of equals
, but the result will be the same - false
isNotEmpty
is the opposite of "".equals(…)
, hence, the fact that it will treat null
like the empty string is different to comparing with an empty string, "".equals("")
→true
, "".equals(null)
→false
, StringUtils.isNotEmpty("")
→false
, StringUtils.isNotEmpty(null)
→false
. If you just want to know whether a string is empty, use string.isEmpty()
, which has the right behavior of returning true
iff it is an empty string and throwing a NullPointerException
if the string is null
…
I am recommending to use this constant as one of the building stones of a robust code, to lower the risk of accidently have nonvisible characters sneak in when assigning an empty string to a variable.
If you have people from all around the world in your team and maybe some of them not so experienced, then it might be a good idea to insist on using this constant in the code.
There are lots of different languages around and people are using their own local alphabet settings on their computers. Sometimes they just forget to switch back when coding and after they switch and delete with backspace, then text editor can leave some junk inside of "". Using StringUtils.EMPTY
just eliminate that risk.
However, this does not have any significant impact on the performance of the code, nor on the code readability. Also it does not resolve some fundamental problem you might experience, so it is totally up to your good judgement weather you will use this constant or not.
Yes, it makes sense. It might not be the only way to go but I can see very little in the way of saying this "doesn't make sense".
In my opinion:
It will still require changing everywhere if you don't define your own variable and use it in multiple places.
you are gonna change an empty string to a different empty string?