Appearance
良好的代码格式化可以提高代码可读性和可维护性。
使用 4 个空格作为缩进,不要使用 Tab:
// 正确:4 空格缩进 public void method() { if (condition) { doSomething(); } } // 错误:Tab 或不统一 public void method() { → if (condition) { → → doSomething(); → } }
public class Example { // 类的成员之间用一个空行分隔 private String name; private int age; public Example() { } public void method1() { } // 方法之间用两个空行分隔 public void method2() { } }
// 正确:运算符两侧加空格 int sum = a + b; if (a == b) { } for (int i = 0; i < 10; i++) { } // 错误 int sum=a+b; if (a==b) { } for (int i=0;i<10;i++) { } // 正确:逗号后面加空格 method(arg1, arg2, arg3); // 正确:分号后面加空格(在 for 循环中) for (int i = 0; i < 10; i++) { } // 错误:逗号/分号前面加空格 method(arg1 , arg2); for (int i = 0 ; i < 10 ; i++) { } // 正确:方法调用不加空格 method(arg1, arg2); // 错误 method (arg1, arg2); // 正确:括号内侧不加空格 if (condition) { } method(args); // 错误 if ( condition ) { } method( args );
左大括号与声明同行,右大括号单独一行:
public void method() { if (condition) { doSomething(); } else { doOther(); } } // 类和接口 public class MyClass { public interface MyInterface { void method(); } }
单行语句可以省略大括号,但不推荐:
// 不推荐:省略大括号 if (condition) doSomething(); // 推荐:始终使用大括号 if (condition) { doSomething(); }
建议每行不超过 120 个字符:
// 正确:拆分长行 public void longMethodName(String parameter1, String parameter2, int parameter3, int parameter4) { // ... } // 正确:使用局部变量减少行长度 String fullName = user.getFirstName() + " " + user.getLastName(); log.info("User {} logged in from {}", fullName, ipAddress);
// 正确:基于最后一个点换行 List<String> filtered = users.stream() .filter(u -> u.isActive()) .map(User::getName) .collect(Collectors.toList()); // 正确:基于操作符换行 int sum = firstValue + secondValue + thirdValue;
// 正确:4 空格缩进 public void method( String param1, String param2, int param3) { // ... } // 正确:参数对齐 public void method(String param1, String param2, int param3) { // ... }
import java.io.IOException; import java.util.List; import java.util.concurrent.CompletableFuture; import javax.servlet.http.HttpServlet; import org.springframework.boot.SpringApplication; import org.springframework.context.annotation.Bean; import com.example.model.User; import com.example.service.UserService;
可以使用通配符导入同包的多个类,但不要导入过多的类:
// 正确 import java.util.*;
一个文件中通常只放一个公共类。类的声明顺序:静态变量 → 实例变量 → 构造器 → 方法。
public class UserService { // 静态变量 private static final Logger log = LoggerFactory.getLogger(UserService.class); // 实例变量 private UserRepository userRepository; // 构造器 public UserService(UserRepository userRepository) { this.userRepository = userRepository; } // 公共方法 public User findById(Long id) { return userRepository.findById(id); } // 私有方法 private void validate(User user) { // ... } }
// 正确:每行声明一个变量 private String name; private String email; private int age; // 错误:一行声明多个变量 private String name, email, address; // 正确:声明时初始化 private int count = 0; private List<String> list = new ArrayList<>();
同类方法按以下顺序排列:公共方法 → 保护方法 → 私有方法 → getter/setter
public class Service { // 公共方法 public void publicMethod() { } // 保护方法 protected void protectedMethod() { } // 私有方法 private void privateMethod() { } // getter/setter public String getName() { return name; } public void setName(String name) { this.name = name; } }
// 正确:参数名有意义 public List<User> findUsersByAgeAndCity(int age, String city) { // ... } // 正确:参数过多时换行 public void createOrder( String orderId, String userId, List<OrderItem> items, BigDecimal totalAmount, String remark) { // ... }
// 正确:简单的 if 可以一行 if (condition) return; // 正确:标准格式 if (condition) { doSomething(); } else if (anotherCondition) { doOther(); } else { doDefault(); } // 错误:嵌套过深 if (condition1) { if (condition2) { if (condition3) { doSomething(); } } }
// 正确:标准 for 循环 for (int i = 0; i < list.size(); i++) { process(list.get(i)); } // 正确:for-each 循环 for (String item : items) { process(item); } // 错误:循环内操作过多 for (int i = 0; i < 100; i++) { User user = new User(); user.setName("User" + i); user.setAge(i % 100); user.setEmail("user" + i + "@example.com"); user.setPhone("1234567890" + i); user.setAddress("Address " + i); userRepository.save(user); }
代码格式化规范
良好的代码格式化可以提高代码可读性和可维护性。
缩进与空格
缩进
使用 4 个空格作为缩进,不要使用 Tab:
2
3
4
5
6
7
8
9
10
11
12
13
空行使用
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
空格使用
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
大括号
K&R 风格(推荐)
左大括号与声明同行,右大括号单独一行:
2
3
4
5
6
7
8
9
10
11
12
13
14
大括号可选情况
单行语句可以省略大括号,但不推荐:
2
3
4
5
6
7
8
行长度
建议每行不超过 120 个字符:
2
3
4
5
6
7
8
9
换行
方法链换行
2
3
4
5
6
7
8
9
10
参数换行
2
3
4
5
6
7
8
9
10
11
12
13
14
import 语句
顺序
2
3
4
5
6
7
8
9
10
11
通配符
可以使用通配符导入同包的多个类,但不要导入过多的类:
2
声明规范
类和接口
一个文件中通常只放一个公共类。类的声明顺序:静态变量 → 实例变量 → 构造器 → 方法。
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
变量声明
2
3
4
5
6
7
8
9
10
11
方法规范
方法顺序
同类方法按以下顺序排列:公共方法 → 保护方法 → 私有方法 → getter/setter
2
3
4
5
6
7
8
9
10
11
12
13
14
方法参数
2
3
4
5
6
7
8
9
10
11
12
13
14
控制结构
if-else
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
for 循环
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20