I was reading some C# coding standards and it has this:
"The event handler should not contain the code to perform the required action. Rather call another method from the event handler"
I was wondering if there is a reason for this (performance or something else) or if it is just a style preference?
In programming, an event handler is a callback routine that operates asynchronously once an event takes place. It dictates the action that follows the event. The programmer writes a code for this action to take place. An event is an action that takes place when a user interacts with a program.
Inline event handlers — don't use these You can find HTML attribute equivalents for many of the event handler properties; however, you shouldn't use these — they are considered bad practice.
An event handler is code that is associated with a particular event, such as “button A pressed”. You create (or register) the association between an event and an event handler by calling a function named “on “.
This event handler invokes a JavaScript code when a click action happens on an HTML element. E.g., when we click a button, a link is pushed, a checkbox checks or an image map is selected, it can trigger the onClick event handler. This event handler invokes a JavaScript code when a window or image finishes loading.
An event handler is meant to connect the GUI to your business logic.
If you have a textbox to enter a user's name and an Add button, clicking the Add button should merely call _userRepository.AddUser(UsernameTextbox.Text)
. You don't want business logic in event handlers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With