java代码格式化


Java source formatting

You are probably familiar with the Eclipse hotkeys to automatically format your source code:

  • Correct Indentation: Ctrl + I
  • Format: Ctrl + Shift + F

These are quite helpful and can save a great deal of time. However, with Eclipse you can take the whole idea of automatically formatting the source code to the next level.

Formatter configuration

At first, one can configure the formatting rules in Eclipse preferences > Java > Code Style > Formatter. You can create profiles and customise these to your needs:

Source formatting rules are mostly a matter of taste. I usually stick to the default profiles because that is what most Java programmers are accustomed to. Purists can also choose the pre-configured Java Code Conventions. As negligible as the exact formatting rules are, what matters is that every programmer in the project adheres to the exact same set of formatting rules. Otherwise they will re-format everybody’s code all the time. Seeing the actual changes in the code will consequently be like looking for a needle in a haystack.

So, how can one make sure that everybody uses the same formatting rules? It is easy: You can configure a project-specific formatter in the project properties under Java Code Style > Formatter:

These settings are saved in a .settings folder in the project folder. You can add this folder to your version control system to make everybody use the exact same formatter settings:

project
`-- .settings
	|-- org.eclipse.core.resources.prefs
	|-- org.eclipse.core.runtime.prefs
	|-- org.eclipse.jdt.core.prefs
	`-- org.eclipse.jdt.ui.prefs

Also, it’s highly recommended to configure the text file encoding and the line delimiter for the project under Resources. This is especially important for projects with developers using different operating systems with different default encodings. I generally recommend UTF-8 and Unix line endings, but again, that is a matter of taste. As long as everybody uses the same settings, everything will work out fine:

Save Actions

So, what’s next? It’s time for save actions! Eclipse can automatically apply source code formatting and additional code cleanups whenever you save a source file. You can activate this feature in the project properties under Java Editor > Save Actions:

This way, whenever you save a source code file the following steps are performed:

  • the code is formatted
  • imports are organized
  • the final modifier is added to fields that are only set in the constructor
  • fields that overwrite a subclass method are annotated with @Override: When a subclass changes and doesn’t offer an overwritten method anymore, you get a compiler warning. This is a highly recommended cleanup as it makes your code more robust against changes in library APIs.
  • unnecessary casts are removed

The most apparent advantage is that everything is consistently formatted. In my experience, consistent formatting makes it much easier to read existing source code.

Again, the save action configuration is saved in the .settings folder and applies to all users if you add these files in your projects version control system (you can also configure these just for your workspace in Eclipse preferences > Java > Editor > Save Actions).

Trailing whitespaces

To perfect source code formatting when it comes to versioning the files, we need to take care of one last detail: trailing whitespaces. I never cared about this until I had to merge a large amount of code for a project with many parallel branches. Here is the problem: An empty line is different from an “empty line with a tab character”. This is also true for white spaces after source lines. Especially if developers use different editors with varying policies regarding to white space handling (most indentation is automatically added while typing), this can cause a lot of trouble when merging code:

The general solution for this problem is to strip all trailing/unnecessary whitespaces. In Eclipse you can do this with the Any Edit plug-in. This removes all trailing whitespace automatically:

Summary

All this might sound negligible, however in my experience, the effort to figure this out and to properly configure it pays off perpetually whenever you write or change a line of code. Happy formatting!


本文转自:http://www.ralfebert.de/archive/java/source_formatting/

优质内容筛选与推荐>>
1、(收藏)《博客园精华集》HTML/CSS部分筛选结果(共90篇)
2、Python初学者应了解的技巧
3、创建一个vue-cli项目
4、web服务器
5、[Oracle整理]Oracle之Procedure参数类型


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号