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/

java代码格式化的更多相关文章

  1. Eclipse中设置Java代码格式化

    一.自定义 Java 代码格式化 [Java-Code-Formatting.xml 下载],下载完毕以后,打开 Eclipse 找到如下图界面,点击 Import 导入即可.

  2. Java代码格式化规范实践总结

    目标说明 统一良好的代码格式规范可以有效提升开发团队之间的「协作效率」,如果不同的开发团队或者开发人员采用不同的代码格式规范,那么每次Format代码都会导致大量的变化,在Code Review及Me ...

  3. MyEclipse配置默认自带的Java代码格式化

    1.配置注释一大段代码块后,格式化代码,已经注释的代码仍然保持原样 (1)首先,Window -> Prefrences -> Java -> Code Style -> Fo ...

  4. IDEA java 代码格式化统一

    Intellij idea 安装格式化插件 ECLIPSE CODE FORMATTER:1,安装插件:网络安装:选择Setting =>Plugins=>Browse repositor ...

  5. eclipse 4.5.2 源码修改 格式化Java代码

    注:本文代码基于eclipse4.5.2 1. 需求:在换电脑之后,如何不用配置eclipse就可以很快进入开发呢,并保持原来的编码规范. 2. 方法:修改eclipse源码 分别修改了两个jar包2 ...

  6. 转!!Java代码规范、格式化和checkstyle检查配置文档

    为便于规范各位开发人员代码.提高代码质量,研发中心需要启动代码评审机制.为了加快代码评审的速度,减少不必要的时间,可以加入一些代码评审的静态检查工具,另外需要为研发中心配置统一的编码模板和代码格式化模 ...

  7. Java代码规范、格式化和checkstyle检查配置文档

    http://www.blogjava.net/amigoxie/archive/2014/05/31/414287.html 文件下载: http://files.cnblogs.com/files ...

  8. 【Java】Eclipse代码格式化-代码模板

    Eclipse代码格式化-代码模板 eclipse 代码模板_百度搜索 编码规范系列(一):Eclipse Code Templates设置 - 青葱岁月 - ITeye博客 善用Eclipse的代码 ...

  9. Java 字符串格式化详解

    Java 字符串格式化详解 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 文中如有纰漏,欢迎大家留言指出. 在 Java 的 String 类中,可以使用 format() 方法 ...

随机推荐

  1. 【KM算法】HDU2255-奔小康赚大钱

    KM算法的裸体.O(n^4)的模板,实际上在增广路径的时候依然有冗余,可以用bfs优化到O(n^3). #include <iostream> #include <cstdio> ...

  2. Java学习笔记(5)

    补day4:如果一个函数的返回值类型是具体的数据类型,那么该函数就必须要保证在任意情况下都保证有返回值.(除了void类型) return关键字的作用: 1.返回数据给函数的调用者 2.函数一旦执行到 ...

  3. Android Studio 下载地址 & SDK 更新教程

    Android Studio 下载地址: http://www.android-studio.org/ SDK 更新教程: http://www.androiddevtools.cn/ SDK 推荐使 ...

  4. 130804组队练习赛ZOJ校赛

    A.Ribbon Gymnastics 题目要求四个点作圆,且圆与圆之间不能相交的半径之和的最大值.我当时想法很简单,只要两圆相切,它们的半径之和一定最大,但是要保证不能相交的话就只能取两两个点间距离 ...

  5. 基于tiny4412的u-boot移植(二)

    作者信息 作者:彭东林 邮箱:pengdonglin137@163.com QQ: 405728433 平台介绍 开发环境:win7 64位 + VMware11 + Ubuntu14.04 64位 ...

  6. Telnet技术白皮书

    转:http://www.cnpaf.net/Class/Telnet/200705/19978.html Telnet的应用不仅方便了我们进行远程登录,也给hacker们提供了又一种入侵手段和后门, ...

  7. 转载:win10 下安装Oracle 11g(问题:环境不满足最低要求)

    安装oracle11g时遇到INS-13001环境不满足最低要求 在以管理员身份运行setup.exe之后,出现了:[INS-13001]环境不满足最低要求,通过网上搜索之后找到了解决途径 首先,打开 ...

  8. 地图投影与ArcGIS坐标系转换

    1. 通常GIS项目涉及到的坐标系 (1)面向局部区域的大比例尺二维平面:高斯投影(横轴墨卡托) 说明:在市一级的小范围区域的GIS系统,比如规划局.国土局.建设局的系统,大都使用高斯投影,以便与地方 ...

  9. java基础之:java注解

    一:元注解 元注解的作用就是负责注解其他注解.Java5.0定义了4个标准的meta-annotation类型,它们被用来提供对其它 annotation类型作说明.Java5.0定义的元注解: 1. ...

  10. angular学习(二)—— Data Binding

    转载请写明来源地址:http://blog.csdn.net/lastsweetop/article/details/51182106 Data Binding 在angular中.model和vie ...