Tuesday, November 20, 2012

Data is Code and Code is Data

So I was reading a really good article on The Nature of Lisp and came across a really interesting concept. In the article Slava Akhmechet gives an example of some pretty simple xml data like so.



[sourcecode language="xml"]
<todo name="housework">
<item priority="high">Clean the house.</item>
<item priority="medium">Wash the dishes.</item>
<item priority="medium">Buy more soap.</item>
</todo>
[/sourcecode]

Nothing crazy just a todo list that could very easily be parsed and displayed in an app. Now let's step back for a second and consider a big hypothetical. Let's pretend us humans in our physical existence were controlled by computers. Now that we've brought this data into a different context we can see that this "data" could just as easily be code. When we look at a todo list we are basically looking at a set of instructions or code. Take Slava's more apparent second xml example.



[sourcecode language="xml"]
<define-function return-type="int" name="add">
<parameters>
<param type="int">arg1</param>
<param type="int">arg2</param>
</parameters>
<body>
<return>
<add value1="arg1" value2="arg2" />
</return>
</body>
</define-function>
[/sourcecode]

Now this is starting to look a bit more like a set of instructions and thats because it is. This could have just as easily been written in php as



[sourcecode language="php"]
function add($arg1, $arg2) {return $arg1 + $arg2;}
[/sourcecode]

My first thoughts seeing this was that xml could very well be an amazing platform for portable code, I'm speaking language portability here. A short scenario of the usefulness of this would go like this. I write an app in C and want to port the logic over to a web app. Instead of completely reverse engineering the C into php, I take my C source code pass it though a parser and spits out a highly formalized xml file that can then have the reverse process done on the php side. Mind you it would be a bit of an undertaking to create the xml specifications well enough to take into account the nuances of the many languages it may be de-parsed into, but the benefits would be enormous. Reusability of code is always an issue, no one likes spending 6 months on a project only to have their boss change they're mind last minute and drop the project (true story). Just some food for thought, all you programmers out there get crackin' .I expect to see the specs by next year...

No comments:

Post a Comment