wood burning stoves*
The moose likes IDEs, Version Control and other tools and the fly likes Generic vs. DSL Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » IDEs, Version Control and other tools
Bookmark "Generic vs. DSL" Watch "Generic vs. DSL" New topic
Author

Generic vs. DSL

Burk Hufnagel
Ranch Hand

Joined: Oct 01, 2001
Posts: 814
    
    3
Terrence,
How does one decide whether to build a Domain Specific Language and when to go the General Programming Language route? While I don't know, I suspect that some domains may be large or complex enough that a DSL for them might border on being a GPL. So I guess I'm wondering where you draw the line - if there is one.

Thanks,
Burk


SCJP, SCJD, SCEA 5 "Any sufficiently analyzed magic is indistinguishable from science!" Agatha Heterodyne (Girl Genius)
Terence Parr
author
Ranch Hand

Joined: Jan 13, 2010
Posts: 35
hiya. I kind of responded to this topic in another post, but let me repeat that I tend to build a DSL when I have to do the same bit of general-purpose programming again and again. Or, sometimes the pain will be so much that I start by building a DSL or tool.

Yep, SQL is a DSL because its domain specific, but it's bigger than many general-purpose programming languages. So implementing SQL would be a huge undertaking. I'd stick with a general-purpose programming language instead of implementing SQL. ;) Thankfully, somebody has already implemented it for us on the various database platforms.
Burk Hufnagel
Ranch Hand

Joined: Oct 01, 2001
Posts: 814
    
    3
Terence Parr wrote:... let me repeat that I tend to build a DSL when I have to do the same bit of general-purpose programming again and again.


Terrance,
Thanks for the reply. I think I'm a little confused though. To me "the same bit of general-purpose programming" sounds more like a reason to create a library or API, not a language. Are there some critical differentiators?

Thanks,
Burk
Terence Parr
author
Ranch Hand

Joined: Jan 13, 2010
Posts: 35
Burk Hufnagel wrote:I think I'm a little confused though. To me "the same bit of general-purpose programming" sounds more like a reason to create a library or API, not a language. Are there some critical differentiators?


Oh, right. good followup. DSLs are like libraries with a different interface. Libraries help a great deal and are a great way to start; you can think of them as factoring out common functionality for reuse by yourself and others just like a DSL. The difference is that sometimes the syntax of your library (method calls etc) is not as readable as a syntax specific to a domain. For example, you can create mathematics libraries but sometimes it's better to use math specific DSL is like Mathematica or Matlab.
Burk Hufnagel
Ranch Hand

Joined: Oct 01, 2001
Posts: 814
    
    3
Terrence,
I think I'm getting the hang of this. So if I create a pizza builder class that has a "humane" interface and lets you order a pizza like this:
Pizza myPizza = PizzaBuilder.makeA(LARGE).pizza().with(THIN_CRUST).and().with(PEPPERONI).and().with(EXTRA_CHEESE);
I've actually created a DSL for placing a pizza order.

Am I on the right track or way out in left-field?

Burk
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Burk Hufnagel wrote:Terrence,So if I create a pizza builder class that has a "humane" interface and lets you order a pizza like this:
Pizza myPizza = PizzaBuilder.makeA(LARGE).pizza().with(THIN_CRUST).and().with(PEPPERONI).and().with(EXTRA_CHEESE);
I've actually created a DSL for placing a pizza order.

Yes, an internal DSL. And I might come up with a non-internal DSL to be used like so:


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
nehaa arora
Greenhorn

Joined: Oct 27, 2009
Posts: 14
Hi,

I am getting a feel for DSL through the discussions since yesterday. If i am getting it right DSLs are more about the idea of kernel application with pluggable utilities/services. Is this a limiting view?

Further, aren't DSLs a huge undertaking in terms of maintainability and usability?

thanks,
-Neha
Burk Hufnagel
Ranch Hand

Joined: Oct 01, 2001
Posts: 814
    
    3
nehaa arora wrote:If i am getting it right DSLs are more about the idea of kernel application with pluggable utilities/services. Is this a limiting view?

