好代码写作的三个重点
2023-11-08 14:33:53
1.清晰:命名变量、函数和类
通常阅读别人的代码时,我们通过变量、函数和类来理解程序意图,所以本质上变量、函数和类 是程序员和程序逻辑之间的接口。因此,当你为变量、类和函数命名时,如果使用不清晰、不可的名称,你实际上是在混淆任何读代码的程序员的程序逻辑。
“Clear and expressive …
// bad
private static String a = "abc";
private static String b = "def";
private static String c = a + b;
// good
private static final String FOO = "abc";
private static final String BAR = "def";
private static final String FOO_BAR = FOO + BAR;
2.简洁:使用短而有意义的名称
变量、函数和类的名称越长,就越难阅读和理解。因此,应尽量使用短而有意义的名称。
“Brevity is the soul of wit.”
// bad
private static final String firstName = "firstName";
private static final String lastName = "lastName";
private static final String emailAddress = "emailAddress";
// good
private static final String fn = "firstName";
private static final String ln = "lastName";
private static final String email = "emailAddress";
3.表达:让名称其用途
变量、函数和类的名称应该能够描述它们的作用。这样,其他程序员就可以更容易地理解代码。
“The best code is self-documenting.”
// bad
private static void doSomething(String input) {
// ...
}
// good
private static void calculateFoo(String input) {
// ...
}
4. 实践:使用一致的命名约定
在整个代码库中使用一致的命名约定可以使代码更易于阅读和理解。这可以帮助其他程序员更快地找到他们需要的信息,并减少错误的发生。
“Consistency is key.”
// bad
private static final String FOO = "abc";
private static final String BAR = "def";
private static final String FOO_BAR = "foobar";
// good
private static final String FOO = "abc";
private static final String BAR = "def";
private static final String FOO_BAR = "fooBar";
5. 示例:使用注释来解释代码
注释可以帮助其他程序员理解你的代码是如何工作的。注释应该清晰、简洁,并与代码保持一致。
“Comments are your friend.”
// bad
// This function does something
private static void doSomething(String input) {
// ...
}
// good
/**
* This function calculates the foo of a given input.
*
* @param input The input to calculate the foo of.
* @return The foo of the input.
*/
private static int calculateFoo(String input) {
// ...
}
6. 组织:将代码组织成模块
将代码组织成模块可以使代码更易于阅读和维护。模块可以按功能、类或文件进行组织。
“Organization is the key to success.”
// bad
public class Foo {
public static void main(String[] args) {
// ...
}
private static void doSomething() {
// ...
}
private static void doSomethingElse() {
// ...
}
}
// good
public class Foo {
public static void main(String[] args) {
// ...
}
private static void doSomething() {
// ...
}
}
public class Bar {
private static void doSomethingElse() {
// ...
}
}
7. 方法:使用有意义的函数和类名称
函数和类名称应该能够描述它们的作用。这样,其他程序员就可以更容易地理解代码。
“Meaningful names are essential.”
// bad
public static void doSomething(String input) {
// ...
}
// good
public static int calculateFoo(String input) {
// ...
}
8. 技巧:使用代码注释来提高代码的可读性
代码注释可以帮助其他程序员理解你的代码是如何工作的。注释应该清晰、简洁,并与代码保持一致。
“Comments are your friend.”
// bad
// This function does something
public static void doSomething(String input) {
// ...
}
// good
/**
* This function calculates the foo of a given input.
*
* @param input The input to calculate the foo of.
* @return The foo of the input.
*/
public static int calculateFoo(String input) {
// ...
}
9. 高质量代码:遵循最佳实践
编写高质量代码需要遵循一些最佳实践。这些最佳实践包括:
- 使用一致的命名约定
- 将代码组织成模块
- 使用有意义的函数和类名称
- 使用代码注释来提高代码的可读性
- 使用工具来帮助你编写更好的代码
“Practice makes perfect.”