当前位置:网站首页>枚举类和魔术值
枚举类和魔术值
2022-07-25 17:01:00 【ZJH'blog】
1.枚举类的优点
1.1可读性
在idea的环境下,鼠标指针移上去就能看到相应的注释
1.2参数传递错误
例如在一些重要的配置如:消息队列的交换机、队列;缓存的key;等等。用枚举类可以防止把关键的属性写错某个字母。
1.3Switch语句优势
如果业务中一个Switch中可能存在很多个情况,就有可能产生越界的问题。使用枚举值,可以在编译之前就防止越界问题
1.4方便查看在哪里用到过
在idea环境下,直接Ctrl+Shift+F搜索该枚举值,可以找到有哪些地方用到了这个业务属性。如果是魔术值,就不能找到
2.魔术值的危害
- 同上1.4,如果是魔术值,则无法与其他相关业务进行关联
- 可能写错字
- 可能相同的业务,不同的人,不同的情况下,值与业务映射关系不同
- 看不到idea注释
3.使用枚举类的误区
3.1业务理解偏差
我在日常工作中发现了这样一个问题:对于表中的delete_flag字段,1代表正常,0代表已删除。
但是很多地方用的是Enale.YES.getvalue() 和Enable.NO.getvalue()。这明显跟这个业务不是直接相关的
正确的用法应该是建立一个DeleteFlag枚举类,用USING(1) 和FORBIDDEN(0)来表示是否删除
3.2本质还是魔术值的枚举值
例如有这样一个枚举类:NUMBER,里面的属性分别是ONE(1) TWO(2) THREE(3),这样的枚举本质上还是一个魔术值,他和业务含义没有直接联系
3.3没有为enum单独分包
相关业务的所有enum应该放在同一个包下。不然可能存在想找相关业务的枚举类但是找不到的情况
3.4无论业务,所有enum放在同一个包下
不同的模块的所有enum如果放在了同一个包下,也很难直观的进行区分。应该按业务耦合程度,在包下再细分子包,存放相关的枚举类
边栏推荐
- What is chain game system development? How to make chain game system development
- Mindoc makes mind map
- Automatic reply of wechat official account development message
- 3D 语义分割——Scribble-Supervised LiDAR Semantic Segmentation
- 3D semantic segmentation - PVD
- 谁动了我的内存,揭秘 OOM 崩溃下降 90% 的秘密
- [knowledge atlas] practice -- Practice of question answering system based on medical knowledge atlas (Part4): problem analysis and retrieval sentence generation combined with problem classification
- Register service instances in ngmodule through dependency injection
- Budget report ppt
- Using rank to discuss the solution of linear equations / the positional relationship of three planes
猜你喜欢

基于SqlSugar的开发框架循序渐进介绍(13)-- 基于ElementPlus的上传组件进行封装,便于项目使用
![[target detection] tph-yolov5: UAV target detection based on Transformer's improved yolov5](/img/be/5348170fb460cbafbdb848d70fea15.png)
[target detection] tph-yolov5: UAV target detection based on Transformer's improved yolov5

Rainbond插件扩展:基于Mysql-Exporter监控Mysql

用秩讨论线性方程组的解/三个平面的位置关系

【obs】转载:OBS直播严重延迟和卡顿怎么办?

免费的低代码开发平台有哪些?

QT listview list display component notes

Fastadmin TP installation uses Baidu rich text editor ueeditor

【数学建模绘图系列教程】二、折线图的绘制与优化
![[Nanjing University of Aeronautics and Astronautics] information sharing for the first and second examinations of postgraduate entrance examination](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[Nanjing University of Aeronautics and Astronautics] information sharing for the first and second examinations of postgraduate entrance examination
随机推荐
Unity is better to use the hot scheme Wolong
IAAs infrastructure cloud cloud network
Various useful forms of London Silver K-line chart
Roson的Qt之旅#100 QML四种标准对话框(颜色、字体、文件、提升)
Multi tenant software development architecture
数据分析与隐私安全成 Web3.0 成败关键因素,企业如何布局?
Automatic reply of wechat official account development message
Chapter III data types and variables
【obs】转载:OBS直播严重延迟和卡顿怎么办?
Solution for win10 device management not recognizing gtx1080ti display device
多租户软件开发架构
GTX1080Ti 光纤HDMI干扰出现闪屏1080Ti 闪屏解决方法
Chain game development ready-made version chain game system development detailed principle chain game source code delivery
气数已尽!运营 23 年,昔日“国内第一大电商网站”黄了。。。
[target detection] tph-yolov5: UAV target detection based on Transformer's improved yolov5
第三章、数据类型和变量
【读书会第13期】+FFmpeg开源项目
Attachment handling of SAP Fiori
【MySQL篇】一文带你初识数据库
在华为昇腾Ascend910上复现swin_transformer