It's considered a bad pratice use return to end a function?

后端 未结 5 1154
忘了有多久
忘了有多久 2021-01-19 10:46

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
{
           


        
5条回答
  •  囚心锁ツ
    2021-01-19 11:19

    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.

提交回复
热议问题