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.)
Trying to grok the mobile phone generation
I keep reading that the computing platform for the younger generation is the mobile phone. For me and my generation, it’s the personal computer. I see the mobile phone mostly as an annoyance. I loathe texting, I don’t really care for voice mail, and I certainly don’t want to talk on the phone unless I absolutely have to.
Reportedly other people don’t see their phones that way. They text constantly, use their phones to surf the web, and generally use the phone as their communications platform of choice. I don’t get it, but there you go.
I will be watching with interest to see whether Twitter takes off and which sorts of people use it. It’s a service that enables you to send text messages from a mobile phone to a central service which then passes them along to your subscribers. Naturally you can subscribe to other people as well. So if you and your five friends all joined and subscribe to one another, you could text a message to Twitter saying, “headed 2 bar after work” and your friends would be notified automatically. In other words, totally useless to me but perhaps intriguing to others.
I’ll be watching to find out whether that’s the case.