当前位置:网站首页>枚举类和魔术值
枚举类和魔术值
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如果放在了同一个包下,也很难直观的进行区分。应该按业务耦合程度,在包下再细分子包,存放相关的枚举类
边栏推荐
- 第三章、数据类型和变量
- 激荡20年,芯片产能从零起步到反超美国,中国制造的又一大成就
- 在华为昇腾Ascend910上复现swin_transformer
- MySQL linked table query, common functions, aggregate functions
- Chapter VI succession
- Bo Yun container cloud and Devops platform won the trusted cloud "technology best practice Award"
- 方正期货网上开户靠谱吗,开户安全吗?
- Jenkins' file parameters can be used to upload files
- Data analysis and privacy security become the key factors for the success or failure of Web3.0. How do enterprises layout?
- [Nanjing University of Aeronautics and Astronautics] information sharing for the first and second examinations of postgraduate entrance examination
猜你喜欢

Text translation software - text batch translation converter free of charge

7.依赖注入

激荡20年,芯片产能从零起步到反超美国,中国制造的又一大成就

mindoc制作思维导图

After 20 years of agitation, the chip production capacity has started from zero to surpass that of the United States, which is another great achievement made in China

Chapter VI succession

win10如何删除微软拼音输入法

第六章 继承

3D semantic segmentation - scribed supervised lidar semantic segmentation
Go语言系列:Go从哪里来,Go将去哪里?
随机推荐
月薪1万在中国是什么水平?答案揭露残酷的收入真相
企业直播风起:目睹聚焦产品,微赞拥抱生态
Fudan University emba2022 graduation season - graduation does not forget the original intention and glory to embark on the journey again
Two methods of importing sqllite table from MySQL
谁动了我的内存,揭秘 OOM 崩溃下降 90% 的秘密
Text translation software - text batch translation converter free of charge
第四章:操作符
Data analysis and privacy security become the key factors for the success or failure of Web3.0. How do enterprises layout?
多租户软件开发架构
MySQL linked table query, common functions, aggregate functions
Is it safe to open a securities account in Huatai VIP account
3D语义分割——PVD
Getting started with easyUI
Roson的Qt之旅#100 QML四种标准对话框(颜色、字体、文件、提升)
EasyUI DataGrid control uses
Various useful forms of London Silver K-line chart
Rainbow plug-in extension: monitor MySQL based on MySQL exporter
Is the online account opening of Founder futures reliable and safe?
WPF implements user avatar selector
Rosen's QT journey 100 QML four standard dialog boxes (color, font, file, promotion)