rc3.org

Strong opinions, weakly held

Tag: software development (page 16 of 16)

Spring is a more desirable skill than EJB

The Spring Team blog announces that the Spring framework is now a more commonly requested skill for developers than EJB. About four years ago, I started building applications using Spring and Hibernate, even though much of the industry focus was still on EJB. I thought that the approach that I and a number of other people had started taking was correct, I was a little bit scared about neglecting a skill that so many employers were seeking.

So I went out and bought the book Mastering EJB and read it cover to cover, just to make sure I wasn’t missing something. After reading it, I realized that while I could see that some projects would require EJB, they would not have made writing any application I’d ever worked on easier, nor would they have improved the functionality of any of those applications. So I promptly forgot everything I’d read and continued to use the lightweight libraries.

It’s nice to look back and find myself completely vindicated.

Bloatware afflicts the auto industry

I’m a sucker for observing behavior in non-software industries that’s analogous to perceived problems in the software industry. This is from a blog post on the 2008 Honda Accord:

The day an automaker redesigns a midsize family sedan and declares it to be, “Less roomy, less powerful and less luxurious!” is the day that I expect to read a Rolling Stone “Top 50 Albums Of All Time” story and find “Frampton Comes Alive!” in the top 10.

So it’s of little surprise that the redesigned 2008 Honda Accord is indeed roomier, more powerful and more luxurious. But I wonder – how much bigger can it get? Out of curiosity, I compared the size of the new Accord to that of the Toyota Avalon. Houston, we have a problem.

One of the most common complaints about software is that as applications get older, they keep getting bigger. It turns out, the same thing happens to cars. This must be why car models eventually get retired and new models take their place — nobody ever releases an updated model of a car that’s smaller, cheaper, and more economical to drive. New models get a new designation. Indeed, that seems to be what’s happening with Honda:

But if it gets any bigger or heavier on the next redesign, the Accord might as well be Honda’s Avalon, with the Civic taking up the midsize spot and the Fit being the “old Civic.”

I’m glad to see that this behavior on the part of businesses (and even open source projects) is really more a function of consumer psychology than it is of bad behavior in the software industry.

Use constants instead of string literals

One practice that I internalized a long time ago but that I still see a lot of experienced developers ignore is habitual . Most applications have common strings that are used in a number of places. These should pretty much always be stored in constants (however your language of choice implements them) rather than appearing as string literals anywhere other than when they are initially assigned. For example, a blog publishing tool might have a status field for posts with possible values of “published”, “draft”, and “scheduled”.

If you’re writing Java, you might write code like this:

blogPost.setStatus("published");

and

if (blogPost.getStatus().equals("published")) {

     // do stuff }

The reason this is bad practice (even though it works like a charm) is that it costs you an opportunity to let the compiler do work for you. Let’s say you write your code like this:

public static final String STATUS_PUBLISHED = "published";

blogPost.setStatus(STATUS_PUBLISHED);

if (blogPost.getStatus().equals(STATUS_PUBLISHED)) {
    // do stuff
}

If there’s a typo in that code, the compiler will catch it for me. Typos in string literals are the cause of a huge number of difficult to diagnose bugs, and are easy to avoid. There are other advantages to this approach as well, especially if you use an IDE like Eclipse. If put a string in a constant, I can use the “References” feature in Eclipse to show me every spot in my code where the constant appears. In a couple of seconds, I can discover every place in my application where the status of a blog post is set to “published”. There are all sorts of other advantages as well, like being able to rename the constant using the rename variable refactoring in the IDE as opposed to search and replace.

This practice is on my mind this week because I finally bothered to learn about Ruby constants, which are incredibly easy to declare and enable me to apply the same practice that I always use when programming in Java.

To create the same constant in Ruby, you’d just type:

STATUS_PUBLISHED = "published"

You can, of course, reference it by name, and if it’s declared in a class called BlogPost, you can reference it like this:

if status == BlogPost::STATUS_PUBLISHED
    # do stuff
end

Ruby will happily show you an error message if you reference that constant without declaring it first, so even though Ruby is looser in terms of variable declarations than Java, the compiler will still help you out in situations like this.

PHP also has constants but in my opinion the declaration syntax makes them a pain to deal with. (I still use them when I’m programming in PHP, though.)

Newer posts

© 2025 rc3.org

Theme by Anders NorenUp ↑