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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s