Further, aren't DSLs a huge undertaking in terms of maintainability and usability?


Nehaa,

I think creating applications that support pluggable utilities/services are anything but limiting - you're allowing people to add new capabilities without modifying the basic application - just like Eclipes and NetBeans. Look at all the capabilities these platforms have because they support plugins.

As for the cost, I suspect that it's similar to the cost of creating your own API or library and that depends on how well you design and write the underlying code.

Burk
nehaa arora
Greenhorn

Joined: Oct 27, 2009
Posts: 14
Burk,

I share your view on the plus of pluggable application framework. I intended to use 'limiting' in my question to mean if i am viewing the DSL application domain, wholly or just an aspect (ie. creating application architectures that support pluggability).

Burk Hufnagel
Ranch Hand

Joined: Oct 01, 2001
Posts: 814
    
    3
nehaa arora wrote:Burk,

I share your view on the plus of pluggable application framework. I intended to use 'limiting' in my question to mean if i am viewing the DSL application domain, wholly or just an aspect (ie. creating application architectures that support pluggability).



Nehaa,
If you consider things like XML, regular expressions, CSS, and ant as DSLs then I believe that what we've been talking about is definitely a small aspect of the whole.
Burk
nehaa arora
Greenhorn

Joined: Oct 27, 2009
Posts: 14
Yes Burk, that sounds quite right

thanks,
Neha
Terence Parr
author
Ranch Hand

Joined: Jan 13, 2010
Posts: 35
Burk Hufnagel wrote:So if I create a pizza builder class that has a "humane" interface and lets you order a pizza like this:
Pizza myPizza = PizzaBuilder.makeA(LARGE).pizza().with(THIN_CRUST).and().with(PEPPERONI).and().with(EXTRA_CHEESE);
I've actually created a DSL for placing a pizza order.


Some may disagree, but I don't think that's a DSL. it looks like Java to me with a nice API. That's a good idea, but not a DSL. Otherwise, all of my code would be in a multiple DSLs. ;) Your 2nd example has a DSL in the string: "+pepperoni +triplecheese" etc...

Internal DSL to me is one where you create NEW syntax but within the existing framework of the programming language. 'course that ain't my term. It's Fowlers'. I should probably go read his def again.
Burk Hufnagel
Ranch Hand

Joined: Oct 01, 2001
Posts: 814
    
    3
Terence Parr wrote:Some may disagree, but I don't think that's a DSL. it looks like Java to me with a nice API. That's a good idea, but not a DSL. Otherwise, all of my code would be in a multiple DSLs. ;) Your 2nd example has a DSL in the string: "+pepperoni +triplecheese" etc...

Internal DSL to me is one where you create NEW syntax but within the existing framework of the programming language. 'course that ain't my term. It's Fowlers'. I should probably go read his def again.


Dang. I thought I was getting the hang of it. So are you saying that when creating a DSL in Java we need to pass one or more Strings to an interpreter/renderer so that we're not using the Java syntax?

BTW, that second example was Lasse's -- good job Lasse!

Burk
dhaval yoganandi
Ranch Hand

Joined: Jun 06, 2009
Posts: 35

Dang. I thought I was getting the hang of it. So are you saying that when creating a DSL in Java we need to pass one or more Strings to an interpreter/renderer so that we're not using the Java syntax?


sorry to interrupt. But if that is the case then I was in wrong impression about DSL. I was thinking internal languages of companies as DSL.


a.k.a stalker.
Burk Hufnagel
Ranch Hand

Joined: Oct 01, 2001
Posts: 814
    
    3
