3 ms·
OK, I'll bite. The third option is obviously wrong. The first one should instead have been "if (StringUtils.isNotEmpty(s))". But it seems like you prefer to ty
by Viliam1234 7y ago
OK, I'll bite. The third option is obviously wrong. The first one should instead have been "if (StringUtils.isNotEmpty(s))".
But it seems like you prefer to type repetitive code that is more difficult to read, rather than use a function, if that function is in a library. Did I get that right? Is that because the repetitive code is relatively short (it gets longer if the variable has a longer name), or because there is the extra line required for the import?
Apache Commons is the "things that should have been in Java SE, but are missing for mysterious reasons" library. I can barely imagine a project that wouldn't benefit from some of its functionality. (Of course, there is always the alternative option of reinventing the wheel.)
By the way, with static imports the code can be reduced to "if (isNotEmpty(s))".
- commandlinefan 7y agoOk, I’ll bite back. Yes, “StringUtils.isEmpty” is materially worse, in every way. It’s not really any more readable: the first time you see it, you ought to check to make sure that is just checking to see if the pointer is non-null and the string is 0-length to make sure that it’s not doing something else, like trimming whitespace, which you may not want. You’re adding a few hundred kilobytes of dependency plus an extra unnecessary function call, just to make it less clear what your program is doing. If you use static imports, it’s even less readable because it’s not at all clear where this mystery function came from: I hope you’re lucky enough to be able to import this code into an IDE and click-through the function to see what it is and what it does. Maybe if it was just this one function it wouldn’t be so bad, but this bit of pointlessness ends up scattered everywhere.