The Horstmann Brace Style

There is a brace style with my name attached to it. In this blog post, I describe its merits and why I had to stop using it.

Now here's a topic on which everybody has an opinion—brace styles. One common style is named after Kernighan and Ritchie:

if (x < 0) {
   printf("Negative\n");
   x = -x;
} 

Another one is named after Eric Allman:

if (x < 0) 
{
   printf("Negative\n");
   x = -x;
} 

Each style has a clear advantage. The K&R style uses fewer lines. In the Allman style, the braces line up.

When I first learned to program in C, in the mid 1980s, there were vigorous arguments about this issue. I was confused because it seemed obvious to me how to combine the benefits of both.

if (x < 0) 
{  printf("Negative\n");
   x = -x;
} 

The braces line up. There is no wasted line. Problem solved.

I used this style for about 20 years. It even appeared in a few of my books, including the first couple of editions of Core Java which, according to my editor, sold about as many copies as K&R.

Unfortunately, I was never able to convert many coders (as we were called in the olden days). A typical dialog would be:

Me: “Look at the brace style I invented. Here! See how the braces line up?”

Coder: “No, no, no. When the braces line up, you waste a line. That's called the 'Allman style'.”

Me: “I know what the Allman style is. But I modified it. Look closely. No wasted space!”

Coder: “That's too weird.”

Me: “Are you going to use it?”

Coder: ”No!!!”

Eventually I gave up. I didn't want to fight the indentation engines in Emacs or Eclipse.

Years later, one of my students asked me if I was the Horstmann whose brace style was mentioned in Wikipedia. That was news to me—a brace style with my name? But there it is—my name in pixels! Apparently, at least one other person noticed. I swear I didn't edit that article.

Make that two. A while ago, Jim Pattee, the creator of the fine Artistic Style code formatter, told me that he had an implementation of the Horstmann style. It's now in the official release, and it works like a charm. Try it out: Download and build from source, then run

astyle --style=horstmann < /path/to/src/java/lang/String.java | less

See how the braces line up! And no wasted space!

I am very tempted to go back to this style for the next edition of Core Java. It'll be easy. Eclipse can do its thing, and then I run all the source through astyle.

Should I? Or is it too weird?