dhaval yoganandisorry to interrupt. But if that is the case then I was in wrong impression about DSL. I was thinking internal languages of companies as DSL. [/quote wrote:

That may still be true.
Terence Parr
author
Ranch Hand

Joined: Jan 13, 2010
Posts: 35
Burk Hufnagel wrote:Dang. I thought I was getting the hang of it. So are you saying that when creating a DSL in Java we need to pass one or more Strings to an interpreter/renderer so that we're not using the Java syntax?


Ok, I just read Martin Fowler's internal DSL definition again. I believe we are in agreement. He says, "Internal DSLs are limited by the syntax and structure of your base language." SO, they are still valid programs in, say, Java or Ruby but you're trying to make it look like another language (a DSL). Ruby lets you do this easily; see Ruby on Rails. Java not so much. You can say "a.add(b)" for but ruby lets you say "a + b" for some weird types like 3D vectors for a game language. In C/C++, the preprocessor lets you do some fun stuff like:

[code]
TURN_LEFT;
TURN_LEFT;
SHOOT_LASER(FORWARD);
[code]

Technically, that's not C though. The C compiler only sees the result of running the macro preprocessor on that.

Internal DSLs are the quickest way to get rolling since you are really just building a library, which you do everyday. When the syntax of the implementation language restricts the expressiveness of your DSL too much, time to build an external DSL. When you need to build a parser, you truly have an external DSL on your hands.

Look at it this way. Internal DSLs are just proper subsets of a programming language so your creativity is constrained and, of course, nonprogrammers can't use them. Internal DSLs *are* libraries so I think of them as that. But, I'm biased towards inflexible languages like Java. In Ruby, I could make function calls that don't look like calls. This let's me create Ruby programs that don't look like what I read in the tutorials...a negative in my view, btw.

Just to confuse you, the char within a string can be anything you want so you can put SQL or StringTemplate or whatever you want in there. The same string can live comfortably in python, C, ruby, SNOBOL, Java, C# etc... To execute that string, you need to implement a parser and an interpreter or translator.
Terence Parr
author
Ranch Hand

Joined: Jan 13, 2010
Posts: 35
dhaval yoganandi wrote:I was thinking internal languages of companies as DSL.


By internal, Martin Fowler means written with in the constraints of another language. More like a library than new syntax.
dhaval yoganandi
Ranch Hand

Joined: Jun 06, 2009
Posts: 35

Terence Parr wrote:
dhaval yoganandi wrote:I was thinking internal languages of companies as DSL.


By internal, Martin Fowler means written with in the constraints of another language. More like a library than new syntax.


If its library kind of thing. Then I was in wrong impression about DSL. I was talking about the internal language implemented by using the custom parser/interpreter. Thanks Mr. Terence for info.
Burk Hufnagel
Ranch Hand

Joined: Oct 01, 2001
Posts: 814
    
    3
Terence Parr wrote:Ok, I just read Martin Fowler's internal DSL definition again. I believe we are in agreement. He says, "Internal DSLs are limited by the syntax and structure of your base language." SO, they are still valid programs in, say, Java or Ruby but you're trying to make it look like another language (a DSL).

<snip>

Look at it this way. Internal DSLs are just proper subsets of a programming language so your creativity is constrained and, of course, nonprogrammers can't use them. Internal DSLs *are* libraries so I think of them as that. But, I'm biased towards inflexible languages like Java. In Ruby, I could make function calls that don't look like calls. This let's me create Ruby programs that don't look like what I read in the tutorials...a negative in my view, btw.


So then my pizza example would be considered a valid internal DSL - right?
Terence Parr
author
Ranch Hand

Joined: Jan 13, 2010
Posts: 35
Burk Hufnagel wrote:So then my pizza example would be considered a valid internal DSL - right?




Would probably count as an internal DSL as it reads pretty well as a pizza language, despite being in Java. Smalltalk for example could do a little bit better:



That is definitely an internal DSL so I guess the Java version is too. If you modified the library so it didn't read so well, it would probably tiptoe over the blurry line into just an API; check this out:



The and() and with() methods here are the key distinguishing element between an internal DSL and just an API. Heh, maybe we should write that down somewhere. oh, we just did! thanks for the example, Burk!
Burk Hufnagel
Ranch Hand

Joined: Oct 01, 2001
Posts: 814
    
    3
Considering you did all the work, you're very welcome.

Actually, I think it makes a nice example.

Thank you!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generic vs. DSL
 
Similar Threads
Software Factories / MPS
DSL
Background required for DSL
Creating Domain-Specific and General Programming Languages
Domain Specific Language (DSL).