20030122 Wednesday January 22, 2003

Commons Lang: StringUtils My new favorite method is the equals method on Commons Lang's StringUtils class. It takes the check for null out of your logic and can help you create cleaner code. Before using it you might have to write something like this:

if (request.getParameter("checkbox") != null 
    && (request.getParameter("checkbox").equals("true"))) {

With StringUtils.equals(String, String), you get a little less coding:

if (StringUtils.equals(request.getParameter("checkbox"), "true")) {

If you're using Struts or some other library that already depends on commons-lang, why wouldn't you use it? Possibly performance reasons, but I doubt it causes much of a hit. Posted in Java at Jan 22 2003, 06:05:51 AM MST 4 Comments

Comments:

Couldn't you just do this? : if ("true".equals(request.getParameter("checkbox"))) { I guess if you're comparing two variables, instead of one varable and one constant(the "true" is never null), the StringUtils.equals is useful.

Posted by James Chochlinski on January 22, 2003 at 06:17 AM MST #

Just dug into commons-lang a bit more and it is really amazing to see the similarity to the code you normally write to get things done. I am now looking through my own code to replace it with commons-lang. I normally use the code that James offers though for equals :)

Posted by Martin van den Bemt on January 22, 2003 at 08:03 AM MST #

And not to be outdone, the isEmpty( str ) and isNotEmpty( str ) methods are handy replacements for the common ( str != null && str.length() > 0 )

Posted by Chris Winters on January 22, 2003 at 08:58 AM MST #

This is very handy indeed. Didn't looked at it before but i'm gonna use it for sure

Posted by Danman on January 22, 2003 at 09:54 AM MST #

Post a Comment:
  • HTML Syntax: Allowed
Click me to subscribe
Matt Raible is a Web Architect who enjoys developing applications with open source technologies. Contact me for rates.
« December 2008
SunMonTueWedThuFriSat
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
   
       
Today

Recent Entries

Tag Cloud