一种方法应该做一件事并且做得好.在这种情况下,您的方法正在做两件事:业务逻辑和错误处理:
public Foo bar() {
try {
//business logic that may throw
//...
//end even more
} catch(BuzzException e) {
//Error handling
}
}
Run Code Online (Sandbox Code Playgroud)
我经常发现自己将try
块的内容提取到一个单独的方法中而没有错误处理:
public Foo bar() {
try {
return unsafeBar();
} catch(BuzzException e) {
//Error handling
}
}
public Foo unsafeBar() throws BuzzException {
//business logic that may throw
//...
//end even more
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
713 次 |
最近记录: |