Code in Event Handlers?

Moving code to it's own method

Page 1 of 1

8 Replies - 865 Views - Last Post: 03 March 2010 - 01:45 PM

#1 EvLSnoopY  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 16
  • View blog
  • Posts: 93
  • Joined: 24-November 09

Code in Event Handlers?

Posted 02 March 2010 - 05:23 PM

I was reading through some Conventions and Best Practices a few days ago and I thought that I would get someone else's take on the following but first let me post up a link to the document that I was reading through; it's a pretty good read. Anyway, in the preceding document it states that instead of putting your algorithm(s) into your Event Handlers that it is better to put your code in it's own method and call that method from your Event Handler.

For the sticklers out there this is what the document says verbatim:

Quote

The event handler should not contain the code to perform the required action. Rather call another method from the event handler.


Now as of late I have adopted this coding practice as my own and I must say that it has made my code flow more and has made my code a lot cleaner. I find my self directing events that have similar needs to one method instead of how I used to do things by writing the code twice (once in each event handler) with only one line of code difference between the two algorithms.

My questions are, is this really a best practice? and if so, is this coding style commonplace in real world applications?

Is This A Good Question/Topic? 1
  • +

Replies To: Code in Event Handlers?

#2 JackOfAllTrades  Icon User is online

  • Saucy!
  • member icon

Reputation: 5954
  • View blog
  • Posts: 23,215
  • Joined: 23-August 08

Re: Code in Event Handlers?

Posted 02 March 2010 - 05:55 PM

Moved to C# Programmers
Was This Post Helpful? 0
  • +
  • -

#3 MentalFloss  Icon User is offline

  • "ADDICTED"[2:5]
  • member icon

Reputation: 525
  • View blog
  • Posts: 1,397
  • Joined: 02-September 09

Re: Code in Event Handlers?

Posted 02 March 2010 - 05:57 PM

It may not be commonplace because I see a ton of code directly in the handlers. It is easy and quick though.
I wrote a blog post about this and my opinion has not changed so I'm just going to directly quote it.

Quote

I see a lot of code posted on the forum with button click event handlers.

Within those handlers is the actual operations.

You know why this is a problem? Everything in this click event can only be called by passing "this" and mocking the event arguments. This very tightly ties the code to the UI.

Every event handler should simply call another method containing the operation chain. With this in place, it is now possible to easily call the code elsewhere. Functionality doesn't get accidentally duplicated and you can cleanly create command line functionality that behaves the same as your UI.

If it's all in the click event itself, it's only going to degrade your code from the outside. There will be mocked calls on it that will probably pass null args. Now you're obligated to check for null. Go review your code - are you checking for null on the args everywhere? Probably not...


(There, now you don't have to actually go to it.)

EDIT: The concept is not strictly tied to the Click event handlers but it's not exactly all event handlers either.

Furthermore, with the refactor abilities in VS, it's as simple as extracting to method. So, go ahead and write the code in the event handler as a Q&D approach, but once you know what you're doing with it, promote it to its own method (usually)

This post has been edited by MentalFloss: 02 March 2010 - 05:59 PM

Was This Post Helpful? 4
  • +
  • -

#4 EvLSnoopY  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 16
  • View blog
  • Posts: 93
  • Joined: 24-November 09

Re: Code in Event Handlers?

Posted 02 March 2010 - 06:22 PM

First of all I would like to apologize to JackOfAllTrades and then say thanks for moving my post to the correct place. And MentalFloss thanks for your response. Basically I needed reassurance that what I had adopted was actually a best practice.
Was This Post Helpful? 0
  • +
  • -

#5 Adkins  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 66
  • View blog
  • Posts: 560
  • Joined: 27-October 09

Re: Code in Event Handlers?

Posted 03 March 2010 - 12:52 AM

My caffeine intake hasn't kicked in yet so I gotta ask MentalFloss. What is Q&D? Other than that though it is some good stuff that I have personally never thought of.
Was This Post Helpful? 0
  • +
  • -

#6 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: Code in Event Handlers?

Posted 03 March 2010 - 06:20 AM

I think MentalFloss summed it up for everybody.
Was This Post Helpful? 0
  • +
  • -

#7 MentalFloss  Icon User is offline

  • "ADDICTED"[2:5]
  • member icon

Reputation: 525
  • View blog
  • Posts: 1,397
  • Joined: 02-September 09

Re: Code in Event Handlers?

Posted 03 March 2010 - 07:03 AM

Quote

What is Q&D?


Sorry. Quick and Dirty. You know? Just get it done to a working point as easily as possible.
Was This Post Helpful? 0
  • +
  • -

#8 Adkins  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 66
  • View blog
  • Posts: 560
  • Joined: 27-October 09

Re: Code in Event Handlers?

Posted 03 March 2010 - 08:44 AM

now that the caffeine is in full force it makes perfect sense. Thanks for the clarification.
Was This Post Helpful? 0
  • +
  • -

#9 Core  Icon User is offline

  • using System.Linq;
  • member icon

Reputation: 773
  • View blog
  • Posts: 5,097
  • Joined: 08-December 08

Re: Code in Event Handlers?

Posted 03 March 2010 - 01:45 PM

This is indeed a good practice, especially considering that major parts of my code (read - functions) are used in different event handlers (with little modification in its code). Saves space and time when it comes to adding/fixing specific aspects. For example, if I want to add a part that verifies something before executing the actual code, I can do this in one place instead of doing it in every single event handler where the code is placed.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1