Oct 6

Strings are a critical part of the Java language and especially so in a web application. Here are a variety of pointers for dealing with Strings.

Use StringUtils

A lot of the common String operations that aren’t in the JDK library are available in Commons Lang StringUtils. Check out those methods and you’ll find your code looking much cleaner (and you’ll have to write less of it!). Anytime you’re about to write a piece of simple string manipulation code that you think should be in the library, check Commons first.

Concatenating a List<String> or a String[]

Check out StringUtils.join() to see if it’s already done for you! Also, see the discussion below about the different ways to concatenate strings.

Case insensitive comparison

Strings have a .equalsIgnoreCase() method that lets you do what you actually want rather than worrying about case conversions.

Prefer StrBuilder to StringBuffer/StringBuilder

Instead of the JDK StringBu* classes and the associated confusion explained below, try using StrBuilder. Apache Commons has created this very handy class that has a much more powerful and flexible API than StringBuffer. Give it a try and you won’t be able to go back to the regular StringBu* classes.

StringBuffer, StringBuilder and +

Strings in Java are immutable, so to append two Strings together the JVM must create another String. When you do that just once, the performance impact is negligible but if you do it many times (in a loop, or a more complicated piece of code) the cost of all of those intermediate Strings adds up. In order to deal with this problem, the StringBuffer and StringBuilder class were added to the Java core libraries. StringBuffer is threadsafe but slightly slower (because of the synchronization) and StringBuilder is not threadsafe but slightly faster. Both of the builder implementations have the con that they make the code harder to read.

The difference between using StringBu* and the ‘+’ operator is generally trivial, except in loops where it can become a significant performance hit.

As a general guideline, you should use the ‘+’ operator to concatenate simple Strings. The java compiler will actually convert code like:

a = b + c + d;

To look like:

a = new StringBuilder(b).append(c).append(d).toString();

So you get the performance when the code is compiled, and easier readability while the code is still in source. As of 1.5, the compiler can NOT yet optimize away the ‘+=‘ operator.

This also means that if you use + in a loop, then you’re incurring a performance penalty for creating a new StringBuilder in every iteration. They’ll be garbage collected eventually, but its not a good habit to get into. Make sure to declare the buffer/builder outside of the loop:

StringBuilder b = new StringBuilder();
 
for(String: stringList){
    b.append(s);
}
 
return b.toString();

Again, most of the basic use cases where you’d want to do that are actually already implemented for you in Commons Lang StringUtils.

Don’t optimize String concatenations!

This may seem to go against the advice of the rest of this post, but bear with me. Don’t immediately go into your codebase fixing these issues everywhere they crop up. That’s just a waste of time. Use profiling to determine which loops are actually causing slowdowns and fix those. Just keep these tips in mind as you write new code and focus on core readability as a primary requirement.


Oct 1

I just moved this domain over from Bluehost to Webfaction. There’s a variety of reasons for this but mainly it just comes down to the fact that Bluehost has been incompetent just too many times for its own good.

Running a web server is not rocket science. You spin up a distribution, install some standard packages and you have a working web host. Things do go wrong though and that’s what distinguishes companies that know what they’re doing from ones that don’t. Time after time Bluehost has proven that they don’t understand the basics of keeping customers happy. Frankly that’s all I want. The actual product could be crappy and held together with gum and duct tape but I don’t care what it does under the hood. I just want my hosting experience to not make me pissed off and be accessible to the outside world. Here are the main issues and differences that have moved me to switch.

Server Status

Bluehost has one crappy page that shows server status whenever they feel like updating it with useful messages like “Our technician is currently investigating a server problem. The source of this problem is unknown at this time. The duration of assessing this is approximately 15 to 45 minutes.” that show without a timestamp or ever show a resolution. I have yet to hear from them about a single reason for the downtime or resolution. Compare that against Webfaction’s status blog.

Control Panel

Bluehost uses an industry standard, cPanel so there’s not much to fault there. However, the login process that they layered on top of it is completely insane. Not only do you send credentials over HTTP rather than HTTPS, those credentials are then SENT RIGHT BACK TO THE BROWSER in cleartext and then sent for a third time in cleartext to the server. Really?

Webfaction has its own control panel which, while less polished than cPanel, is actually incredibly effective and has some very cool abstractions set up for running multiple applications and sites.

Applications

Bluehost comes with a bunch of generic installable applications which are great if you’re a consumer and don’t really do web development on your own. Webfaction comes with a ton of pre-configured packages for Trac, SVN, Django, Wordpress and a variety of others that are more friendly to the developer/technical person crowd.

File organization

This is my bluehost home directory:

[###@box500 ~]$ ls
access-logs         ioncube  public_ftp      sources   tmp
etc                 logs     public_html     ssl       webalizer.conf
fantastico_backups  mail     sourceguardian  thermopy  www

This is my webfaction home directory:

[###@web41 ~]$ ls
bin  lib  logs  sources webapps

Can you tell which host actually cares about the little details such as not putting random crap in a home directory? Most of the directories in the Bluehost root aren’t ones I’ve created or even touched. It’s similar to Apple’s restructuring of the standard Unix directories to names and a structure that actually *gasp* makes a little more intuitive sense (”what do you mean its not in /usr/share/sbin/etc/lib/? where else would it be?!?”).

Cost

Who cares? Seriously. If you’re paying anything from $6 through $20 a month for web hosting what difference does it make? $14 is one nice lunch a month and that’s really stretching the price differences. I’ll probably end up paying a few bucks more per month for Webfaction but its likely to make me a lot happier.

Mailing Lists

Both hosts use Mailman for managing mailing lists, however cPanel offers an automated way to create/delete them whereas Webfaction does it manually. How often do you actually create/remove mailing lists?

Mail

Because Bluehost can’t figure out a way to deal with spammers or understand that a customer who has been with them for over two years won’t suddenly turn into a spam king they limit the rate of SMTP messages you can send. So if you’re doing a reply-all with 20 of your friends, expect that email to take several minutes to complete sending. This is technical competence right up there with login over HTTP.

The Plug

If you’re reading my blog, chances are that Webfaction is a good host for you. Give it a try.