[Proposal] StreamPark code-style-and-quality Improve #2915
Replies: 22 comments
-
The naming conventions are quite mixed up, the priority for variable names is to use nouns, and the priority for method names is to start with a verb. For the db layer (non-service layer), the basic CRUD operations like getBy, selectBy, queryBy, etc., are now standardized according to com.baomidou.mybatisplus.core.mapper.BaseMapper:
For the service layer, basic CRUD operations are named according to com.baomidou.mybatisplus.extension.service.IService:
|
Beta Was this translation helpful? Give feedback.
-
For collection return values, unless there are special circumstances (such as needing thread safety), always return the interface. |
Beta Was this translation helpful? Give feedback.
-
Redundant string encoding // strings are extracted as constant references.
public static final String STATUS_SUCCESS = "success";
public static final String STATUS_FAIL = "error";
private static final long serialVersionUID = -8713837118340960775L;
public static RestResponse success(Object data) {
RestResponse resp = new RestResponse();
// strings not be extracted
resp.put("status", STATUS_SUCCESS);
resp.put("code", ResponseCode.CODE_SUCCESS);
resp.put("data", data);
return resp;
} |
Beta Was this translation helpful? Give feedback.
-
Unreasonable branch order results in multiple lines of code depth+1. if (request instanceof MultipartHttpServletRequest) {
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
Map<String, MultipartFile> files = multipartRequest.getFileMap();
for (String file : files.keySet()) {
MultipartFile multipartFile = multipartRequest.getFile(file);
ApiAlertException.throwIfNull(
multipartFile, "File to upload can't be null. Upload file failed.");
boolean fileType = FileUtils.isJarFileType(multipartFile.getInputStream());
ApiAlertException.throwIfFalse(
fileType, "Illegal file type, Only support standard jar files. Upload file failed.");
}
}
return true;
----->>
if (!(request instanceof MultipartHttpServletRequest)) {
return true;
}
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
Map<String, MultipartFile> files = multipartRequest.getFileMap();
for (String file : files.keySet()) {
MultipartFile multipartFile = multipartRequest.getFile(file);
ApiAlertException.throwIfNull(
multipartFile, "File to upload can't be null. Upload file failed.");
boolean fileType = FileUtils.isJarFileType(multipartFile.getInputStream());
ApiAlertException.throwIfFalse(
fileType, "Illegal file type, Only support standard jar files. Upload file failed.");
}
return true;
-------------------- |
Beta Was this translation helpful? Give feedback.
-
Branch handling leads to redundant lines, making the code less concise. org.apache.streampark.console.base.mybatis.interceptor.PostgreSQLQueryInterceptor#intercept
CacheKey cacheKey;
BoundSql boundSql;
if (args.length == 4) {
boundSql = ms.getBoundSql(parameter);
cacheKey = executor.createCacheKey(ms, parameter, rowBounds, boundSql);
} else {
cacheKey = (CacheKey) args[4];
boundSql = (BoundSql) args[5];
}
->>
boolean fourLen = args.length == 4;
BoundSql boundSql = fourLen ? ms.getBoundSql(parameter) : (BoundSql) args[5];
CacheKey cacheKey =
fourLen ? executor.createCacheKey(ms, parameter, rowBounds, boundSql) : (CacheKey) args[4];
|
Beta Was this translation helpful? Give feedback.
-
if ... if (nested condition) // map
if (obj instanceof Map) {
if (((Map<?, ?>) obj).isEmpty()) {
return true;
}
}
-->>
// map
if (obj instanceof Map && ((Map<?, ?>) obj).isEmpty()) {
return true;
} |
Beta Was this translation helpful? Give feedback.
-
too lines in a method try to split the method body with mutil sub-methods at proper points.
1. for fine-test
2. for semantic
3. for fine-readable |
Beta Was this translation helpful? Give feedback.
-
The method lacks comments:when, how, what , note |
Beta Was this translation helpful? Give feedback.
-
The method's parameters and return values lack non-null or null annotations and constraints when there are restrictions. |
Beta Was this translation helpful? Give feedback.
-
Lacks necessary Class header documentation |
Beta Was this translation helpful? Give feedback.
-
Lacks unit tests (final) |
Beta Was this translation helpful? Give feedback.
-
(Final) Splitting or refactoring of business logic |
Beta Was this translation helpful? Give feedback.
-
References: |
Beta Was this translation helpful? Give feedback.
-
Hi, @VampireAchao Thanks for your comments. LGTM & looking forward to the next step to advance it~ 👍 |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
StreamPark-console code-style-and-quality Improve1 Pull Requests & Changes Rule
2 Code Checkstyle
3 Programming Specification3.1 Naming Style
3.2 Constant Definition
// Positive demo: Strings are extracted as constant references.
public static final String STATUS_SUCCESS = "success";
public static final String STATUS_FAIL = "error";
private static final long serialVersionUID = -8713837118340960775L;
public static RestResponse success(Object data) {
RestResponse resp = new RestResponse();
// Negative demo:Strings not be extracted
resp.put("status", STATUS_SUCCESS);
resp.put("code", ResponseCode.CODE_SUCCESS);
resp.put("data", data);
return resp;
}
3.3 Methods Rule
In addition, it is also necessary to consider whether the splitting is reasonable in terms of components, logic, abstraction, and other aspects in the scenario
3.4 Collection RuleFor
If there are multiple threads, the following declaration or returned types can be used:
3.5 Concurrent Processing
3.6 Control/Condition Statements
3.7 Code Comments Rule
Note:
3.8 Java Lambdas
3.9 Java Streams
3.10 Pre-Conditions CheckingUse a unified 4 Exception ProcessingIt could reference to About Exception Processing for streampark-console-service Module. 5 Log
6 Testing
How to do
References
|
Beta Was this translation helpful? Give feedback.
-
Hi, @VampireAchao Thanks for your efforts on the discussion page. If there are no other new suggestions or changes before the end of this week, it would be the time to prepare to initiate a voting email. |
Beta Was this translation helpful? Give feedback.
-
Great job, thanks for your driving and hard work. This is very helpful in improving the code quality of StreamPark. BTW, we can initiate another discussion about the scala code style |
Beta Was this translation helpful? Give feedback.
-
Add a rule: Enumeration type must have a suffix of "Enum", e.g: UserTypeEnum, ResourceTypeEnum. |
Beta Was this translation helpful? Give feedback.
-
Hi, @wolfboys Thanks for the suggestion~ Undoubtedly, the rule will improve readability. However, in some cases, it can make the declaration line of the enumeration type particularly long. This requires us to be as concise as possible when defining enumeration classes, and when using static methods referenced by enumerations, this issue can be magnified. Of course, in response to this issue, we can limit the use of static import methods when calling static methods to alleviate this problem. Of course, I'd like to hear more opinion/advice from you & others. |
Beta Was this translation helpful? Give feedback.
-
Will be advanced in #2986 |
Beta Was this translation helpful? Give feedback.
-
Completed in #2986 |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
All reactions