DEV Community

Discussion on: Data directed programming

Collapse
 
niharmore33 profile image
nihar

Data-driven programming can probably have different meanings, but here is the one I use it for: it is a style of programming in which specialization is done through data structures and not boilerplate code.
You can learn many programming languages here: hackr.io/

Examples are probably a better way to understand the concept.

Instead of writing (pseudocode):

DoSomething(a,b)
DoSomething(c,d)
DoSomething(e,f)[

You write:

things_to_do = [ [a,b], [c,d], [e,f] ]
for each thing in things_to_do
DoSomething(thing)

If you have first-class functions, instead of writing:

switch x
case a
f(y)
case b
g(y)

You write:

switch = { a: f, b: g}
switchx

Now, more realistic examples. This one is extracted from a Ruby library I wrote, and it makes some methods defined on Arrays work on Strings. I think it illustrates the power of data-driven programming coupled with metaprogramming quite well.

methods = %w{first first= head head= init last last= tail}
methods.each{|n| define_method(n){|*a,&b| chars.to_a.send(n,*a,&b).join}}

Collapse
 
alancampora profile image
Alan Campora • Edited

Actually, the examples seem to be similar to the issue we're solving in the article. If I try to adapt your solution to the issue of having two different representations, it could look like this: data = {"polar": function realPart(){...}, "rectagular": function realPart(){}}, and then somehow you give that data to the switch =D.
It's great to see that data driven programming can also be implemented in contexts!