java中我们很常见的设计API的例子是对一个对象进行增,删,查,改.比如ObjectaddObject(Objectobj);ObjectdelObject(Objectobj);ObjectmodifyObject(Objectobj);成功返回obj,不成功(参数不合法,或已经存在,或不存在)抛出异常还是intaddObject(Objectobj);intdelObject(Objectobj);intmodifyObject(Objectobj);如果成功返回0,不成功,返回其他数字还是booleanaddObject(Objectobj);booleandelObject(Objectobj);booleanmodifyObject(Objectobj);如果成功返回0,不成功(参数不合法,或已经存在,或不存在),抛出异常感谢各位热心回答,我已经决定采用第一种方式了.原因:1.性能不是那么的关键;2绝大多数我们认为参数是正确的.排除异常较返回Errorcode让代码更加简洁.补充:以下是从这里找到的一个回答,基本决定采用第一中方式,这或许是java语言本身的特点,如果是c或cpp我更倾向于第二种方式.Exceptionsimposequiteanoverheadontheruntimeperformance,butmakesreasoningabouttheprogramflowdrasticallyeasier.Thisreducesfaultyprogramming(semanticerrors),especiallyasitforcesyoutodealwiththem-they'failsecurely'byterminatingtheprogramiftheyareignored.Theyareidealfor'situationswhicharenotsupposedtohappen'.Alsotheycantransportmetadatalikeastacktrace.Errorcodes,ontheotherhand,arelight-weight,fast,butforcesthemethodcallertoexplicitelycheckthem.Failuretodosooftenresultsinprogramflaws,whichcanrangefromsilentdatacorruption,securityholes,tonicefireworksifyourprogramhappenstoberunninginsideaspacerocket.
添加回答
举报
0/150
提交
取消
