从一个函数的早期返回比if语句更优雅? [英] Is returning early from a function more elegant than an if statement?

查看:112
本文介绍了从一个函数的早期返回比if语句更优雅?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我自己和一个同事对以下哪一个更加优雅有争议。我不会说谁是谁,所以是公正的。哪个更优雅?

  public function set hitZone(target:DisplayObject):void 
{
if (_hitZone!= target)
{
_hitZone.removeEventListener(MouseEvent.ROLL_OVER,onBtOver);
_hitZone.removeEventListener(MouseEvent.ROLL_OUT,onBtOut);
_hitZone.removeEventListener(MouseEvent.MOUSE_DOWN,onBtDown);

_hitZone = target;

_hitZone.addEventListener(MouseEvent.ROLL_OVER,onBtOver,false,0,true);
_hitZone.addEventListener(MouseEvent.ROLL_OUT,onBtOut,false,0,true);
_hitZone.addEventListener(MouseEvent.MOUSE_DOWN,onBtDown,false,0,true);
}
}

...或...

  public function set hitZone(target:DisplayObject):void 
{
if(_hitZone == target)return;

_hitZone.removeEventListener(MouseEvent.ROLL_OVER,onBtOver);
_hitZone.removeEventListener(MouseEvent.ROLL_OUT,onBtOut);
_hitZone.removeEventListener(MouseEvent.MOUSE_DOWN,onBtDown);

_hitZone = target;

_hitZone.addEventListener(MouseEvent.ROLL_OVER,onBtOver,false,0,true);
_hitZone.addEventListener(MouseEvent.ROLL_OUT,onBtOut,false,0,true);
_hitZone.addEventListener(MouseEvent.MOUSE_DOWN,onBtDown,false,0,true);

}


解决方案

其中一个可以打破规则(即最佳做法)的情况之一。一般来说,您希望在函数中尽可能少的返回点。实际的原因是它简化了你对代码的阅读,因为你总是可以假设每个函数都会接受它的参数,做逻辑,并返回结果。为各种情况增加额外收益往往使逻辑复杂化,并增加阅读和充分挖掘代码所需的时间。一旦您的代码达到维护阶段,多个返回可能会对新程序员的生产力产生巨大的影响,因为它们尝试破译逻辑(其注释稀疏且代码不清楚时尤其糟糕)。问题在函数长度方面呈指数增长。



那么为什么在这种情况下,每个人都喜欢选项2?这是因为您正在设置通过验证传入数据或其他可能需要检查的不变量执行该功能的合同。用于构建验证的最漂亮的语法是检查每个条件,如果条件失效,立即返回。这样你就不必通过所有支票来维护某种类型的isValid布尔。



总结一下:我们真的在看如何写验证代码而不是一般逻辑;选项2更适用于验证码。


Myself and a colleague have a dispute about which of the following is more elegant. I won't say who's who, so it is impartial. Which is more elegant?

public function set hitZone(target:DisplayObject):void
        {
            if(_hitZone != target)
            {
                _hitZone.removeEventListener(MouseEvent.ROLL_OVER, onBtOver);
                _hitZone.removeEventListener(MouseEvent.ROLL_OUT, onBtOut);
                _hitZone.removeEventListener(MouseEvent.MOUSE_DOWN, onBtDown);

                _hitZone = target;

                _hitZone.addEventListener(MouseEvent.ROLL_OVER, onBtOver, false, 0, true);
                _hitZone.addEventListener(MouseEvent.ROLL_OUT, onBtOut, false, 0, true);
                _hitZone.addEventListener(MouseEvent.MOUSE_DOWN, onBtDown, false, 0, true);
            }
        }

...or...

public function set hitZone(target:DisplayObject):void
        {
            if(_hitZone == target)return;

            _hitZone.removeEventListener(MouseEvent.ROLL_OVER, onBtOver);
            _hitZone.removeEventListener(MouseEvent.ROLL_OUT, onBtOut);
            _hitZone.removeEventListener(MouseEvent.MOUSE_DOWN, onBtDown);

            _hitZone = target;

            _hitZone.addEventListener(MouseEvent.ROLL_OVER, onBtOver, false, 0, true);
            _hitZone.addEventListener(MouseEvent.ROLL_OUT, onBtOut, false, 0, true);
            _hitZone.addEventListener(MouseEvent.MOUSE_DOWN, onBtDown, false, 0, true);

        }

解决方案

This is one of those cases where it's ok to break the rules (i.e. best practices). In general you want to have as few return points in a function as possible. The practical reason for this is that it simplifies your reading of the code, since you can just always assume that each and every function will take its arguments, do its logic, and return its result. Putting in extra returns for various cases tends to complicate the logic and increase the amount of time necessary to read and fully grok the code. Once your code reaches the maintenance stage then multiple returns can have a huge impact on the productivity of new programmers as they try to decipher the logic (its especially bad when comments are sparse and the code unclear). The problem grows exponentially with respect to the length of the function.

So then why in this case does everyone prefer option 2? It's because you're are setting up a contract that the function enforces through validating incoming data, or other invariants that might need to be checked. The prettiest syntax for constructing the validation is the check each condition, returning immediately if the condition fails validity. That way you don't have to maintain some kind of isValid boolean through all of your checks.

To sum things up: we're really looking at how to write validation code and not general logic; option 2 is better for validation code.

这篇关于从一个函数的早期返回比if语句更优雅?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