Open Letter to Geertjan Wielenga

Wilfred Springer

Geertjan Wielenga has been trying to pull me back into the NetBeans community for a couple of years in a row now. I admire his perseverance; if this is typical for the whole NetBeans team, then Eclipse is going out of the window some day soon.

There is one thing - really, just one thing - that would make me drop Eclipse immediately in favor of NetBeans. That's having better support for fluent interfaces in the way the IDE formats source code.

Now, I've been working on a couple of fluent interfaces over the last couple of years, and it's just awesome. It will always result in code that is easier to read, and it doesn't cost you a dime; you get the benefits of Java 5 type safety , without sacrificing readability.

Let's take this Pecia example:

            .text("This is a document. Make sure you also check out the ")
            .emphasis("next").text(" section.")
        .para("That's all folks.")

Now, IMHO, this is pretty easy to read and understand. The layout of your code clearly reflects the structure of the underlying document model. However, if you press Command-Shift-F to format your code, this is what you get:

doc.section("Introduction").para().text("This is a document. Make sure you also check out the ").emphasis("next").text(" section.").end().end().section("Conclusion").para("That's all folks.").end().end();

Not quite as good as what we had before.

Now, I would love to have a solution that basically prevented this. I was thinking about this for a while, and I could imagine introducing a couple of annotations for it. Annotations for fluent interfaces. Annotations that basically tell your IDE how to treat different components in your fluent interfaces for formatting.

Maybe annotations on the type of object produced by the section() operation, informing the compiler to treat this object as a "code block", in terms of the indentation. And perhaps an annotation on the .end() method telling the IDE to consider it the end of the "code block".


So, this is to you Geertjan: I solemnly swear to erase Eclipse from my hard disk, as soon as something like this gets implemented in NetBeans. Maybe an annotation based approach is what is needed, maybe it isn't; I don't really care, as long as something gets done about this.

(If anyone else has some thoughts on this, I'd be happy to hear about it.)

Comments (12)

  1. Xavier - Reply

    June 26, 2009 at 12:27 pm

    To do that in Eclipse, I just append a blank comment // to each line to preserve breaks.

  2. Peter - Reply

    June 26, 2009 at 8:46 pm

    How does eclipse handle this?

  3. Peter - Reply

    June 26, 2009 at 10:26 pm

    This is simple 🙂

    ... and this works in NetBeans, too.
    Wilfred, do you make your statement now true?

    "I solemnly swear to erase Eclipse from my hard disk, as soon as something like this gets implemented in NetBeans"

    We give you some time until 6.7. Then you don't need to install NetBeans twice in a week 😉

  4. Peter - Reply

    June 26, 2009 at 10:29 pm

    Ah, stop. The problem is the indent...

    You can hack a bit:

    ((HashMap) new HashMap().//
    /* */clone();

  5. Peter - Reply

    June 26, 2009 at 10:40 pm

    Sorry, for posting again.
    But I think the simplest style could be the following

    ((HashMap) new HashMap().

    1. easy to read
    2. simple to implement (I guess + hope) for the formatter tool
    3. simple to use
    4. easly applied to other scenarios (I had a similar problem with xml in strings ...)

    (The underlines were made only because of this blog software which stripes whitespaces)

  6. Peter - Reply

    June 26, 2009 at 10:42 pm

    ( ... trim whitespaces )

  7. Wilfred Springer - Reply

    June 27, 2009 at 10:05 am

    Using trailing slashes (//) works fine for newline preservation, but I actually want the indent to be ok as well. And ideally it should be kept in sync with the indent set in my IDE. So //#start-verbatim would sort of work, but the IDE wouldn't help me to layout what's inside, and whenever I would change my indentation settings, the IDE wouldn't be able to adjust what's inside the //#start- and //#end-verbatim comments.

  8. Peter - Reply

    June 27, 2009 at 6:50 pm

    Hmmh, I think it is a very rare usecase. So that I think the developers of netbeans won't accept the (high?) implementation effort.

    What else do you prefer in eclipse?
    Maybe I can convince you ... 😉

  9. weeniearms - Reply

    June 27, 2009 at 11:31 pm

    I doubt anyone will consider this as worth the effort of implementing. It's rather impossible to do such a thing without any special annotations in your code and having extra noise just for formatting purposes doesn't seem a good thing either. Nevertheless, removing newline characters in the process of code formatting is rather disturbing. If I break my lines for readability purposes then the IDE should not mess that up for me, especially when it comes to method chaining.

    In such rare cases like the one described in this post I would suggest using groovy and the builders concept. It formats right in almost any IDE and is even more readable then the provided pecia example.

  10. Rob Sessink - Reply

    June 28, 2009 at 11:34 am

    the new Galileo version of Eclipse has an new Formatter option to preserve user line breaks, no sure about the alignment of spacing/tabs

    Please see

  11. Kieron Wilkinson - Reply

    June 28, 2009 at 1:46 pm

    I don't think it is that rare, we do the same thing all the time, like in our XML builder DSL. The Annotation idea is great, though I don't suppose it needs to be annotations, just as long as you can specify the indent/newline behaviour in the DSL classes and not the call site. I guess I'll submit this to the IntelliJ IDEA enhancement requests 🙂

  12. uf - Reply

    December 15, 2010 at 1:42 pm

    You do not need format full text. I format usualy a selected text not to miss my other formating.

Add a Comment