gpt4 book ai didi

PHP 多重 if/elseif 和错误消息/处理最佳实践

转载 作者:可可西里 更新时间:2023-10-31 22:59:22 25 4
gpt4 key购买 nike

这是我经常遇到的问题,我从未发现/想出最佳实践情况。异常可能是可行的方法,但是我正在使用的应用程序没有使用它们,所以我试图坚持使用当前使用的方法。

在需要检查 3、4、5 或更多不同条件并且设置错误消息或继续处理的情况下,布局 if 语句、返回、消息等的最佳方式是什么。在代码开头进行所有错误检查是否是最佳做法?

这是一个具有一些真实世界类型条件的示例。

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;
}

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;
}

或者这个有一些问题

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;
}

我只是不确定处理这种嵌套的 if-this-then-do-that-then-if-this-then-do-that 功能的最佳方式。非常感谢您提供的任何见解!

最佳答案

为了保持代码整洁,我倾向于这样做:

function process($objectId,$userId,$newData)
{
$object = $this->getObject($objectId);

if($object === false)
{
return "message";
}

if($object->userOwnsObject($userId) === false)
{
return "message";
}

if($this->setValidationRules() === false)
{
return "unable to set validation rules";
}

if(false !== ($parent = $object->getParentObject()))
{
return "unable to get parent object";
}

/*... etc ...*/

//if your here the all the checks above passed.
}

通过这样做,您还可以节省资源,因为您直接返回到位,代码看起来更干净,不需要 2 个嵌套

但是如果你从头开始构建函数我不明白为什么你不能在你的新代码中使用异常,它不会干扰当前的应用程序,并使生活更简单

function process($objectId,$userId,$newData)
{
if(false !== ($parent = $object->getParentObject()))
{
throw Exception("unable to get parent object");
}

/*... etc ...*/
}

try
{
$this->process(....);
}
catch(Exception $e)
{
show_error_page('invalid.php',$e);
}

或者另一种方法是使用名为 InternalError 的静态方法创建错误处理类,如下所示

abstract class Error
{
public static InternalError(Exception $Ex)
{
Logger::LogException($Ex);
//Then flush all buffers and show internal error,
}
}

所以除了上面的 show_error_page 你还可以这样做:

try
{
$this->process(....);
}
catch(Exception $e)
{
Error::InternalError($e); //this provides user with an interface to report the error that has just been logged.
}

这样您所有的Exception都会被记录下来并可以在您的管理系统中查看,这意味着您可以更快地跟踪错误而不是依赖成员来明显地看到错误,但得到一个很好的道歉使用电子邮件表单要求他们描述他们试图做什么,错误 ID 将附加到表单中,因此您可以跟踪用户到错误。

这是 IMO 错误处理的最佳形式。

关于PHP 多重 if/elseif 和错误消息/处理最佳实践,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4250321/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com