Category Archives: java

Apache Commons

If you are a Java developer, and you don’t know about Apache Commons, you should take a minute to check it out.  A lot of times when you need to write some mundane code to do something, and you are thinking to yourself, “I’m sure a million other people have had to write this code”, you should take a quick look at commons to see if they did it.  A lot of times they have it packaged up in a nice library.  One of my favorites is the IOUtils class in commons-io, which can pull an InputStream into a String object in one line of code, but they have a bunch of other stuff that is useful as well.

Advertisements

Maven is like apt-get for Java

When I first learned Linux, I used Slackware 3.0.  In those days, if you wanted to install extra software, you pretty much had to compile it yourself.  And they didn’t even have configure scripts back then, if I recall correctly.  Eventually, Redhat took over, and software was easier to install via RPM packages.  At first, RPMs weren’t available for everything, so you still had to compile some stuff, but eventually they became pretty ubiquitous.  Dependencies could be a big pain though.  You download one RPM you want, and you find out you need to download five more that it depends on.  And those five each depend on five others.  Remember rpmfind.net?  Then came yum and apt-get, and those trouble were alleviated.

Now, building Java apps is almost like managing a Linux system.  There are so many great third party libraries available, you can find ones to do almost any common task you need, saving lots of time when coding stuff.  But, the people that write those libraries also use other libraries.  Things like Hibernate have a lot of dependencies.  Getting all of the jars for the libraries you need can be somewhat like the old RPM hunt.  This is where Maven comes in.  Just tell it you need Hibernate and it will make sure you have everything else you need.  It’s pretty awesome, and it can completely eliminate those sessions where you waste a lot of time looking for jar files.

Spring JDBC is awesome

I would like to just take a moment to express my love for Spring JDBC. I just deleted giant swaths of database connection handling code from an app I’m maintaining by replacing a DAO with a Spring JDBC version. If you haven’t taken the time to learn about it, check it out here.

Sometimes I feel like constructors should be removed from Java

Sometimes constructors in Java are convenient. You can instantiate your class with all the data you need with a simple one-liner:

Car car = new Car(numberOfWheels, color, engine, doors);

That code is fairly readable and concise, but things can quickly spiral out of control. Unless you use really descriptive variable names like those above, it’s pretty easy to get lost. Eventually you end up with code like this (partially obfuscated to protect the innocent):


foo = new Bar(rs.getString(1),
rs.getString(2),
rs.getString(3),
rs.getString(4),
rs.getString(5),
rs.getString(6),
rs.getString(7),
rs.getString(8),
rs.getString(9),
rs.getString(10),
rs.getDate(11),
rs.getDate(12),
(“Y”.equals(rs.getString(13)) ? true : false),
(“Y”.equals(rs.getString(14)) ? true : false),
(“Y”.equals(rs.getString(15)) ? true : false),
(“Y”.equals(rs.getString(16)) ? true : false),
(“Y”.equals(rs.getString(17)) ? true : false),
(“Y”.equals(rs.getString(18)) ? true : false),
(“Y”.equals(rs.getString(19)) ? true : false),
rs.getTimestamp(20),
rs.getTimestamp(21),
rs.getString(22),
rs.getString(23),
rs.getString(24));

This is the point where I say you gotta just use a blank constructor and setter methods. Much more readable. And on a side note, when you are typing rs.getString(24), you should probably start thinking about how it might be more readable if you refer to your result set columns by name. That is all.

A recipe for trouble

Dear the Internet,

Please never write this code:

if (value == null || value.equals(“”)) {
d = new Date(0);
} else {

This is what I like to call “bug hiding”. Rarely does a person really mean 12/31/1969 when they pass you a null or blank value. That is all.

JSP is still really bad

I had to do some maintenance work recently on a JSP app. Normally, I avoid JSP like the plague because my (dated) experience with it has been mostly bad. The app I had to maintain was a stone age thing full of scriptlets, but I did a little research and used JSTL to do some of the work on the updates. I was pretty impressed with how cleanly you can do some flow control stuff now. So, I started some new development on a new project with JSP as a proof of concept to see if it is a tolerable programming environment now. It isn’t. For the real basics, there has been a lot of improvement, but some of the mechanisms for taglibs, dealing with variable between servlet/JSP, etc. are still very clunky. And today I encountered this. If you forget to put the return type when defining a function within your app, you get a super mysterious error.


I guess you could blame the implementation (Jasper), but what does anyone else use? Also, you can sort of tell from the screen shot of the editor, but the JSP editor in the current version of Eclipse is pretty awful. It displays some of your double quotes as single quotes, which is really awkward.

Miscellany

Here’s a good example of some bad user interface design in my favorite IDE.


I know the Maven stuff is a plugin, so I’m guessing NetBeans allows plugins to install tabs under “Miscellaneous”. So what you end up with is a configuration screen that looks simple until you find out that there are really supposed to be 30 tabs instead of 7, but they are hiding out in the Miscellaneous section.