Blog

Open Letter to Geertjan Wielenga

26 Jun, 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:
[java]
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();
[/java]
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:
[java]
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();
[/java]
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.)

guest
12 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Xavier
Xavier
12 years ago

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

Peter
12 years ago

How does eclipse handle this?

Peter
12 years ago

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
12 years ago

Ah, stop. The problem is the indent…
You can hack a bit:
((HashMap) new HashMap().//
clone()).//
/* */clone();

Peter
12 years ago

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
12 years ago

( … trim whitespaces )

Wilfred Springer
12 years ago

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
12 years ago

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
weeniearms
12 years ago

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
Rob Sessink
12 years ago

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
Kieron Wilkinson
12 years ago

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 🙂

uf
uf
11 years ago

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

Explore related posts