从函数中提前返回是否比 if 语句更优雅? [英] Is returning early from a function more elegant than an if statement?

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

问题描述

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

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

...或...

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.

那么为什么在这种情况下每个人都喜欢选项 2?这是因为您正在设置一个合约,该函数通过验证传入数据或其他可能需要检查的不变量来强制执行该合约.构建验证的最漂亮的语法是检查每个条件,如果条件无效则立即返回.这样您就不必在所有检查中维护某种 isValid 布尔值.

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.

总而言之:我们真正关注的是如何编写验证代码而不是通用逻辑;选项 2 更适合验证代码.

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天全站免登陆