From 8c969ab102015de43b1e9f27bfb69e3f18a0f25e Mon Sep 17 00:00:00 2001 From: ChunFuWu <319355703@qq.com> Date: Wed, 20 Sep 2023 08:43:55 +0800 Subject: [PATCH] [Improve] Add code quality rules (#252) * [Improve] Add code quality rules * [Improve] Add code quality rules --- .../code-style-and-quality-guide.md | 132 +++++++++++++++++- 1 file changed, 129 insertions(+), 3 deletions(-) diff --git a/community/submit_guide/code-style-and-quality-guide.md b/community/submit_guide/code-style-and-quality-guide.md index 86ea140b3..05fc1e068 100644 --- a/community/submit_guide/code-style-and-quality-guide.md +++ b/community/submit_guide/code-style-and-quality-guide.md @@ -135,9 +135,9 @@ sidebar_position: 3 private static final long serialVersionUID = 1L; ``` -2. Redundant strings should be extracted as constants -If a constant has been hardcoded twice or more times, please directly extract it as a constant and change the corresponding reference. -In generally, constants in `log` can be ignored to extract. +2. Redundant strings should be extracted as constants + >If a constant has been hardcoded twice or more times, please directly extract it as a constant and change the corresponding reference. + In generally, constants in `log` can be ignored to extract. - Negative demo: @@ -400,6 +400,132 @@ to reduce code line depth and improve readability like follows: 1. Use a unified `Utils.requireXXX` to complete the validation of the prerequisite, and if possible, replace the `AlertXXException.throwIfXXX` by new pre-conditions checking. +### 3.11 StringUtils + +1. Use `StringUtils.isBlank` instead of `StringUtils.isEmpty` + + - Negative demo: + + ```java + if (StringUtils.isEmpty(name)) { + return; + } + ``` + + - Positive demo: + + ```java + if (StringUtils.isBlank(name)) { + return; + } + ``` + +2. Use `StringUtils.isNotBlank` instead of `StringUtils.isNotEmpty` + + - Negative demo: + + ```java + if (StringUtils.isNotEmpty(name)) { + return; + } + ``` + + - Positive demo: + + ```java + if (StringUtils.isNotBlank(name)) { + return; + } + ``` + +3. Use `StringUtils.isAllBlank` instead of `StringUtils.isAllEmpty` + + - Negative demo: + + ```java + if (StringUtils.isAllEmpty(name, age)) { + return; + } + ``` + + - Positive demo: + + ```java + if (StringUtils.isAllBlank(name, age)) { + return; + } + ``` + +### 3.12 `Enum` Class + +1. Enumeration value comparison + + - Negative demo: + + ```java + if (status.equals(JobStatus.RUNNING)) { + return; + } + ``` + + - Positive demo: + + ```java + if (status == JobStatus.RUNNING) { + return; + } + ``` + +2. Enumeration classes do not need to implement Serializable + + - Negative demo: + + ```java + public enum JobStatus implements Serializable { + ... + } + ``` + + - Positive demo: + + ```java + public enum JobStatus { + ... + } + ``` + +3. Use `Enum.name()` instead of `Enum.toString()` + + - Negative demo: + + ```java + System.out.println(JobStatus.RUNNING.toString()); + ``` + + - Positive demo: + + ```java + System.out.println(JobStatus.RUNNING.name()); + ``` + +4. Enumeration class names uniformly use the Enum suffix + + - Negative demo: + + ```java + public enum JobStatus { + ... + } + ``` + + - Positive demo: + + ```java + public enum JobStatusEnum { + ... + } + ``` + ## 4 Exception Processing This `streampark-console-service` module is the core module for processing user requests.