Event Driven Programming – What is it?

Event Driven Programming – Introduction

In essence, Event Driven Programming (EDP) is a programming paradigm. A paradigm is a distinct set of concepts and methods that form a model or pattern. Furthermore, in computer science, a programming paradigm is a way of categorising programming languages based on their attributes.

Traditionally a computer program is a set of logical steps that accepts input, processes that input and produces an output. This is the case with functional and procedural programming, where the emphasis is on how the data is to be processed rather than the data itself. With event driven programming, the flow of the program is determined by events rather than the logic needed to manipulate data.

An event can be defined as a perceptible occurrence or change that happens in a computer program. In Event Driven Programming, the events are usually monitored and programmed to set off an action when certain parameters are met. Any program that adapts or carries out a task in response to a distinct event is regarded as an Event Driven Program. The following are some of the most basic examples of events:

  • Key Press Events – Where the user of the program presses a particular key.
  • Mouse Click Events – When the user of the program clicks the mouse.
  • Automated background events e.g. a Timer – Where there is a timer running in the background of the program that serves a specific purpose. For example – a timer that is running to trigger an alarm.

In an Event Driven Program, the flow of the program is defined primarily by the user events, such as any of the ones mentioned above. Although the events appear incredibly simple, if implemented correctly they can serve the users with efficient and accurate responses which tend to improve the overall user experience of a system. The selection of events presented to the user grants them better control over the system and its execution flow.

Event Driven Programming – Event Handlers

With event driven programming as the system will be constantly checking for events, there is a need to provide event handlers. By definition, an event handler is a method or function containing statements that will be executed when an event is triggered. The handler is the routine that processes the user action i.e. mouse-click, keystroke etc. Most modern programming frameworks manage the implementation of event handlers and allow the developers to focus on the execution code (the code within the function).

A nice example of a built in event handler is HTML5’s onclick handler. In this case the system is ‘listening’ for the user to click their mouse over the button. HTML5 handles this, and the programmer only has to worry about writing the code to be executed when the event is triggered. In the following sample HTML code, I will write code for a button that when clicked will redirect the user to another web page.

    <title>HTML On Click Example</title>
    <button onclick="window.location.href = 'https://google.com';">Click  Me!

Here, the onclick code tells the page to listen out for when the button is being clicked. This is a simple example of an event handler that is managed by the language. As the programmer I only have to worry about what is to be executed after the event. In this case, the user is redirected to Google. This code creates a button as such;

Click Me Button

If a user clicks the button, the redirection code is executed.

The majority of today’s development environments provide developers with event handler templates, which allows a programmer to focus on the formulating the actual event code. Another example of this is developing a Windows Forms Application in Microsoft Visual Studio. A developer can simply drag and drop controls, and choose from a selection of events associated with that particular code. The IDE will then implement the built in event-handler, and direct the developer to where they should write the code that is triggered by the event. Below is some of the built in event handlers associated with buttons in a Windows Form Application.

VS Event Handlers

If I choose the ‘Click’ event, the IDE will produce an event handler for me and redirect me to that handler in the editor.

Event Driven Programming – Languages

In short, the majority of modern programming languages support event driven development. However, some languages and environments off features and functionality that can make the EDP programming process easier:

  • Visual Basic (VB)
  • C Sharp (C#)
  • Java
  • Visual C++

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: