当前位置:网站首页>Enumeration classes and magic values
Enumeration classes and magic values
2022-07-25 17:05:00 【ZJH'blog】
1. Advantages of enumerating classes
1.1 Readability
stay idea Under the environment of , Move the mouse pointer up to see the corresponding comments
1.2 Parameter passing error
For example, in some important configurations such as : Message queuing switch 、 queue ; The cache key; wait . Enumerating classes can prevent you from writing a key attribute wrong .
1.3Switch Sentence advantage
If one in the business Switch There may be many situations in , There may be cross-border problems . Use enumeration values , You can prevent cross-border problems before compiling
1.4 It is convenient to check where it has been used
stay idea In the environment , direct Ctrl+Shift+F Search the enumeration value , You can find out where this business attribute is used . If it is magic value , Can't find it
2. The harm of magic value
- ditto 1.4, If it is magic value , It cannot be associated with other related businesses
- You may write wrong words
- Maybe the same business , Different people , In different situations , The value is different from the business mapping relationship
- Out of sight idea notes
3. Misunderstanding of using enumeration classes
3.1 Business understanding deviation
I found such a problem in my daily work : For delete_flag Field ,1 For normal ,0 Represents the deletion of .
But many places use Enale.YES.getvalue() and Enable.NO.getvalue(). This is obviously not directly related to this business
The correct usage should be to build a DeleteFlag Enumeration class , use USING(1) and FORBIDDEN(0) To indicate whether to delete
3.2 The essence is the enumeration value of magic value
For example, there is such an enumeration class :NUMBER, The attributes inside are ONE(1) TWO(2) THREE(3), Such enumeration is essentially a magic value , He has no direct connection with the meaning of business
3.3 Not for enum Separate subcontracting
All related businesses enum It should be put under the same bag . Otherwise, there may be situations where you want to find enumeration classes of related businesses but cannot find them
3.4 Regardless of business , all enum Under the same bag
All of the different modules enum If you put it under the same bag , It is also difficult to distinguish intuitively . It should be based on the degree of business coupling , Fine molecular package under the package , Store related enumeration classes
边栏推荐
- 聊聊如何用 Redis 实现分布式锁?
- Why 4everland is the best cloud computing platform for Web 3.0
- 企业直播风起:目睹聚焦产品,微赞拥抱生态
- In the eyes of 100 users, there are 100 QQS
- [book club issue 13] +ffmpeg video capture function
- Is the online account opening of Founder futures reliable and safe?
- 【obs】转载:OBS直播严重延迟和卡顿怎么办?
- jenkins的Role-based Authorization Strategy安装配置
- 使用Huggingface在矩池云快速加载预训练模型和数据集
- 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
猜你喜欢

MySQL之联表查询、常用函数、聚合函数

WPF implements user avatar selector

在华为昇腾Ascend910上复现swin_transformer

Rebudget: balance efficiency and fairness in market-based multi-core resource allocation by reallocating the budget at run time

数据分析与隐私安全成 Web3.0 成败关键因素,企业如何布局?

China's chip self-sufficiency rate has increased significantly, resulting in high foreign chip inventories and heavy losses. American chips can be said to have thrown themselves in the foot

Enterprise live broadcast: witness focused products, praise and embrace ecology

In the eyes of 100 users, there are 100 QQS

How to deploy applications on IPFs using 4everland cli

152. Product maximum subarray
随机推荐
Don't believe these "rumors" in the process of preparing for the exam!
GTX1080Ti 光纤HDMI干扰出现闪屏1080Ti 闪屏解决方法
WPF implements user avatar selector
Mindoc makes mind map
文字翻译软件-文字批量翻译转换器免费
Hcip notes 12 days
[redis] redis installation
简述redis集群的实现原理
Text translation software - text batch translation converter free of charge
基于SqlSugar的开发框架循序渐进介绍(13)-- 基于ElementPlus的上传组件进行封装,便于项目使用
异常处理机制专题1
C#入门基础教程
152. 乘积最大子数组
虚拟内存管理
第五章:流程控制
152. Product maximum subarray
[knowledge atlas] practice -- Practice of question and answer system based on medical knowledge atlas (Part5 end): information retrieval and result assembly
In the eyes of 100 users, there are 100 QQS
自定义mvc项目登录注册和树形菜单
Test framework unittest command line operation and assertion method