Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Functionality code in event handler bad practice?

Tags:

c#

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?

like image 905
Firedragon Avatar asked Nov 08 '11 09:11

Firedragon


People also ask

What is an event handler function?

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.

Are inline event handlers bad?

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.

What is event handler code?

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 “.

What are event handlers and give an example of an event handler?

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.


1 Answers

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.

like image 176
CodeCaster Avatar answered Sep 21 '22 18:09

CodeCaster