views:

315

answers:

8

This may be subjective, I don't know: I have this problem, which I'm kind of equating to the "what language for this project?" question, since I can't seem to solve it.

I've been commisioned to write a book about a certain domain (let's say a very specific branch of physics) for a very technically savvy community, but who are not programmers. It is a book on this subset of algorithms that they use day in day out.

For this, given my audience, I've been toying with the idea of defining a DSL, instead of making them learn language X, and discuss the algorithms in this light, instead of in a given language or in pseudo-code.

The question is, then: what are some indications that what you need is a DSL rather than a library of functions to be called from a well-established, general-purpose language?

Thanks.

EDIT: Suggestions so far in favor of DSL:

  • Shield from general-purpose language complexity.
  • Make "programmer" more productive in his/her domain.
  • Make language concepts highly intuitive for newbies in programming. (Just thought of this now)
+1  A: 

I would think that the purpose of a DSL was to abstract away details and enable a programmer (or implementor) to be productive by keeping their head in the domain, so to speak.

If you want to explain the details of a collection of algorithms, I would stick with one of the usual suspects (C++?).

An important consideration is whether your audience already use a single language predominately. If so, the choice has been made for you!

Another thing you might want to consider, is that by choosing a popular mainstream language, your book is applicable to a wider audience, without first having to 'learn' a DSL.

Mitch Wheat
+2  A: 

You can use a DSL to decouple a set of related operations from the calling language.

If you want to shield your users from the complexity of the general-purpose language (or restrict their access to that language because they could abuse it), design a DSL.

But if you want the operations to be used in a manner integrated with the calling language, then use a library of functions (or other operators).

Bill Karwin
+2  A: 

I think it will depend on writing a DSL and functional programming languages, as, in my experience, physicists tend to be fairly competent in Fortan or Matlab.

You may want to try using those languages for your examples, as it will enable you to get into algorithms as it may help those that are more technical.

As Mitch Wheat mentioned, DSLs are good when you want to hide the details from the user.

James Black
+2  A: 
  1. Your audience are non-programmers.
  2. You are targeting a specific field.
  3. They need to get job done.

I would choose a DSL over a general purpose language.

AraK
These are probably people that know Fortran and Matlab, as many physicists seem to write their own programs since many programmers can't understand the domain enough to help them, and Matlab is just a very useful tool that allows them to write programs also.
James Black
+2  A: 

You could choose a DSL to match exactly the domain you're dealing with. Programmers can be comfortable with a general purpose language and applying it in many situations. Your non-programmers may benefit from a language that is directly relevant to them. It can also be easier for them to understand if keywords and concepts match their expertise.

Like most things, it's a trade-off. A DSL should be easier for your audience, but it could well be more work for you.

dave
+1  A: 

Ultimately I would look at it this way: a DSL (like any programming language) is a set of lexical tokens and idioms that you can use to explain how to do something. Implicit within that set of tokens is a set of idioms.

The idea of any language is that you select an appropriate one that makes it easier to express expected algorithms.

A good example are reporting languages (eg Natural). That's because reports have common idioms like groups, breaking conditions and so on. Anyone who has done that in a general purpose language would tell you that it can be really tedious to do things like break conditions but it's certainly possible.

So the question you should be asking is this: are there any common idioms that would be tedious, difficult or extremely verbose to express in a general purpose language? You should also be asking: is there a particular subset of functionality that you are most interested in that would benefit by making it a language concept? This could be things like vectors and matrices for linear algebra (with the appropriate operations) or the same for integral/differnetial calculus or differential equations.

What you'd really like to have in the end with a DSL is where, say, a 500 line program could be express as 50-100 lines of your DSL and this applies for most "expected" algorithms. I guess it's a little like normalization in data modelling where the goal is to remove repeating groups. Writing the same 10-20 lines of code with a little variation suggests a common idiom.

As for a library of functions, that is a more limited view of the same thing (ultimately) except that a library of function is merely a package of different behaviours. That ssumes you define a library of functions as just that and don't instead include complex object hierarchies, closures and so on. If you do then the line between a "library of functions" and a DSL is blurred.

I would think of a library of functions as, say, the set of math functions that exist in most modern languages (sin, cos, square root and so forth). So I guess it comes down to:

  • Packages of code -> library of functions
  • Common idioms -> DSL
cletus
A: 

Use psuedo-code to explain your algorithms. That would be more generic than using a general-purpose programming language with library API.

How complex do you feel your DSL will have to be? Will it take a lot for readers to become familiar with it?

Although you would get to fill the back of your book with the source code to the library to fill those pages.

Sean A.O. Harney
A: 
Jerry Coffin