Skip to main content

Fix the indentation in the JDK source code.

14 replies [Last post]
ebeneezer
Offline
Joined: 2004-12-03

Many parts of the Java source code that ships with the JDK are very poorly formatted with respect to indentation. Take a look at the Basic UI classes for starters (BasicScrollPaneUI, BasicInternalFrameUI, etc.).

The lack of indentation makes the code very hard to follow and this always gives me a bad headache, especially when developing things in Swing which frequently requires knowing the JDK source code.

It's such an easy thing to run the source code through a formatter to improve its readability, so I think this is little effort, big gain.

Of less importance, opening many of the JDK source files in a decent editor (like IDEA) always reveals a load of redundant imports in the JDK source code that could easily be tidied up.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
cowwoc
Offline
Joined: 2003-08-24

+1

Easy to do, way overdue.

Part of the "open-source" process is that your source-code has to be of higher calibur than most, so others find it easy to read and understand it. Cleaning up the JDK code will do Sun as much good as the rest of the community.

jsando
Offline
Joined: 2003-06-22

I agree it would be nice, although it looks just fine in IntelliJ or Textpad with the proper tab settings. Most open source projects just use spaces to keep it simple.

As for comments, I've spent a few hours poking around javac and its pretty well commented.

tsinger
Offline
Joined: 2003-06-10

> it looks just fine in IntelliJ or Textpad with the proper tab settings.

There is no proper tab setting at all. It's an individual setting like the color of your desktop. Source code should like nice aligned independent of the tab settings.

Different people prefer different indentations (== tab settings) - even in the same project. Hence there exist a smart-tab option in IDEA.

But in the case of the JDK, it would be enough, when it uses just spaces, because this is the cheap alternative to tab-settings-independent good-enough readable code.

> Most open source projects just use spaces to keep it
> simple.

Most open source projects' source looks horrible, because from time to time there are still some tabs in it. (Same might apply to closed source as well, but I cannot see it.)

Tom

rexguo
Offline
Joined: 2004-06-21

+1

I think equally important is having
more comments. I was very surprised
to find the lack of comments especially
on the 'internals' code that is not
part of the public API. Comments for
these code are critical to the understanding
of the codebase and why things are
implemented in a particular way.
An extra minute spent on writing an
enlightening comment will save a lot
of time for the tens of thousands of
developers like us so that we can
effectively help fix bugs and provide
useful feedback.

.rex

zander
Offline
Joined: 2003-06-13

I never had too many problems with it, really. Just set your tab stop to 8 spaces and your indent to 4. Or get a mature editor (like vim) if yours can't do that :P

tsinger
Offline
Joined: 2003-06-10

> Just set your tab stop to 8 spaces and your indent to 4.

This is thought very selfish. Good code looks nice with every tab-size. Hence (only?) IntelliJ IDEA has the smart-tab-feature (. is a space, -> a tab):

This example looks nice with 8 spaces per tab:
[code]------->public void test(Foo foo
------->.................Bar bar) {
------->------->/* do something */
------->}[/code]
... or with 3 spaces per tab:
[code]-->public void test(Foo foo
-->.................Bar bar) {
-->-->/* do something */
-->}[/code]
> Or get a mature editor (like vim) if yours can't do
> o that :P

Does it support the smart tabs like shown above? If not, put it in the trash and use a mature IDE(A) :P

Tom

zander
Offline
Joined: 2003-06-13

> > Just set your tab stop to 8 spaces and your indent to 4.
>
> This is thought very selfish. Good code looks nice
> with every tab-size.

Asking sun to re-indent megabytes of source is not?

> > Or get a mature editor (like vim) if yours can't do
> > o that :P
>
> Does it support the smart tabs like shown above? If
> not, put it in the trash and use a mature IDE(A) :P

Vim is the sort of application that you can find new features in for the rest of your live, so I'd have to answer "probably". I personally use all-spaces; when I type 1 tab expands to 4 spaces, and when I read a file 1 tab is 8 spaces. This seems to work for almost all source files. And where it does not work, those guys mostly put a code-format line at the bottom which is then parsed and your editor automatically adjusts to the setting.

The rule for tabs/spaces/indent is (and this comes from years watching flamewars) that there is no rule, just a good default. Which I found to be the one I described above.

I may not agree with your choice of indenting, but I will do my best to defend your right to use it :)

yishai
Offline
Joined: 2003-11-16

> Asking sun to re-indent megabytes of source is not?

They don't have to use it that way themselves, they just have to run it when they distribute it.

> The rule for tabs/spaces/indent is (and this comes
> from years watching flamewars) that there is no rule,
> just a good default. Which I found to be the one I
> described above.
>
> I may not agree with your choice of indenting, but I
> will do my best to defend your right to use it :)

Sure, but Sun can use spaces. It is not a request that it have my coding style, just that it use spaces instead of tabs so that it have a consistent-looking coding style.

tsinger
Offline
Joined: 2003-06-10

> ... that it have a consistent-looking coding style.

And this coding style must be independent of any characters-per-tab option of the IDE. So there are two choices:
a) use only spaces or
b) use smart tabs.

And since SUN surely does not use an IDE, which supports smart tabs, only spaces should work.

Tom

mgrev
Offline
Joined: 2003-08-12

+1

A no-brainer.

monika_krug
Offline
Joined: 2004-10-14

+ 1.

The mixture of spaces and tabs messes everything up, and in Eclipse I cannot even reformat the source code that comes up when navigating to classes that belong to the JRE and not to me.

Monika.

yishai
Offline
Joined: 2003-11-16

+1. It is so easy to do too. Just fire up IDEA on the source tree, and reformat code. ;)

OK, use NetBeans if they must ...

tsinger
Offline
Joined: 2003-06-10

+1

Taking a look at the JDK sources in IDEA drives me nuts because of the random mixture of tabs and spaces and the erroneous JavaDocs.

Tom

kcpeppe
Offline
Joined: 2003-06-15

Good think Idea has a good file formatter ;)