0

In The Art of Unix Programming, Eric Raymond tells that the data-driven style is about creating small interpreters for "mini-languages" to allow controling the control-flow of the program at runtime.

How is that related to displaying data ? I could not find anything that was about creating DSLs in D3.js

1 Answer 1

2

The d3.js library is an "embedded DSL". It's embedded (using the syntax of the javascript host language), in contrast to a "freestanding DSL", which corresponds to what Eric Raymond is talking about in "The Art of Unix Programming".

And it's a DSL, because it implies a different programming style. In d3.js you describe the relationship between your data and the desired visual, instead of creating loops over loops prescribing what to do next.

To quote from the d3.js page:

Modifying documents using the W3C DOM API is tedious: the method names are verbose, and the imperative approach requires manual iteration and bookkeeping of temporary state. For example, to change the text color of paragraph elements:

var paragraphs = document.getElementsByTagName("p");
for (var i = 0; i < paragraphs.length; i++) {
   var paragraph = paragraphs.item(i);
   paragraph.style.setProperty("color", "white", null);
}

D3 employs a declarative approach, operating on arbitrary sets of nodes called selections. For example, you can rewrite the above loop as:

d3.selectAll("p").style("color", "white");

See this example of d3.js in the wild. with procedural javascript you'll create several loops and selects and subselects, instead of describing what to do with which part of the data.

Another fantastic example of an embedded DSL is "ggplot" from Hadley Wickham, in the R programming language.

9
  • I agree, but I don't understand at all how it is related to the definition of "data-driven" as given by E. Raymond. e.g. a data-driven software is a software where you create your own embedded interpreter; d3 does not seems to be an interpreter for anything. Nov 15, 2016 at 10:40
  • There is a more consensual notion of "data-driven" meaning: you describe how the program/software/system reacts to which kind of data, and the detailed steps to be taken are generated from that description automatically. Then the input data is processed according to the rules you have given. This is a super set of what Eric Raymond says. Nov 21, 2016 at 10:36
  • so a domain specific language doesn't have to be an interpreter on it's own. it can simply be a library that is smooth to use, and that moves from telling the system how to do things with loops to telling the system what to do d3.selectAll(...).style.(...) This is a DSL, just in javascript syntax. hth? Nov 21, 2016 at 10:39
  • 1
    do you have some kind of paper that presents this definiton formally ? Nov 21, 2016 at 14:47
  • 1
    for the record I've also added trustable references to freestanding and embedded DSLs on the Mother of All Wikis, which is especially dedicated to software engineering patterns Nov 25, 2016 at 13:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.