[removed] inline functions vs predefined functions

前端 未结 13 1956
心在旅途
心在旅途 2020-12-02 11:00

Can any body throw me some arguments for using inline functions against passing predefined function name to some handler.

I.e. which is better:

13条回答
  •  孤街浪徒
    2020-12-02 11:37

    I tend towards named functions as well. Anonymous function refs are quick, but should only be used for simple stuff. My rule of thumb is that if the function is more than 2 lines of code, it probably belongs in it's own definition.

    This is complicated by most example code making use of Anonymous functions. But the samples are usually very simplistic. The method falls apart as things get more complicated. I've seen function refs nested in function refs as the developer realized that more callbacks were needed in subsequent steps. Instead of this tree based logic, I prefer the organization of isolated functions.

    And usually end up happy that I can reuse one of the functions I define later.

    One important use of an Anonymous Function is when you need to pass scoped data into your function call, but then I usually just wrap my function into the anonymous function.

    Named functions are also absolutely necessary if you ever get into Test Driven Development.

提交回复
热议问题