冒险选择非标准的Java代码缩进样式?

gla*_*den 3 java coding-style indentation

如果选择非标准缩进样式会有所不同吗?

这是我经常看到的风格:

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class Test {
    static public void main(String args[]) throws Exception {
        FileInputStream fin = new FileInputStream("infile.txt");
        FileOutputStream fout = new FileOutputStream("outfile.txt");

        FileChannel inc = fin.getChannel();
        FileChannel outc = fout.getChannel();

        ByteBuffer bb = ByteBuffer.allocateDirect(1024);

        while (true) {
            int ret = inc.read(bb);
            if (ret == -1)
                break;

            bb.flip();
            outc.write(bb);
            bb.clear();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

但我更喜欢这种风格,一切都从下一行开始:

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class Test
{
    static public void main(String args[]) throws Exception
    {
        FileInputStream fin = new FileInputStream("infile.txt");
        FileOutputStream fout = new FileOutputStream("outfile.txt");

        FileChannel inc = fin.getChannel();
        FileChannel outc = fout.getChannel();

        ByteBuffer bb = ByteBuffer.allocateDirect(1024);

        while (true)
        {
            int ret = inc.read(bb);
            if (ret == -1)
                break;

            bb.flip();
            outc.write(bb);
            bb.clear();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我发现这更容易阅读,但如果我使用这种风格,我会遇到与他人合作的问题吗?

Tho*_*sen 6

  1. 确定团队中的单一约定(最好是标准的约会,以防您以后想与他人合作)
  2. 配置您和所有人的 IDE以使用该格式.
  3. 使重新格式化自动发生,最好是每个Ctrl-S.

这将使所有源始终保持一致,并确保源存储库中的更改是实际更改,而不仅仅是稍后重新格式化.

对于Eclipse,这可以通过配置格式化程序(我碰巧喜欢默认值)来完成,并保存首选项,然后可以由其他人加载.此外,Java - > Editor - > Save操作允许在每个Ctrl-S上自动重新格式化,这也是一个可爱的首选项.

我发现上面有一个额外的启发式

  • 一切都必须适合一条线

给出了很多自动触发的重构,给出了许多命名的本地,然后通过命名捕获意图,这反过来又适用于调试,因为你通常在单步执行时在调试器中显示更多的变量值,并且你倾向于每行的NullPointerExceptions机会较少.


编辑: 我在我的博客上写到这一点.


编辑2014-08-19:似乎如果将Eclipse格式化程序设置保存到文件,IntelliJ IDEA可以使用该文件格式化源.