これはかなり規則的に起こっている問題で、ベストプラクティスの状況を見つけたことはありません。例外はおそらく私が働いているアプリケーションは使用しないので、現在使用されているメソッドに固執しようとしています。複数のif/elseifとエラーメッセージング/エラー処理のベストプラクティス
3つ、4つ、5つ以上の異なる条件をチェックする必要があり、エラーメッセージが設定されているか処理が続行されている場合に、ステートメント、リターン、メッセージなどをレイアウトする最良の方法は何ですか?コードの先頭ですべてのエラーチェックを物理的に行うのがベストプラクティスですか?
実際のタイプの条件の例を示します。
function process($objectId,$userId,$newData)
{
$error = '';
if(($object = $this->getObject($objectId)) && $object->userOwnsObject($userId))
{
if($this->isValid($newData))
{
if($object->isWriteable())
{
if($object->write($newData))
{
// No error. Success!
}
else
{
$error = 'Unable to write object';
}
}
else
{
$error = 'Object not writeable';
}
}
else
{
$error = 'Data invalid';
}
}
else
{
$error = 'Object invalid';
}
return $error;
}
OR
function process($objectId,$userId,$newData)
{
$error = '';
if((!$object = $this->getObject($objectId)) && !$object->userOwnsObject($userId))
{
$error = 'Object invalid';
}
elseif(!$this->isValid($newData))
{
$error = 'Data invalid';
}
elseif(!$object->isWriteable())
{
$error = 'Object not writeable';
}
elseif(!$object->write($newData))
{
$error = 'Unable to write to object';
}
else
{
// Success!
}
return $error;
}
それは、この場合には、オプション2を移動するための方法であると私には明らかです。はるかに明確です。
function process($objectId,$userId,$newData)
{
$error = '';
if(($object = $this->getObject($objectId)) && $object->userOwnsObject($userId))
{
$this->setValidationRules();
$parent = $object->getParentObject();
$parent->prepareForChildUpdate();
if($this->isValid($newData,$parent))
{
$newData = $this->preProcessData($newData);
if($object->isWriteable())
{
// doServerIntensiveProcess() has no return value and must be done between these two steps
$this->doServerIntensiveProcess();
if($object->write($newData))
{
// No error. Success!
$parent->childUpdated();
}
else
{
$error = 'Unable to write object';
}
}
else
{
$error = 'Object not writeable';
}
}
else
{
$error = 'Data invalid';
}
}
else
{
$error = 'Object invalid';
}
return $error;
}
をORそれでいくつかの問題を持っている、これは
function process($objectId,$userId,$newData)
{
$error = '';
if((!$object = $this->getObject($objectId)) && !$object->userOwnsObject($userId))
{
$error = 'Object invalid';
}
// Is it wrong to hate multi-line conditionals?
elseif(!$this->setValidationRules() || (!$parent = $object->getParentObject()) ||
!$parent->prepareForChildUpdate() || !$this->isValid($newData,$parent))
{
$error = 'Data invalid';
}
elseif((!$newData = $this->preProcessData($newData)) || !$object->isWriteable())
{
$error = 'Object not writeable';
}
// Where does doServerIntensiveProcess() with no return value go??
elseif(!$object->write($newData))
{
$error = 'Unable to write to object';
}
else
{
// Success!
$parent->childUpdated();
}
return $error;
}
私はちょうどこのネストされた場合は、これを処理するための最良の方法のわからない:今、我々はそれが少し複雑にすることができますそれでは、そうしていれば、そのような種類の機能です。あなたが提供できる何らかの洞察力を知らせてくれてありがとう!
あなたは例外があることを知っているようですが、なぜそれらを使用したくないのですか?私は彼らがあなたの現在のコードベースで使われていないと理解していますが、今は始めるのが良い時期です。エラーを表示するために戻り値の文字列を使用することは、絶対にひどい考えです。 – ryeguy
ええ、そうです。特定の方法でスタイルされた大規模なコードベースです。私はつま先を踏みつけたり、他の開発者を例外の謎を探索する際に庭の道を上って行きたいとは思わない。 – fred