使用 return 结束函数被认为是一种不好的做法? [英] It's considered a bad pratice use return to end a function?

查看:50
本文介绍了使用 return 结束函数被认为是一种不好的做法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名 PHP 和 ActionScript 开发人员,在我的一些函数中,我使用 return 来结束它.示例:

I'm a PHP and ActionScript developer, and in some of my functions I use return to end it. Example:

private function lolsome(a:String):void
{
   if(a == "abs"){return void;}

   // function's code      
}

我可以将函数的代码放入其else,但我更喜欢这种方式,因为在我看来,这样更易​​读.我只是想知道这是否被认为是一种不好的做法或类似的东西.

I could just place the function's code into its else, but I prefer this way because in my opinion, this is more legible. I just want to know if this is considered a bad practice or something like that.

谢谢!

推荐答案

如果您在复杂的长函数中返回,我只会认为这是不好的做法,因为其他人在查看算法时可能更难理解它.然而,一开始就拥有大的长函数是不好的做法(它们通常应该被拆分成多个更小的函数).

I would only consider it bad practice if you have returns inside long complicated functions because it can be harder for someone else to understand the algorithm when looking at it. However, it is bad practice to have big long functions in the first place (they should generally be split up into multiple smaller functions).

总的来说,我会考虑在函数开始时验证参数和状态,然后返回到良好的实践,甚至不仅仅是确定.

Overall, I would consider validating parameters and state at the beginning of a function and just returning to be good practice, not even just ok.

但仍然要注意不要在主逻辑中乱扔具有多个不同返回值的函数.

But still be careful not to litter a function with several different returns within the main logic.

这篇关于使用 return 结束函数被认为是一种不好的做法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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