Re: Criteria for a good JSON RDF serialisation

* Toby Inkster wrote:
>Here's my example for Talis' RDF/JSON:
>
>	function get_name (d, homepage)
>	{
>	  var foaf = function (term)
>	    { return 'http://xmlns.com/foaf/0.1/' + term; }
>	  for (var s in d) {
>	    var matches_homepage = false;
>	    if (d[s][foaf('homepage')]) {
>	      for (var i in d[s][foaf('homepage')]) {
>	        o = d[s][foaf('homepage')][i];
>	        if (o['value']==homepage && o['type']=='uri') {
>	          matches_homepage = true;
>	        }
>              }
>            }
>	    if (matches_homepage && d[s][foaf('name')]) {
>	      for (var i in d[s][foaf('name')]) {
>	        o = d[s][foaf('name')][i];
>	        if (o['type']=='literal') {
>	          return o['value'];
>	        }
>	      }
>	    }
>	  }
>	}
>
>24 lines. I think that line count is a good measure of the quality of
>the serialisation. (Low line counts being good.)

JavaScript would seem poorly suited for processing or querying data
structures, but if you must do it, I would recommend you have a look
at Kai Jäger's http://jsinq.codeplex.com/ library which ports .NET's
Enumerable interfaces used by "LINQ" to JavaScript, which should make
the code, erm, more readable, which is kinda more important than the
line count.

First step in rewriting it without using a library:

  function get_name(d, homepage) {
    var foaf = function (term)
      { return 'http://xmlns.com/foaf/0.1/' + term; }
    for (var s in d) {
      if (!d[s][foaf('homepage')])
        continue;
  
      if (!d[s][foaf('name')])
        continue;
  
      var matches_homepage = false;
  
      for (var i in d[s][foaf('homepage')]) {
        o = d[s][foaf('homepage')][i];
        if (!(o['value'] == homepage && o['type'] == 'uri'))
          continue;
        matches_homepage = true;
        break;
      }
  
      if (!matches_homepage)
        continue;
  
      for (var i in d[s][foaf('name')]) {
        o = d[s][foaf('name')][i];
        if (o['type'] == 'literal') {
          return o['value'];
        }
      }
    }
  }
  
This makes the nesting levels more reasonable and breaks out of the
first loop when a match has been found. Then it would seem the loops
should be factored into functions, but we can at least get rid of the
variable there and merge the two filters into one to beat you on the
line count:

  function get_name(d, homepage) {
    var foaf = function (term)
      { return 'http://xmlns.com/foaf/0.1/' + term; }
    for (var s in d) {
      if (!d[s][foaf('homepage')] || !d[s][foaf('name')])
        continue;
  
      for (var i in d[s][foaf('homepage')]) {
        o = d[s][foaf('homepage')][i];
        if (!(o['value'] == homepage && o['type'] == 'uri'))
          continue;
  
        for (var i in d[s][foaf('name')]) {
          o = d[s][foaf('name')][i];
          if (o['type'] == 'literal') 
            return o['value'];
        }
        break;
      }
    }
  }

This should be easier to translate to jsinq aswell, something like
.where(...).where(...).any(...).where(...).select(...).first(...).
I don't think it is all that reasonable to compare data formats based
on JavaScript's lack of support for data strutctures, nowadays you'd
write your JavaScript in Java or C# or Python, or at least use some
library to ease your pain. Though of course evaluating data formats
by seeing how you use them in code generally is a decent metric.

(The code is obviously entirely unreviewed and untested as you did
not provide sample data I could copy and paste, I am not in fact sure
if `d` is an array or other object, or if I've understood what it's
supposed to do... all the nesting levels make that rather hard.)
-- 
Björn Höhrmann · mailto:bjoern@hoehrmann.de · http://bjoern.hoehrmann.de
Am Badedeich 7 · Telefon: +49(0)160/4415681 · http://www.bjoernsworld.de
25899 Dagebüll · PGP Pub. KeyID: 0xA4357E78 · http://www.websitedev.de/ 

Received on Thursday, 10 March 2011 00:32:52 UTC