if(StringUtil.isEmpty(username))thrownewICRClientException("username can not be null");if(StringUtil.isEmpty(password))thrownewICRClientException("password can not be null");if(udto==null)thrownewICRClientException("ICRUploadDTO can not be null");
重构之后:
1234567891011
//将原来的地方替换为checkStringParamEmpty(username,"username");checkStringParamEmpty(password,"password");checkStringParamEmpty(udto.getUrlPath(),"urlPath");...//新增一个方法privatevoidcheckStringParamEmpty(Stringvalue,Stringname)throwsICRClientException{if(StringUtil.isEmpty(value)){thrownewICRClientException(name+" can not be null");}}
原代码中不止这3个参数的校验,还有很多,越多参数的校验,我们重构后的复杂度就会越低。
代码复杂度变化:原来是3,修改后为1。
多String值判断
123
if(!udto.getPriority().equals("0")&&!udto.getPriority().equals("1")&&!udto.getPriority().equals("2")&&!udto.getPriority().equals("3"))thrownewICRClientException("priority must be 0/1/2/3");
重构之后:
123456789
//将原来代码替换为checkValueWithinList(udto.getPriority());...//新增一个方法:privatevoidcheckValueWithinList(Stringpriority)throwsICRClientException{if(!Arrays.asList("0","1","2","3").contains(priority)){thrownewICRClientException("priority must be 0/1/2/3");}}
代码复杂度变化:原来是4,修改后为1。
对list的非空判断
12
if(list==null||list.size()==0)thrownewICRClientException("list can not be null");
重构之后:
1234567
//将原来的代码替换为checkValueWithinList(udto.getPriority());...//新增一个方法privatevoidcheckListNoNull(Listlist)throwsICRClientException{if(list.isEmpty())thrownewICRClientException("list can not be null");}