A lot of code is still written as a sequence of if-then-else statements, which is perfectly fine, however in some cases a data driven programming approach can prove to be more flexible and reduce the amount of code that needs to be written and maintained.

It is important to note I am not arguing the merits of one method over the others, instead I would like to introduce you to the concept and show you how it can be helpful. We will discuss some pros and cons towards the end of this post.

Traditional (non-data driven) Solution

The following code snippet shows a typical coding solution of sending a set of emails to a user who just happen to sign up on our website.

In this implementation, as new rules and scenarios need to be added, we will continue to make code changes to meet the requirements. Overtime this will become more and more complicated to read and maintain, specially when there is a large number of scenarios to cover.

Data Driven Solution

A data driven programming approach simplifies this solution by separating the logic and data as you can see in the following code.

Pros and Cons

The biggest benefit of separating the data and the code is that the data can now be moved completely out of code. In the example above I used a local rule_set structure, but I might very well read it through a file or database or download it over the network. Even non-programmers can maintain and control the rule set without writing any code.

For example adding a different welcome message for a new supported country, for example Mexico in Spanish, would require minimum code changes. However if a new type of rule needs to be added then yes we will need to write code to support that.

On the down side, for some of the complex data driven applications you will find that debugging may not be straight forward because a bug may just depend on the data and state of the program, this is specially true when we have a large set of rules to process. I recommend spend extra time in designing data driven applications, read Coding By Thinking post for some advice on how to do that.

In addition the implementation could get tricky when rules have inter-dependencies or when the order of processing matters.

Cover Photo by Lorenzo from Pexels

Introduction to Data Driven Programming
Tagged on: