Monthly Archives: December 2009

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.

Ditch MySQL now


MySQL was “bought” by Sun last year, as you may know. The fact that this is significant sort of illustrates one of the reasons I didn’t like MySQL from the moment I met it in 1997 or so. It’s only like half in the game as far as being free and open source. It is tied way too much to MySQL AB, the company, and the way it is licensed is totally shady. Anyway, now Sun is dying and Larry Ellison is about to scoop it up. Monty, the MySQL guy is whining to the Internet, trying to get the European Commission to block the deal. It’s a sad story, but if Oracle doesn’t buy Sun, they will just need to get bought by someone else or go out of business. The real sad part of the story is the death of Sun, one of the original great Internet companies (my other favorite being SGI which met its fate years ago). In any case, MySQL’s future is dark and cloudy.

That is why I’m just going to go ahead and plug my favorite database, PostgreSQL. You should check it out, it’s awesome. It is way more powerful than MySQL and the licensing is better. It was built from the ground up to be a real database, not some tinkering project like MySQL. If you are running a project, especially an open source one, that is dependent on a database, you should take a serious look at PostgreSQL and consider making it your database of choice.

Always name your constraints in Oracle

In Oracle, when you create a constraint (like a primary key), you can either provide a name or have Oracle generate one for you. I got lazy on a recent project, and had Oracle generate the constraint names for the primary keys on my new tables. Later, I had to come back and change the key structure and it bit me. What I like to do is keep a set of DDL files in my project to update the database schema as it evolves. When you want to change the structure, you create a new DDL file and put in your ALTER TABLE statements. You apply it to the dev database, then when you are satisfied, you apply it to QA and eventually production. This may vary depending how many environments you have, but the idea is a consistent set of database updates across environments.

Anyway, today I had to drop the primary keys and create new ones, so I ended up with statements like this:

ALTER TABLE CRITICAL_COURSE_REQUIREMENT DROP CONSTRAINT SYS_C0031831;
ALTER TABLE CRITICAL_COURSE_REQUIREMENT ADD CONSTRAINT CRITICAL_COURSE_REQUIREMENT_PK PRIMARY KEY (CATALOG_TERM, ACADEMIC_PLAN, SUBJECT, CATALOG_NBR);

As you can see, the DDL is then dependent on the runtime state of the Oracle database at the time the original PK was added. This sucks, because it will end up with a different name in each environment. So, in the new PK, I gave it a name (CRITICAL_COURSE_REQUIREMENT_PK) which will be consistent across environments next time I need to change it.