Tuesday 11 August 2020

Speak the language of the domain

In this post I will share about what DSL and how we can create our own.

Domain Specific languages are specialized languages targeted to domain, it has less vocabulary as compared to general purpose languages. DSL is not new concepts it has been around for ages for eg Cascading Style Sheets  is one of the very popular one that we get exposed to everyday, excel spreadsheet macros, SQL,Build tools like make , Ant etc and many tools in unix like awk,sed etc.

XML can be also categorized as DSL but not sure how many people will love that! 

DSL maps problem domain to solution domain

DSL are built using general purpose language and are targeted for specific domain like 

  • Accounting domain
  • Insurance domain
  • Trading domain
  • Testing etc
DSL is about simplification or removing friction required to solve the problem, good DSL will enable all stakeholders of the product to participate in software development.  
 

DSL Categories

Internal


Internal DSL are written in the same language as host language and use host language compiler to produce executable that is executed by language runtime

Internal domain specific language

Internal DSL can be also seen as a library implemented on top of host language. 

External

External DSL is not written in the same host language , it can be written in some scripting language and then parsed and compiled.  

External domain specified language

 Building external DSL is more involved , it has various new components like lexical analyzer , parser , compiler , code generator.

DSL does not have to be textual , for some domains it makes sense to create visual DSL..


Lets gets started

Enough of talking! show me some DSL.

All the examples shown are based on java, although java is not the best language to build DSL but a lot is possible with such a non expressive language.  

Rule engine

Rule engine is good candidate for  DSL because non tech stakeholder of product understand domain  very well, any simplification for business rules creation will helps with collaboration.


This is a general purpose rule engine that allows clients to define rule and action to take when rule is satisfied. In the above example rule engine is used to decide the discount rate for FX transactions.
An Important design goal about any DSL is that it must have vocabulary from domain and it should be expressive. Expressiveness is a subjective thing and different languages have different support, so host language selection plays an important role.  


State machine

State machine is another general purpose thing that most applications use, every mutation in a system can be modeled as a finite state machine.  


This example takes the popular Circuit Breaker Design Pattern and is implemented as a state machine. State machine domain vocabulary contains states ,events, transition rules. In-case of circuit breaker possible valid states are Open,Half Open, Closed and events are connect, re-connect


Testing framework

Testing framework uses DSL extensively to make tests expressive, some of examples of dsl based testing framework are gherkin(BDD) , scala test , jasmine etc.   


In testing domain specification, scenario & assertions are important concepts. 

Trading System

Investment banking system has a complex domain, DSL in this area gives huge dividends because we can engage domain experts (i.e traders) when the system is built.  



Trading system will have Orders, Instrument (i.e Equity , fixed income etc), order type (i.e buy/sell), price, full or partial order.



How to build 

Now we have seen some real examples of DSL, let's discuss on how to building one.

  • Domain is the core thing in DSL, getting a language that is based on domain core concepts is very important.
  • Implicit context - Remembering context in which terms are used plays an important role in expressiveness.
  •  Well designed abstraction - users with beginner level knowledge should also be able to use language.
  • Tools - Having tools like dsl workbench plays an important role in adoption. 

Trade off 

Nothing comes for free and DSL also have some

  • Build complexity.
  • Learning curve for users of language 
  • Exceptions and error handling are not trivial
  • Performance issue.
  • Integration issue
  • Backward compatibility. 

Conclusion

DSL has huge return on investment when domain is complex.

All the code used in post is available @ github

No comments:

Post a Comment