Is it bad practice to write inline event handlers

前端 未结 3 1567
南方客
南方客 2021-02-04 23:17

Is it bad practice to write inline event handlers ?

For me, I prefer use it when I want to use a local variable in the event handler like the following:

I prefer

3条回答
  •  执念已碎
    2021-02-04 23:55

    In most cases I would rather have the separate methods like “timer_Tick()”, however I should rather it be called OnTimerTick() as:

    • When I read the class, it is clearer wheat is going on. The “On” tells me its can event handler.
    • It is easier to set a break point in the method in the “inline” case.
    • The event is fired a long time after “Foo” contractor has returned, and I don’t think it as running in t the scope of the contractor.

    However if the event will only be fired before the method it is declared in-line returns and the object the event is set on has a scope what that is limited to the declaring method, then I think the “in line” version is better. Hence I like using “in line” for the compare delegate being passed to a “sort” method.

提交回复
热议问题