What is data-driven programming?
I've been tasked at work to write a detailed engineering plan for a logistics application that we are coding to propose to a customer. I have been told that it is a data-driven application. What does it mean for an application to be "data-driven"? What is the opposite? I can't seem to get any really clear answer for this although while web searching I can see many people posting their own examples. Any help would be greatly appreciated.
Solution 1:
Data driven progamming is a programming model where the data itself controls the flow of the program and not the program logic. It is a model where you control the flow by offering different data sets to the program where the program logic is some generic form of flow or of state-changes.
For example if you have program that has four states: UP - DOWN - STOP - START
You can control this program by offering input (data) that represents the states:
- set1: DOWN - STOP - START - STOP - UP - STOP
- set2: UP - DOWN - UP - DOWN
The program code stays the same but data set (which is not of a dynamic input type but statically given to the computer) controls the flow.
Solution 2:
Although there are more than a few ideas as to what data driven programming is, allow me to give an example using a data structure and a function.
Non data driven example:
data_lloyd = {'name': 'Lloyd', 'lives': 'Alcoy }
data_jason = {'name': 'Jason', 'lives': 'London' }
go = function(x)
if x.name == 'Lloyd'
then
print("Alcoy, Spain")
else
print("London, UK")
end
Data driven example:
data_lloyd = {'name': 'Lloyd', 'lives': function(){ print("Alcoy, Spain") }
data_jason = {'name': 'Jason', 'lives': function(){ print("London, UK") }
go = function(x)
x.lives()
end
In the first example the decision to show one result or the other is in the code logic. In the last example the output is determined by the data that is passed to the function and for that reason we say the output is 'driven' by the data.
Solution 3:
"I have been told that it is a data-driven application" - you need to ask whoever told you that.
You don't want to read some plausible answer here and then find out that it's not at all what the person in charge of your project meant. The phrase is too vague to have an unambiguous meaning that will definitely apply to your project.
Solution 4:
Data driven development is something that one can make changes to the logic of the program by editing not the code but the data structure.
You might find more information about data-driven programming here.
Procedural Programming
var data = {
{do:'add',arg:{1,2}},
{do:'subtract',arg:{3,2}},
{do:'multiply',arg:{5,7}},
};
foreach(var item in data){
switch(item.do){
case 'add':
console.log(item.arg[0] + item.arg[1]);
break;
case 'subtract':
console.log(item.arg[0] - item.arg[1]);
break;
case 'multiply':
console.log(item.arg[0] * item.arg[1]);
break;
}
}
Data Driven Programming
var data = {
{do:'+',arg:{1,2}},
{do:'-',arg:{3,2}},
{do:'*',arg:{5,7}},
};
foreach(var item in data){
console.log(eval (item.arg[0] + item.do + item.arg[1]);
}