简体   繁体   中英

Why is the Java library source code so strangely indented?

I often rely on the JDK source code to understand how I should implement an interface, and I often find some very strange indentation style at use. For instance, in DefaultCellEditor.java :

public DefaultCellEditor(final JTextField textField) {
    editorComponent = textField;
this.clickCountToStart = 2;
    delegate = new EditorDelegate() {
        public void setValue(Object value) {
    textField.setText((value != null) ? value.toString() : "");
        }

    public Object getCellEditorValue() {
    return textField.getText();
    }
    };
textField.addActionListener(delegate);
}

I'm wondering if this is due to my IDE or not, since I find this kind of indentation quite strange and difficult to read.

Sounds like a tab vs spaces issue. Try setting the tab width to 4 spaces (or 8 if it is 4).

Here is what I see when browsing the OpenJDK code for DefaultCellEditor online.

在此输入图像描述

我怀疑你的IDE忽略或错误解释制表符。

If the indentation is consistent across different methods, then there's probably some (unwritten) reason why it's that way.

If there's no rhyme or reason, then (a) the coder(s) didn't care about indentation, or (b) the coder(s) did care, and something happened en route to your computer. The likely culprit is that not enough attention was given to how tabs and spaces were used to structure the code, and your IDE's indentation settings for tabs are different than those of the last person's who touched the code.

If you can make the whitespace characters visible in your text editor, that should show you if a mix of tabs and spaces was used.

It seems that NetBeans formats the code of the Java Source Code ( src.jar ). I extracted the jar manually and I opened javax/swing/DefaultCellEditor.java using a plain text editor and the indentation is really that bad. So, it is not the IDE.

But why the indentation is that bad; sorry, I don't know.

One possibility is IDE/editor problem on author's side.

Back in my PHP days I used PSPad editor which does something very similar to this - in PSPad indentation looks OK but in other viewers/editors indentation is mangled. AFAIK it has something to do with mixing spaces with tabulators and different tab width.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM