Writing Custom DSLs
Neil Green
Today’s Talk
• The Evolutionary History of DSLs
• What a DSL is and what it is for
• Defining your DSL’s Grammar
• Impleme...
The Evolutionary History of DSLs
How Can We Create a Machine Language
that Reads Like a Human Language?
?
Punch Cards
How Humans First Spoke to Machines
Low Level Languages
Higher Level Languages
High Level Languages
Object Oriented Languages
Domain Modeling
User
Interface
Model Database
Models as Data Transfer
User
Interface
Domain Model
(Business Logic)
Database
Models as Business Logic
MVC Pattern
ViewModel
Controller
View
Domain Model
Controller
MVC for Complex Applications
Creating A Domain Model
1. Understand your domain
2. Model your domain
3. Implement your domain
Understand Your Domain
Speak with the
customer
Develop a mutual
language
Implement the
Model
Diagram a
Model
Nouns
Classes...
Model the Domain
Implement the Domain Model
Speculative Implementation
(guessing based on assumptions)
Implement the Domain Model
Write
Test
Write
Code to
Pass Test
Refactor
Code
Implementing the Domain Model
Behavior Driven Development
Write Tests
Write Tests
Alone
Write Test
with
Customer
Test Driv...
Language of Test = Language of Domain
Have the Customer Express
Expectations Directly in Code
What a DSL is and what it is for
Domain Specific Language
1. Grammar
2. Implementation
3. Rules
Common DSLs
General DSLs
• SQL
• Regular Expression
• Unix shell scripts
JavaScript DSLs
• jQuery
• D3js
• Gulp
• Mocha/Ch...
DSLs Make Complex Problems Simpler
select * from Table
DSLs Also Simplify Business Problems
• Automotive
• Banking
• Consumer
• Education
• Engineering
• Energy
• Oil and Gas
• ...
Business DSLs get their Grammar
from the customer NOT THE PROGRAMMER
Business DSLs are for customers Technical DSLs are fo...
Defining your DSL’s Grammar
Defining the Grammar
Programmer
Customer
Domain Model
The Alarm Problem Domain
“Hey Paul, how's it going? Yesterday our office admin came in early to
setup for the companies weekly status meeting, but ...
“Hey Paul, what's up? I know I asked you to not have the
alarm set for our weekly status meetings, but during
memorial day...
Grammar
Hey Paul, how's it going? Yesterday our office admin came in early to
setup for the companies weekly status meetin...
Grammar
Hey Paul, how's it going? Yesterday our office admin came in early to
setup for the companies weekly status meetin...
Grammar
• Our
• Office admin
• Code
• Alarm
• Yesterday
• Early
• Weekly
• Every Monday morning
• Memorial day
• July 4th
...
Grammar
• Our  Company
• Office admin  Employee & Role
• Code
• Alarm
• Not
• Should
• Set  On
• On
• Off
VerbsNouns (t...
Idiom
• New Year’s Day
• Birthday of Martin Luther King, Jr.
• Washington’s Birthday
• Memorial Day
• Independence Day (“J...
Grammar
• Company
• Employee
• Role
• Code
• Alarm
• Not
• Should
• On
• Off
VerbsNouns (things)
• Day
• 6:00am, 9:00am, 1...
Grammar
Alarm on weekdays* 8:00am-6:00pm
Alarm off Mondays 6:00am-8:00am
Alarm on federal holidays
* Weekdays = Every Mond...
Grammar
The Alarm should be on every weekday
between 8:00am-6:00pm, but the alarm
should be off Mondays from 6:00am-8:00am...
Verify Grammar
“Yeah, this is pretty good, only
thing is, we don’t close for
Columbus day”
Grammar
The Alarm should be on every weekday
between 8:00am-6:00pm, but the alarm
should be off Mondays from 6:00am-8:00am...
Grammar
• Company
• Employee
• Role
• Code
• Alarm
• Not
• Should
• On
• Off
VerbsNouns (things)
• Day
• Every
• Week
• We...
Grammar
• Alarm • Not
• On
• Off
VerbsNouns (things)
• Weekdays; “Mondays”
• Federal Holiday; “Columbus Day”
• 6:00am, 8:0...
(security guard by day, programmer by night)
Implementing the Grammar
Implementing the Grammar
• Compiler?
• Lexer?
• Parser?
• Tokenizer?
• Interpretor?
• Symbol table?
Implementing the Grammar
Method Chaining!
(Fluent Interface)
Implementing the Grammar
Alarm on weekdays 8:00am-6:00pm
Alarm off Mondays 6:00am-8:00am
Alarm on federal holidays
Alarm o...
Implementing the Grammar
Alarm.on().weekdays(‘8:00am’, ’6:00pm’);
Alarm.off().Mondays(‘6:00am’, ‘8:00am’);
Alarm.on().fede...
Implementing the Grammar
alarm = new AlarmDsl();
alarm.on().weekdays(‘8:00am’, ’6:00pm’);
alarm.off().Mondays(‘6:00am’, ‘8...
Configuring the Domain
Using objects:
new AlarmSchedule({
on: true,
startTime: ‘8:00am’,
endTime: ‘6:00am’
});
Configuring the Domain
Using functions:
AlarmSchedule.isOn = isTimeBetween(
timeRange(‘8:00am’, ‘6:00am’)
);
Paul’s Written a Custom DSL!!!
How to Write a Custom DSL
1. Review Conversation with Customer
2. Remove meaningless words
3. Separate nouns and verbs
4. ...
Implementing your DSL
New Domain: Shipping
Shipping Problem Domain
• Customer
• Destination
• Order
• Item
• Box
• Delivery Date
• Shipping Cost
• Dimensions
• Weigh...
Shipping Domain Model
Shipping DSL Grammar
when shipping domestically
if delivery date is flexible ship as cheaply as possible
if delivery date ...
Shipping DSL Grammar
when domestically
if flexible cheaply
if specified pass
when internationally
then cheaply
Shipping DSL Grammar
when().domestically().
if().flexible().cheaply().
if().specified().pass().
when().internationally().
...
Shipping DSL Grammar
dslBuilder.
when().domestically().
if().flexible().cheaply().
if().specified().pass().
when().interna...
Parsing the Grammar
Parsing the Grammar
Parsing the Grammar
Parsing the Grammar
Parsing the Grammar
Parsing the Grammar
Parsing the Grammar
The DSL Builder
JSON of the DSL Builder Rules
Shipping DSL Grammar with Drones!)
when shipping with drones
if weather is favorable send as cheaply as possible
otherwise...
How to Learn More
Thanks!!!
Writing Custom Domain Specific Languages (DSLs)
Writing Custom Domain Specific Languages (DSLs)
Writing Custom Domain Specific Languages (DSLs)
Writing Custom Domain Specific Languages (DSLs)
Writing Custom Domain Specific Languages (DSLs)
Próxima SlideShare
Cargando en…5
×

Writing Custom Domain Specific Languages (DSLs)

4.432 visualizaciones

Publicado el

My talk for JSConf 2014:

When writing complex business logic, it is critically important to maintain clean code though the judicious applications of Test Driven Development and Domain Driven Design. However, even these powerful techniques fall short of solving the problem at the heart of building complex software: building what the customer actually wants.

Domain Specific Languages (DSLs) allow us to capture complex business requirements in code written in the language of the customer. Once an ubiquitous language between you and your customer is defined and implemented as a DSL, the code can quite literally be given back to the customer to edit and refine. This is not a theory, or a myth. I have done this under real-world constraints and deadlines, and you can as well.

JavaScript’s ability to blend Object Oriented and Functional Programming paradigms makes it an ideal language for authoring custom DSLs. Unfortunately, too often developers are unclear on how to identify when a custom DSL is an appropriate solution, and when it is, how to go about writing one. I will take you through the process of developing a few different custom DSLs from planning to implementation, as well as how to performance tune and debug your new custom language. My hope is that you will gain a powerful tool for managing complex software that will keep you sane, and your customers happy.

Publicado en: Software, Tecnología, Educación

Writing Custom Domain Specific Languages (DSLs)

  1. 1. Writing Custom DSLs Neil Green
  2. 2. Today’s Talk • The Evolutionary History of DSLs • What a DSL is and what it is for • Defining your DSL’s Grammar • Implementing your DSL
  3. 3. The Evolutionary History of DSLs
  4. 4. How Can We Create a Machine Language that Reads Like a Human Language? ?
  5. 5. Punch Cards How Humans First Spoke to Machines
  6. 6. Low Level Languages
  7. 7. Higher Level Languages
  8. 8. High Level Languages
  9. 9. Object Oriented Languages
  10. 10. Domain Modeling
  11. 11. User Interface Model Database Models as Data Transfer
  12. 12. User Interface Domain Model (Business Logic) Database Models as Business Logic
  13. 13. MVC Pattern ViewModel Controller
  14. 14. View Domain Model Controller MVC for Complex Applications
  15. 15. Creating A Domain Model 1. Understand your domain 2. Model your domain 3. Implement your domain
  16. 16. Understand Your Domain Speak with the customer Develop a mutual language Implement the Model Diagram a Model Nouns Classes Verbs Methods
  17. 17. Model the Domain
  18. 18. Implement the Domain Model Speculative Implementation (guessing based on assumptions)
  19. 19. Implement the Domain Model Write Test Write Code to Pass Test Refactor Code
  20. 20. Implementing the Domain Model Behavior Driven Development Write Tests Write Tests Alone Write Test with Customer Test Driven Development
  21. 21. Language of Test = Language of Domain
  22. 22. Have the Customer Express Expectations Directly in Code
  23. 23. What a DSL is and what it is for
  24. 24. Domain Specific Language 1. Grammar 2. Implementation 3. Rules
  25. 25. Common DSLs General DSLs • SQL • Regular Expression • Unix shell scripts JavaScript DSLs • jQuery • D3js • Gulp • Mocha/Chai
  26. 26. DSLs Make Complex Problems Simpler select * from Table
  27. 27. DSLs Also Simplify Business Problems • Automotive • Banking • Consumer • Education • Engineering • Energy • Oil and Gas • Financial (Finance) • Food and beverage • Government • Healthcare • Insurance • Manufacturing • Media • Online • Real estate • Retail • Technology • Telecommunications • Transportation (Travel)
  28. 28. Business DSLs get their Grammar from the customer NOT THE PROGRAMMER Business DSLs are for customers Technical DSLs are for programmers
  29. 29. Defining your DSL’s Grammar
  30. 30. Defining the Grammar Programmer Customer Domain Model
  31. 31. The Alarm Problem Domain
  32. 32. “Hey Paul, how's it going? Yesterday our office admin came in early to setup for the companies weekly status meeting, but he couldn't remember the code to turn off the alarm. The cops had to come out and he tried to explain he was an employee because he had a key, but they didn’t believe him and ended up calling me. We have the same meeting every Monday morning, so can we just not have the alarm set so he doesn't need the alarm code to get in?”
  33. 33. “Hey Paul, what's up? I know I asked you to not have the alarm set for our weekly status meetings, but during memorial day I was worried that we had the alarm off when we should have had it on. I had a friend of mine who's business got robbed on July 4th, 'cause I guess they figured no one would be there.”
  34. 34. Grammar Hey Paul, how's it going? Yesterday our office admin came in early to setup for the companies weekly status meeting, but he couldn't remember the code to turn off the alarm. The cops had to come out and he tried to explain he was an employee because he had a key, but they didn’t believe him and ended up calling me. We have the same meeting every Monday morning, so can we just not have the alarm set so he doesn't need the alarm code to get in? Hey Paul, what's up? I know I asked you to not have the alarm set for our weekly status meetings, but during memorial day I was worried that we had the alarm off when we should have had it on. I had a friend of mine who's business got robbed on July 4th, 'cause I guess they figured no one would be there.
  35. 35. Grammar Hey Paul, how's it going? Yesterday our office admin came in early to setup for the companies weekly status meeting, but he couldn't remember the code to turn off the alarm. The cops had to come out and he tried to explain he was an employee because he had a key, but they didn’t believe him and ended up calling me. We have the same meeting every Monday morning, so can we just not have the alarm set so he doesn't need the alarm code to get in? Hey Paul, what's up? I know I asked you to not have the alarm set for our weekly status meetings, but during memorial day I was worried that we had the alarm off when we should have had it on. I had a friend of mine who's business got robbed on July 4th, 'cause I guess they figured no one would be there.
  36. 36. Grammar • Our • Office admin • Code • Alarm • Yesterday • Early • Weekly • Every Monday morning • Memorial day • July 4th • Not • Should • Set • Off • On VerbsNouns
  37. 37. Grammar • Our  Company • Office admin  Employee & Role • Code • Alarm • Not • Should • Set  On • On • Off VerbsNouns (things) • Yesterday  Day • Early  6:00am – 9:00am • Weekly  Every & Week • Every Monday morning  Every & Day & 9:00am–12:00pm • Memorial day  (Idiom) • July 4th  (Idiom) Nouns (time)
  38. 38. Idiom • New Year’s Day • Birthday of Martin Luther King, Jr. • Washington’s Birthday • Memorial Day • Independence Day (“July 4th”) • Labor Day • Columbus Day • Veterans Day • Thanksgiving Day • Christmas Day Federal Holiday 
  39. 39. Grammar • Company • Employee • Role • Code • Alarm • Not • Should • On • Off VerbsNouns (things) • Day • 6:00am, 9:00am, 12:00pm • Every • Week • Federal Holiday Nouns (time)
  40. 40. Grammar Alarm on weekdays* 8:00am-6:00pm Alarm off Mondays 6:00am-8:00am Alarm on federal holidays * Weekdays = Every Monday - Friday
  41. 41. Grammar The Alarm should be on every weekday between 8:00am-6:00pm, but the alarm should be off Mondays from 6:00am-8:00am. Also, the alarm should be on for federal holidays.
  42. 42. Verify Grammar “Yeah, this is pretty good, only thing is, we don’t close for Columbus day”
  43. 43. Grammar The Alarm should be on every weekday between 8:00am-6:00pm, but the alarm should be off Mondays from 6:00am-8:00am. Also, the alarm should be on for federal holidays, but not for Columbus Day
  44. 44. Grammar • Company • Employee • Role • Code • Alarm • Not • Should • On • Off VerbsNouns (things) • Day • Every • Week • Weekday; “Mondays” • Federal Holiday; “Columbus Day” • 6:00am, 8:00am, 12:00pm, 6:00pm Nouns (time)
  45. 45. Grammar • Alarm • Not • On • Off VerbsNouns (things) • Weekdays; “Mondays” • Federal Holiday; “Columbus Day” • 6:00am, 8:00am, 12:00pm, 6:00pm Nouns (time)
  46. 46. (security guard by day, programmer by night) Implementing the Grammar
  47. 47. Implementing the Grammar • Compiler? • Lexer? • Parser? • Tokenizer? • Interpretor? • Symbol table?
  48. 48. Implementing the Grammar Method Chaining! (Fluent Interface)
  49. 49. Implementing the Grammar Alarm on weekdays 8:00am-6:00pm Alarm off Mondays 6:00am-8:00am Alarm on federal holidays Alarm off Columbus Day
  50. 50. Implementing the Grammar Alarm.on().weekdays(‘8:00am’, ’6:00pm’); Alarm.off().Mondays(‘6:00am’, ‘8:00am’); Alarm.on().federal().holidays(); Alarm.off(‘Columbus Day’);
  51. 51. Implementing the Grammar alarm = new AlarmDsl(); alarm.on().weekdays(‘8:00am’, ’6:00pm’); alarm.off().Mondays(‘6:00am’, ‘8:00am’); alarm.on().federal().holidays(); alarm.off(‘Columbus Day’); alarm.buildConfiguredDomainModel()
  52. 52. Configuring the Domain Using objects: new AlarmSchedule({ on: true, startTime: ‘8:00am’, endTime: ‘6:00am’ });
  53. 53. Configuring the Domain Using functions: AlarmSchedule.isOn = isTimeBetween( timeRange(‘8:00am’, ‘6:00am’) );
  54. 54. Paul’s Written a Custom DSL!!!
  55. 55. How to Write a Custom DSL 1. Review Conversation with Customer 2. Remove meaningless words 3. Separate nouns and verbs 4. Identify Idioms 5. Review grammar with Customer 6. Implement the Grammar
  56. 56. Implementing your DSL
  57. 57. New Domain: Shipping
  58. 58. Shipping Problem Domain • Customer • Destination • Order • Item • Box • Delivery Date • Shipping Cost • Dimensions • Weight • Fragility • Address • International • Carrier • Rate
  59. 59. Shipping Domain Model
  60. 60. Shipping DSL Grammar when shipping domestically if delivery date is flexible ship as cheaply as possible if delivery date is specified pass shipping cost on to customer when shipping internationally then ship as cheaply as possible
  61. 61. Shipping DSL Grammar when domestically if flexible cheaply if specified pass when internationally then cheaply
  62. 62. Shipping DSL Grammar when().domestically(). if().flexible().cheaply(). if().specified().pass(). when().internationally(). then().cheaply
  63. 63. Shipping DSL Grammar dslBuilder. when().domestically(). if().flexible().cheaply(). if().specified().pass(). when().internationally(). then().cheaply() .build();
  64. 64. Parsing the Grammar
  65. 65. Parsing the Grammar
  66. 66. Parsing the Grammar
  67. 67. Parsing the Grammar
  68. 68. Parsing the Grammar
  69. 69. Parsing the Grammar
  70. 70. Parsing the Grammar
  71. 71. The DSL Builder
  72. 72. JSON of the DSL Builder Rules
  73. 73. Shipping DSL Grammar with Drones!) when shipping with drones if weather is favorable send as cheaply as possible otherwise ship domestically when shipping domestically if delivery date is flexible ship as cheaply as possible if delivery date is specified pass shipping cost on to customer when shipping internationally then ship as cheaply as possible
  74. 74. How to Learn More
  75. 75. Thanks!!!

×