If the specification is too strict, it will restrict the innovation of programmers. If the specification is not, various codes will be written. This paper tries to say from personal code experience which should be regarded as the specification and which should not be forced.

1. The name

Naming is the most important specification, but it is not enough to give, for example, hump naming. You need to restrict the “business terms” and “technical terms” used in your program.

1.1 Problem Examples

For example, in “file backup program” making address during the development process, I often want to use all kinds of file name (folder path string, folder path object, folder file object, file path string, object file path, the file object, compression of source folder < string, the file object, object > path, Compressed target file…..) Different classes at different times have completely different names and the code is hard to read. (There are strings at the end of File, and File objects; The end of Path sometimes denotes a string, sometimes denotes a Path object), and the code at one point struggles to read operations involving files.

1.2 Solutions

File naming conventions

entity type named
file File xxxFile
folder File xxxDir
The file path Path xxxFilePath
Folder path Path xxxDirPath
The file path String xxxFileStr
Folder path String xxxDirStr

Naming conventions for compressed files

entity type named
Compressed source folder File sourceDir
Destination compressed file File destZipFile
Compressed source file File sourceFile
Unzip the files File zipFile
Unzip folder File destDir

Document comparison terminology specification

entity named instructions
Abstract digest Calculate whether the file has changed, currently by MD5 or file length
version version Generate a summary file for the folder as a whole. A unified summary file can be called a version of the file.

2. Basic grammar

2.1 Control Statements

  • Stream and forEach are preferred

  • If you need access to non-final, use normal loop writing

2.1 class and record

  • [Recommendation] Record is preferred as a value object or a simple entity. Business complex objects are designed as entities using Class and contain business operations

2.2 interface

  • [Recommendation] No different implementation can use the interface
  • [Mandatory] Different implementations choose interfaces

3. Get more expressive code with the new JDK

Expressiveness is the ability to make code as short as possible without affecting reading.

Compare the first two pieces of code, which do you think is better?

List<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(5);
list.add(3);
Copy the code

VS

var list = List.of(1, 5, 3);
Copy the code

It is clear that the following method not only has less code, but is also easier to read. We should choose as short a code as possible without compromising the reading experience. With the development of Java’s new syntax, the language is becoming more expressive. Here are some more expressive ways: weak types define var syntax, local variables are also expressive enhancements that do not use types, and it is easy to read types based on context.

3.1 lambda

List<String> list = map.get("key");
if (list == null) {
    list = new ArrayList<>();
}
Copy the code

VS

List<String> list = map.computeIfAbsent("key", key -> new ArrayList<>());
Copy the code

Lamda makes it easy to pass processes as parameters, making it easy to implement the “template approach” design pattern. Reuse the execution skeleton of the function to coordinate the scheduling of incoming LAMda procedures.

3.2 Stream-Lamda enhanced collection

The stream that JDK8 initially supports is actually some syntax-sugar done with Lamda. Lamda support is provided for common operations on collections such as traversal, map, and Reduce operations. Operations on collections that you used to care about how to do, now you just care about what to do.

List<String> names = new ArrayList<>();
for (User user : users) {
    names.add(user.getName());
}
Copy the code

VS

List<String> names = users.stream().map(user -> user.name).collect(Collectors.toList());
Copy the code

3.3 Chain writing

Chain writing eliminates the repeated subject and is more expressive.

var article = new Article(1L, "testUser");
article.setTitle("title");
article.setContent("content");
article.setOrderNum(100);
Copy the code

VS

var article = new Article(1L, "testUser").setTitle("title").setContent("content").setOrderNum(100);
Copy the code