当前位置:网站首页>代码简洁高效建议
代码简洁高效建议
2022-07-16 18:56:00 【thoughtCodes】
前言:
1.一定要进行逻辑上的优化,使得代码更加简洁
1.1.1.利用构造方法
构造方法,可以简化对象的初始化和设置属性操作。对于属性字段较少的类,可以自定义构造方法。
1.3.利用 Map 的 computeIfAbsent 方法
1.4.利用链式编程
链式编程,也叫级联式编程,调用对象的函数时返回一个this对象指向对象本身,达到链式效果,可以级联调用。链式编程的优点是:编程性强、可读性强、代码简洁。
if (CollectionUtils.isNotEmpty(userList)) {
2.2.避免条件判断
double result = Math.max(MIN_LIMIT, value);
public static final List ANIMAL_LIST;
static {
List animalList = new ArrayList<>();
animalList.add(“dog”);
animalList.add(“cat”);
animalList.add(“tiger”);
ANIMAL_LIST = Collections.unmodifiableList(animalList);
}
2.4.简化数据拷贝
UserVO userVO = new UserVO();
BeanUtils.copyProperties(userDO, userVO);
userVOList.add(userVO);
UserVO userVO = new UserVO();
BeanUtils.copyProperties(userDO, userVO);
userVOList.add(userVO);
Assert.notNull(userId, “用户标识不能为空”);
3.利用数据结构
private static final double[] SCORE_RANGES = new double[] {90.0D, 80.0D, 60.0D, 30.0D};
public static int getGrade(double score) {
for (int i = 0; i < SCORE_RANGES.length; i++) {
if (score >= SCORE_RANGES[i]) {
return i + 1;
}
}
return SCORE_RANGES.length + 1;
}
对于映射关系的 if-else 语句,可以用Map来简化。此外,此规则同样适用于简化映射关系的 switch 语句。
3.4.利用 ThreadLocal 简化
ThreadLocal 提供了线程专有对象,可以在整个线程生命周期中随时取用,极大地方便了一些逻辑的实现。用 ThreadLocal 保存线程上下文对象,可以避免不必要的参数传递。
参考资料:
https://blog.51cto.com/u_15057823/2632449
边栏推荐
- Cann experience officer's fifth experience documentary (Part 1)
- [dynamic programming]dp21 regular expression matching - difficult
- Unity-2d pixel lattice ablation
- [JS encapsulates a simple asynchronous API to obtain asynchronous operation results and process parsing]
- True question of CCF (anger takes 100 faints)
- Flutter draws very interesting Bezier curve animation
- MySQL -- string function
- Epic-kbs9 industrial computer brushing document
- Copy the contents of one folder to another folder
- Do you know the answers to the common questions in the interview of senior programmers? With answer
猜你喜欢

为什么网络安全在工厂中很重要?

What is the event delegation in JS?

Sword finger offer 55 - ii balanced binary tree

如何应对供应链中第三方的安全风险

Different image patches are processed by different expert models! Nanyang Institute of Technology & Mila sparse fusion hybrid expert model SF MOE has super generalization ability! The code is open sou
![[the pro test is valid]npm warn config global ` --global`, `--local` are deprecated Use `--location=global` instead.](/img/07/9f43b0b8ea0c887900d4daa351f1f8.png)
[the pro test is valid]npm warn config global ` --global`, `--local` are deprecated Use `--location=global` instead.

Design and sharing of inclinometer based on single chip microcomputer

Win10 how to convert FAT32 format disks into NFTs format without formatting

Copy the contents of one folder to another folder

鲁汶大学招募博士后研究员,利用AI/ML对太阳活动区图像进行分析和耀斑预测...
随机推荐
Unity about some possible reasons and solutions for using addforce of rigidbody but it doesn't work
Embedded development: seven techniques for accelerating firmware development
基于STM32+华为云IOT设计的物联网鱼缸【玩转华为云】
Internet of things aquarium designed based on stm32+ Huawei cloud IOT [play with Huawei cloud]
Pytorch depth separable convolution and mobilenet_ v1
不同的图像patch由不同的专家模型来处理!南洋理工&Mila稀疏融合混合专家模型SF-MoE,具有超强泛化能力!代码已开源!...
裁员之水天上来
Different image patches are processed by different expert models! Nanyang Institute of Technology & Mila sparse fusion hybrid expert model SF MOE has super generalization ability! The code is open sou
I2C通信协议实现在OLED显示屏数据显示
Topic 2-iis write permission vulnerability analysis and traceability
I2C通信协议编程实现
Part 13: implementation of STM32 I2C serial bus communication
Classic application method of installing HAP on Hongmeng development board
【黄啊码】为什么我建议您选择go,而不选择php?
Sql笔记
Best practices for exclusive resource pool use -notebook and training task linkage
R language uses GLM function to build Poisson logarithm linear regression model, processes three-dimensional contingency table data to build saturation model, and uses step function to realize stepwis
What are the directions of voice signal processing in audio and video?
R language uses LM function to build regression model and BoxCox function of mass package to find the best power transformation to improve the fitting degree of the model (determine the best λ Paramet
UE4 shadow: perobjectshadow verification