我负责的几个函数与一些代码味道:函数返回值或整数状态代码重构
public function example($a, $b, $c) {
$something = doSomething($a);
$more = doMoreStuff($b,$c);
$evenMore = doEvenMoreStuff($a,$c);
if(!$something) {
//error code because blablabla
return 1;
}
if(!$something and $more == "whatever") {
//another different error because blebleble
return 2;
}
if(!$more) {
//this means another error because bliblibli
return 3;
}
if(!$evenMore) {
//yep, error, returning code error 4
return 4;
}
//etc...
//if no errors
return $something + $more + $evenMore;
}
我应该如何处理这些错误代码?我想在像这样创建具有常量值的类:
class ExampleError {
const BLABLABLA = 1;
const BLEBLEBLE = 2;
const BLIBLIBLI = 3;
const BLOBLOBLO = 4;
}
然后重构功能多变的线条像
return 1;
;
到
return ExampleError::BLABLABLA;
试图使其更具可读性。有更好的方法吗?
mmm,错误包括'丢失的ID字段','问题更新数据','无效状态',...(他们是边缘情况?),感谢术语“幻数” – vivoconunxino
谢谢内维尔,我真的感谢你的taughts – vivoconunxino