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/
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?