Click here to Skip to main content
15,898,222 members
Articles / Programming Languages / C#

Why Events? Flexibility

Rate me:
Please Sign up or sign in to vote.
3.00/5 (2 votes)
31 Jul 2013CPOL4 min read 11.4K   4   13
Flexibility

Background

There are many different approaches to developing software, but in my opinion, the opposite ends of the spectrum end up being:

  1. Knowing how the whole system looks, feels, and operates before coding a single line
  2. Having an idea of what the user wants and coding to make it happen

Although I'm generalizing a lot here, it's sort of like the battle between Waterfall and Agile. Okay, great. So what am I rambling on about here?

Well, in the first case, you know all the ins and outs of the system. You can structure your system so that almost no matter how complex it is, you can ensure that method A is always run immediately after method B which is etc. The design is completely controlled. You have a spec for how all the components work together. The problem? Well, most of this will be reserved for another write-up, but in our case there's no flexibility. If that spec changes after your complicated system is already in place, how much code do you have to go change?

Enter: Events

Okay, so I hinted at why something like Waterfall might cause some headaches, but you don't need to develop your software in an agile fashion to have flexibility. Events can help you with that!

If you're not already familiar with what an event is, it's simply a delegate that one would call when some particular criteria is met. In C#, which is what I'll be assuming for this write-up, you add event handlers to an event so that when the event is invoked, the event handlers get called (and they get called in the order that they were "hooked" onto the event). Splendid.

So what's great about events? How are they going to fix your brittle code? Well, if you approach software design in terms of components in a system and how they interact, it's really beneficial to think about how certain things react to each other. If you're planning your whole system out ahead of time, you could just always call method A, then method B, and then method C in order every time you want something in particular to happen. But is that flexible? What happens when method B now calls an additional method, let's say method D, due to some architectural changes that had to be made. Well... That's all fine and dandy unless you have scenarios where you don't want to run D in your A,B,C method calls you have everywhere.

So, what if you had thought about it this way:

  1. I want B to run once A is run.
    • I need an event for A.
    • I need to call method B in my event handler for event A.
  2. I want C to be run once B is run.
    • I need an event for B
    • I need to call method C in my event handler for event B.

And then someone comes along and throws method D into the mix. Well, you know that there is at least one particular case where you want D to be run after B is run... So make another event handler! Your listing now changes to:

  1. I want B to run once A is run.
    • I need an event for A.
    • I need to call method B in my event handler for event A.
  2. I want C to be run once B is run.
    • I need an event for B.
    • I need to call method C in my event handler for event B.
  3. I want D to be run when condition X is met after B is run.
    • I need an event for B (which I already have!).
    • I need to call method C in my event handler for event B.
      • I can add a whole second event handler for event B (so I get C then D called)
      • OR I can modify the existing event handler that calls method C to conditionally call method D when necessary

And now how do you check the condition? Pass in some fancy event args with your state.

Summary

Events can help you keep your code flexible when your structure is changing because they provide hooks into the code when certain conditions are met. You don't need to rely on a firmed-up design for how methods are required to be called in some particular order. If your design changes because you want something to execute under different conditions, just hook up to the event that meets those conditions instead and re-write minimal code.

I'm huge on using events in my architectures, so I'll try to write up a few more posts on events and why I like them. The flexibility I just described is only one very small part of it, and it certainly requires that you shift your perspective on how components in a system interact.

This article was originally posted at http://ncosentino.us.to/2013/07/15/why-events-flexibility

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader Microsoft
United States United States
I'm a software engineering professional with a decade of hands-on experience creating software and managing engineering teams. I graduated from the University of Waterloo in Honours Computer Engineering in 2012.

I started blogging at http://www.devleader.ca in order to share my experiences about leadership (especially in a startup environment) and development experience. Since then, I have been trying to create content on various platforms to be able to share information about programming and engineering leadership.

My Social:
YouTube: https://youtube.com/@DevLeader
TikTok: https://www.tiktok.com/@devleader
Blog: http://www.devleader.ca/
GitHub: https://github.com/ncosentino/
Twitch: https://www.twitch.tv/ncosentino
Twitter: https://twitter.com/DevLeaderCa
Facebook: https://www.facebook.com/DevLeaderCa
Instagram:
https://www.instagram.com/dev.leader
LinkedIn: https://www.linkedin.com/in/nickcosentino

Comments and Discussions

 
Questiongreat Pin
S Leistikow22-Jul-13 17:52
S Leistikow22-Jul-13 17:52 
AnswerRe: great Pin
Dev Leader23-Jul-13 1:12
mvaDev Leader23-Jul-13 1:12 
GeneralRe: great Pin
S Leistikow9-Oct-13 3:23
S Leistikow9-Oct-13 3:23 
GeneralRe: great Pin
Dev Leader9-Oct-13 4:36
mvaDev Leader9-Oct-13 4:36 
Question[My vote of 1] Comment Pin
FatCatProgrammer21-Jul-13 11:08
FatCatProgrammer21-Jul-13 11:08 
I can see this only working for 3 methods then becomes the bigest nightmare. Structured programming and events are not interchangeable!
Relativity

AnswerRe: [My vote of 1] Comment Pin
Dev Leader21-Jul-13 11:19
mvaDev Leader21-Jul-13 11:19 
GeneralRe: [My vote of 1] Comment Pin
FatCatProgrammer22-Jul-13 7:27
FatCatProgrammer22-Jul-13 7:27 
GeneralRe: [My vote of 1] Comment Pin
Dev Leader22-Jul-13 7:50
mvaDev Leader22-Jul-13 7:50 
GeneralMy vote of 3 Pin
Klaus Luedenscheidt20-Jul-13 19:10
Klaus Luedenscheidt20-Jul-13 19:10 
GeneralRe: My vote of 3 Pin
Dev Leader20-Jul-13 19:21
mvaDev Leader20-Jul-13 19:21 
GeneralRe: My vote of 3 Pin
Klaus Luedenscheidt21-Jul-13 20:10
Klaus Luedenscheidt21-Jul-13 20:10 
GeneralRe: My vote of 3 Pin
Dev Leader22-Jul-13 1:09
mvaDev Leader22-Jul-13 1:09 
GeneralRe: My vote of 3 Pin
S Leistikow22-Jul-13 17:50
S Leistikow22-Jul-13 17:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.