CleanCode CH7

来源:互联网 发布:音频剪切合并软件 编辑:程序博客网 时间:2024/06/06 02:24

错误处理:

1 使用异常,而非返回码。

try{

}catch

{

}

 

2 从异常处理查看对第三方接口打包的好处:

比如:

ACMPort port = new AcmPort(12);

 

try

{

    port.open();

}catch (DeviceRespronerror e){

 report err;

log(xxxx);

}

catch( GMXERROR e{

report err;

log(xxxx);

}

....

 

 

既然知道调用第三方的接口的行为,即可以考虑打包:

LocalPort port = new LocalPort(12);

 

try{

    port.open

}catch(localexpection e)

{

 e.report()

e.log();

}

}

 

class LocalPort{

ACMPORT port;

try{

   port.open()

}catch(DeviceRespronerror e)

{

    throw new LocaldeviceRespronerror(e);

}

catch (gmcerror e)

{

  throw new gmxerr(e)

}

}

类似第三方API打包是一个很好的实践手段,降低了对他的依赖,不必绑死在第三方。测试中交容易模拟。

 

3 NULL值

不要返回NULL值: 解决方案 1 抛出异常  2 返回特例对象。

不要传递NULL值

原创粉丝点击