Enum Lesson I Learned
来源:互联网 发布:3位字母未注册域名 编辑:程序博客网 时间:2024/05/17 04:10
Current situation: we have several service providers for our business, each of them has different values for a specific enum, for example, SegmentNo, V may have a value set {0,1}, whereas M may have {1,2}, further, our system defines our own enumerations to a unified object, like {10, 20} for the SegmentNo. The question is: How many enums do we need? Our current situation is 3 enums, worse that our team still prefer passing integer type for SegmentNo, rather than Enum, which forms a enum hell, we often pass a wrong value and boring to debug this again and again, developers often confused by the passed value, really unpleasant.
/* We often pass wrong parameter for this kind of API. Don't do this. */public void doSomeBusiness(int vSegmentNo){}Improvement:
1. Encapsulate the mapping in a single enum if they have same semantic, only differ by value.
public enum SegmentNo {FT(10, 0), H1(20, 1), ET(30, -1);private int id;private int vSegmentNo;SegmentNo(int id, int vSegmentNo) {this.id = id;this.vSegmentNo = vSegmentNo;}public int getId() {return this.id;}public int getvSegmentNo() {return this.vSegmentNo;}}2. Refactor the API, pass enums instead of integers, fetch the right integer if and only if necessary.
public void doSomeBusiness(SegmentNo sn){int vsn = sn.getvSegmentNo();}If we code in this manner, unlikely to pass incorrect value between methods. I believe this practice is much better.
- Enum Lesson I Learned
- Lesson Learned From Project
- Programming lessons I learned
- What I learned from english
- What I Learned at Google
- 8 months in Microsoft, I learned these
- 41 Things I've Learned By 40
- Lesson: Basic I/O
- LESSON 6 MALWARE part I
- 【论文笔记】Show and Tell: Lesson learned from the 2015 MSCOCO Image Captioning Challenge
- activemq of what i have learned (run helloworld)
- Presentation: what have I learned from MS Team?
- Top 20 Programming Lessons I've Learned in 20 Years
- 57 Things I’ve Learned Founding 3 Tech Companies
- Top 20 Programming Lessons I've Learned in 20 Years
- 57 Things I've Learned Founding 3 Tech Companies
- What I Have Learned From My First Scientific Research
- 8 Things I learned from "High Performance MySQL"
- VC程序Release程序崩溃后问题根源查找
- 快捷配置电脑IP地址
- SSL请求trustStore的两种注册方式
- linux `倒引号
- 前台向后台传递中文值时 乱码
- Enum Lesson I Learned
- IOS 开发之 CocoaPods讲解
- GS01创建信息集及其他GROUP在ABAP程序中的调用方法
- linux fread和read
- PHP timestamp function
- Android之ScrollView嵌套ListView冲突
- 通过在JAVA类中动态设置系统参数的问题
- opencv学习-imgprocess-凸包函数Convex Hull和boundingRect以及minEnclosingCircle
- jmeter压力测试,简单样例