• Home
  • RSS Feed
  • Log in

Open Letter to Geertjan Wielenga
Posted by Wilfred Springer mid-morning: June 26th, 2009

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:

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();

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".

test1

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.)

  • Share/Bookmark

Tags: fluent interface, Java, netbeans
Filed under Java | 11 Comments »



11 Responses to “Open Letter to Geertjan Wielenga”



    Xavier Says:
    Posted at: June 26, 2009 at 12:27 pm

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



    Peter Says:
    Posted at: June 26, 2009 at 8:46 pm

    How does eclipse handle this?



    Peter Says:
    Posted at: 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 ;-)



    Peter Says:
    Posted at: June 26, 2009 at 10:29 pm

    Ah, stop. The problem is the indent…

    You can hack a bit:

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



    Peter Says:
    Posted at: June 26, 2009 at 10:40 pm

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

    //#start-verbatim
    ((HashMap) new HashMap().
    _____clone()).
    __clone();
    //#end-verbatim

    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)



    Peter Says:
    Posted at: June 26, 2009 at 10:42 pm

    ( … trim whitespaces )



    Wilfred Springer Says:
    Posted at: 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.



    Peter Says:
    Posted at: 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 … ;-)



    weeniearms Says:
    Posted at: 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.



    Rob Sessink Says:
    Posted at: June 28, 2009 at 11:34 am

    Hoi,
    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
    http://download.eclipse.org/eclipse/downloads/drops/R-3.5-200906111540/eclipse-news-part2.html



    Kieron Wilkinson Says:
    Posted at: 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 :-)



Leave a Reply

Click here to cancel reply.

Deployment automation for Java application running on Websphere, WebLogic and JBoss

Archives

  • March 2010
  • February 2010
  • January 2010
  • December 2009
  • November 2009
  • October 2009
  • September 2009
  • August 2009
  • July 2009
  • June 2009
  • May 2009
  • April 2009

Xebia Sites

  • Xebia Corporate
  • Xebia France
  • Xebia India

Categories

  • Java (282)
  • Agile (109)
  • General (50)
  • Testing (42)
  • Performance (42)
  • Hibernate (36)
  • Scrum (33)
  • Podcast (31)
  • Architecture (31)
  • Spring (28)
  • SOA (24)
  • Maven (22)
  • Project Management (22)
  • Middleware (23)
    • Deployment (14)
  • Flex (17)
  • JPA (17)
  • Eclipse (15)
  • Xebia Labs (15)
  • Quality Assurance (14)

Tag Cloud

    Agile Lean Groovy Performance Spring Closures product owner Maven Grails fitnesse Scrum XML Scala JavaOne Xebia IntelliJ Semantic Web Functional Programming Agile Awareness Workshop esb Poppendieck Ajax Java qcon Architecture SOA Seam Hibernate Introduction to Agile Testing