Skip to main content

The Horstmann Brace Style

Posted by cayhorstmann on June 28, 2010 at 3:31 PM PDT

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) {
   x = -x;

Another one is named after Eric Allman:

if (x < 0) 
   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/ | 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?

Related Topics >>


I think the horstmann style is the only sane brace style in ...

I think the horstmann style is the only sane brace style in an insane world. What would be cool is if this page had instructions on how to configure the code formatters in eclipse and intellij to horstmann.

you can also collapse the trailing staircases of closing ...

you can also collapse the trailing staircases of closing braces to make an exceptionally compact style, though there is a bit of trompe l'oeil at the end, because the right-most closing brace actually closes the left-most block -- but it looks nice and saves a LOT of lines that otherwise have just one closing brace on them. Example

for (...)
{   if (...)
    {   while(...)
        {   if (...)
            {   blah blah blah ...
}   }   }   }

brace style

The K&R style is better because it tightly couples the block to the statement. There is no doubt that the block and the statement are one unit.

The other two styles only loosely couple the block to the statement. It is not as clear that the block and the statement are one unit.

Try putting an extra semicolon at the end of the first line.

For the K&R style:
if (x < 0) {;
x = -x;
There is no change in the meaning because the block is tightly coupled with the statement.

For the other two styles:
if (x < 0) ;
x = -x;
The meaning changed because the block is only loosely coupled with the statement. You left your defense wide open.

When I was young and naïve I used the Altman style and I was fanatic about using it. At the turn of the millennium I saw the light. Now that I am old I can't stand the Altman or Horstmann styles. But I do like Horstmann's books; if he used the K&R style his books would be perfect.

Allman obviously. Everything

Allman obviously. Everything else is just obfuscation.

Brace indentation style

I've been programming since 1965 (yup, 45 years). When I started writing C/C++ fifteen years ago, it took me about a week to realize that "Horstmann style" was the only one that made sense. How on Earth can you quickly match braces with the K&R style? Isn't programmer productivity important? When browsing through code and skimming logic, don't you want to tell at a glance where blocks start & end?

As for Allman style, why waste an extra blank line? Screen real estate is still valuable. The fewer wasted lines there are, the less page up/page down the programmer is forced to do, and the fewer distractions to his/her concentration.

Horstmann style - anything else is second-rate.

I think the K&R style is

I think the K&R style is still superior. Copying code by lines is easier. Braces may not match, but it is enough that "if" and the closing brace match. People who forget the opening brace at the end of the if-line do not deserve any help.
That is the penalty if one uses a language which uses curly braces instead of "begin" and "end" as God in His incarnation of Niklaus Wirth has intended us to do after He gave his prophet the tables of PASCAL...
Greetings from Kiel,

I like Allman too, but Horstmann is good for books, I think.

I prefer to Allman style because I think it's clear.
And I think it doesn't need to save lines for we all have a high resolution display and good coders seldom write a method too long, so we need only focus on a small piece of codes every time, and make it clear is more useful.

But, for your situation, i think the Allman style is the worst one, because we need to save our planet and trees, so I think we could use K&R or Horstmann style in books.

Hey, i've got an idea, could we just try to count how many pages we could save if we use Horstmann style in the Core Java books, it may be a considerable number for the more than 1800 pages books.

I could get used to it

I used to use the Allman style way back when I was writing MFC apps, but I have been a die-hard K&R fan since switching to Java (which I first learned from Core Java 1.1 volumes 1 & 2)!  :-)

I still prefer the K&R style, but I see the benefits of the Horstmann style and could certainly get used to it.


Other professors at SJSU

Professor Beeson uses the Horstmann style I think. Also, Professor Smith at San Jose State had by far the most different style: for(...) { if(...) { foo()..} } There is a full example here: I've certainly never seen anyone ever use anything like that before Prof. Smith. Crazy kids these days with their brackets... --Glenn

K&R Rules

Hi, I dont think I would use your style. I like short and compact methods. I often move whole lines or blocks of code and chances are high that with Horstman Style you break your code by moving accidentially also the opening bracket. I may also interfere with Refactor -> extract method that I use often. So K&R is a perfect match for me. But yes for example code in a book it would be something fresh and I think you should use it for your books again. Have fun, - Bernd

I use it

I use Horstmann style in my lectures. I picked it up from your Java Concepts, 1st edition. Braces that line up are important for students and compact code is important for my slides. For programming exercises I use Allman style, so students will see what happens in the real world. They are free to choose whatever style they like. And for non-teaching programming I use K&R. If IDEs would support Horstmann style I suppose I would switch. Floris

First, congrats for having

First, congrats for having branded your own style! Personally, I can't stand the K&R - for me it's really hard to read. My standard is the GNU style, with braces on their own lines and indented by two spaces. It's important for me to have blank lines - they show the 'rythm' of the code and clearly put in evidence control structures. Overall, it's the global aesthetical effect of the code that drives my formatting choices. It's some wasted space? Well, it's also a further reason for keeping that method short.

Nice job, but what about the else?

My best wishes for pushing the brace line-up style. Great way to fend off that silliness of the { at the end of the if line. I've had several times when a Jr. developer (coder) has asked for help in finding a bug, to which they are sure that it's a bug in the compiler. ;) one look at their style and I say, "line the braces up, and see if that draws out your issue". I think the "wastes a line" argument anymore is ludicrous with 23" monitors at 2600x1990 res and you have 500 lines on your screen...what's 15 extra lines, I mean with the font most coders use you can't read it anyway. :) So back to the point, what about the "else"? I've been using this format, is this your style? if(xxxxxxxx) { // sometimes I put code or comments here x = y; }else { // sometimes I put code or comments here too y = x; } Congrats on the Wikipedia reference. =Shawn

Too weird

Why is it advantageous to have braces line up?

IMO the "if" and "}" are important, being the beginning and ending of the construct. The "{" is not important, so hiding it at the end of a line is a good thing.

My other objection to the Horstmann style is that the first statement of the block looks different from the others. It shouldn't -- a block is a list of statements, all equally important. A consequence of treating the first statement differently is that if you decide to move it later in the block or delete it, it's a more complicated editing operation than it should be.

Code conventions

In an open source world, I really do think we should all bite the bullet and use the coding style set forth to us by our language overlords. For instance I personally prefer tabs to spaces, but these days I always use spaces either because the language style guides (in python) say I should or because they limit what you can do with tabs (java code conventions specifically state tabs should 8 spaces, not 4). So, since the java code conventions advocate K&R style, I would use K&R style. Most open source projects and a lot of the in-house projects students are likely to encounter will be using some minor variant of this style, and they will be well helped by being able to read it fluently. If they've seen a lot of Horstmann style code, it'll take them that much longer to adjust to the K&R style in new code. (personally I like K&R because it's slightly easier to move code lines around with it than Allman or Horstmann style, but it's just personal preference)


Dr. Horstmann, it's a little too weird for me. It's an elegant way to blend the two brace styles, making compact code that is easy to trace. Personally, I prefer the K&R style because you can use that line for 3 things - the function signature (or block definition), the opening bounding brace, and a simple comment to explain its purpose or caveats for the rest of that line. If you have short blocks several levels deep, the Allman style pushes the closing braces off the screen, decreasing readability at a higher level than line by line.

Odd coincidence.

I was at an AP workshop today and the presenter was using this style. You'll be happy to know that although I found it totally bizarre and would never use anything but K&R style myself, I did note that it makes a lot more sense than Allman style! ; )

I personally like Allman

I personally like Allman style, and I didnt really notice that in "Horstmann" style you save a line. Also, while using an editor like Eclipse makes it harder to follow horstmann style, I'd probably use it if editors supported it. Running it through a code formatter later is just too much of an hassle, no?