80
Writing Custom DSLs Neil Green

Writing Custom Domain Specific Languages (DSLs)

Embed Size (px)

DESCRIPTION

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.

Citation preview

Page 1: Writing Custom Domain Specific Languages (DSLs)

Writing Custom DSLs

Neil Green

Page 2: Writing Custom Domain Specific Languages (DSLs)

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

Page 3: Writing Custom Domain Specific Languages (DSLs)

The Evolutionary History of DSLs

Page 4: Writing Custom Domain Specific Languages (DSLs)

How Can We Create a Machine Language that Reads Like a Human Language?

?

Page 5: Writing Custom Domain Specific Languages (DSLs)

Punch CardsHow Humans First Spoke to Machines

Page 6: Writing Custom Domain Specific Languages (DSLs)

Low Level Languages

Page 7: Writing Custom Domain Specific Languages (DSLs)

Higher Level Languages

Page 8: Writing Custom Domain Specific Languages (DSLs)

High Level Languages

Page 9: Writing Custom Domain Specific Languages (DSLs)

Object Oriented Languages

Page 10: Writing Custom Domain Specific Languages (DSLs)

Domain Modeling

Page 11: Writing Custom Domain Specific Languages (DSLs)

User Interface

Model Database

Models as Data Transfer

Page 12: Writing Custom Domain Specific Languages (DSLs)

User Interface

Domain Model(Business Logic) Database

Models as Business Logic

Page 13: Writing Custom Domain Specific Languages (DSLs)

MVC Pattern

ViewModel

Controller

Page 14: Writing Custom Domain Specific Languages (DSLs)

View

Domain Model

Controller

MVC for Complex Applications

Page 15: Writing Custom Domain Specific Languages (DSLs)

Creating A Domain Model

1. Understand your domain2. Model your domain3. Implement your domain

Page 16: Writing Custom Domain Specific Languages (DSLs)

Understand Your Domain

Speak with the customer

Develop a mutual language

Implement the Model

Diagram a Model

NounsClasses

VerbsMethods

Page 17: Writing Custom Domain Specific Languages (DSLs)

Model the Domain

Page 18: Writing Custom Domain Specific Languages (DSLs)

Implement the Domain Model

Speculative Implementation(guessing based on assumptions)

Page 19: Writing Custom Domain Specific Languages (DSLs)

Implement the Domain Model

Write Test

Write Code to

Pass Test

Refactor Code

Page 20: Writing Custom Domain Specific Languages (DSLs)

Implementing the Domain Model

Behavior Driven Development

Write Tests

Write Tests Alone

Write Test with

Customer

Test Driven Development

Page 21: Writing Custom Domain Specific Languages (DSLs)

Language of Test = Language of Domain

Page 22: Writing Custom Domain Specific Languages (DSLs)

Have the Customer Express Expectations Directly in Code

Page 23: Writing Custom Domain Specific Languages (DSLs)

What a DSL is and what it is for

Page 24: Writing Custom Domain Specific Languages (DSLs)

Domain Specific Language

1. Grammar2. Implementation3. Rules

Page 25: Writing Custom Domain Specific Languages (DSLs)

Common DSLs

General DSLs• SQL• Regular Expression• Unix shell scripts

JavaScript DSLs• jQuery• D3js• Gulp• Mocha/Chai

Page 26: Writing Custom Domain Specific Languages (DSLs)

DSLs Make Complex Problems Simpler

select * from Table

Page 27: Writing Custom Domain Specific Languages (DSLs)

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)

Page 28: Writing Custom Domain Specific Languages (DSLs)

Business DSLs get their Grammar from the customer NOT THE PROGRAMMER

Business DSLs are for customers Technical DSLs are for programmers

Page 29: Writing Custom Domain Specific Languages (DSLs)

Defining your DSL’s Grammar

Page 30: Writing Custom Domain Specific Languages (DSLs)

Defining the Grammar

Programmer

Customer

Domain Model

Page 31: Writing Custom Domain Specific Languages (DSLs)

The Alarm Problem Domain

Page 32: Writing Custom Domain Specific Languages (DSLs)

“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?”

Page 33: Writing Custom Domain Specific Languages (DSLs)

“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.”

Page 34: Writing Custom Domain Specific Languages (DSLs)
Page 35: Writing Custom Domain Specific Languages (DSLs)

GrammarHey 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.

Page 36: Writing Custom Domain Specific Languages (DSLs)

GrammarHey 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.

Page 37: Writing Custom Domain Specific Languages (DSLs)

Grammar

• Our• Office admin • Code• Alarm• Yesterday • Early • Weekly • Every Monday morning• Memorial day • July 4th

• Not• Should• Set• Off• On

VerbsNouns

Page 38: Writing Custom Domain Specific Languages (DSLs)

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)

Page 39: Writing Custom Domain Specific Languages (DSLs)

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

Page 40: Writing Custom Domain Specific Languages (DSLs)

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)

Page 41: Writing Custom Domain Specific Languages (DSLs)

Grammar

Alarm on weekdays* 8:00am-6:00pm Alarm off Mondays 6:00am-8:00amAlarm on federal holidays

* Weekdays = Every Monday - Friday

Page 42: Writing Custom Domain Specific Languages (DSLs)

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.

Page 43: Writing Custom Domain Specific Languages (DSLs)

Verify Grammar

“Yeah, this is pretty good, only thing is, we don’t close for

Columbus day”

Page 44: Writing Custom Domain Specific Languages (DSLs)

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

Page 45: Writing Custom Domain Specific Languages (DSLs)

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)

Page 46: Writing Custom Domain Specific Languages (DSLs)

Grammar

• Alarm • Not• On• Off

VerbsNouns (things)

• Weekdays; “Mondays”• Federal Holiday; “Columbus Day”• 6:00am, 8:00am, 12:00pm, 6:00pm

Nouns (time)

Page 47: Writing Custom Domain Specific Languages (DSLs)

(security guard by day, programmer by night)

Implementing the Grammar

Page 48: Writing Custom Domain Specific Languages (DSLs)

Implementing the Grammar

• Compiler?• Lexer?• Parser?

• Tokenizer?• Interpretor?• Symbol table?

Page 49: Writing Custom Domain Specific Languages (DSLs)

Implementing the Grammar

Method Chaining!(Fluent Interface)

Page 50: Writing Custom Domain Specific Languages (DSLs)

Implementing the Grammar

Alarm on weekdays 8:00am-6:00pm Alarm off Mondays 6:00am-8:00amAlarm on federal holidaysAlarm off Columbus Day

Page 51: Writing Custom Domain Specific Languages (DSLs)

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’);

Page 52: Writing Custom Domain Specific Languages (DSLs)

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()

Page 53: Writing Custom Domain Specific Languages (DSLs)

Configuring the Domain

Using objects:

new AlarmSchedule({ on: true, startTime: ‘8:00am’, endTime: ‘6:00am’});

Page 54: Writing Custom Domain Specific Languages (DSLs)

Configuring the Domain

Using functions:

AlarmSchedule.isOn = isTimeBetween(

timeRange(‘8:00am’, ‘6:00am’));

Page 55: Writing Custom Domain Specific Languages (DSLs)

Paul’s Written a Custom DSL!!!

Page 56: Writing Custom Domain Specific Languages (DSLs)

How to Write a Custom DSL

1. Review Conversation with Customer2. Remove meaningless words3. Separate nouns and verbs 4. Identify Idioms 5. Review grammar with Customer6. Implement the Grammar

Page 57: Writing Custom Domain Specific Languages (DSLs)

Implementing your DSL

Page 58: Writing Custom Domain Specific Languages (DSLs)

New Domain: Shipping

Page 59: Writing Custom Domain Specific Languages (DSLs)

Shipping Problem Domain

• Customer• Destination• Order • Item• Box• Delivery Date• Shipping Cost

• Dimensions• Weight• Fragility• Address• International• Carrier • Rate

Page 60: Writing Custom Domain Specific Languages (DSLs)

Shipping Domain Model

Page 61: Writing Custom Domain Specific Languages (DSLs)

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

Page 62: Writing Custom Domain Specific Languages (DSLs)

Shipping DSL Grammar

when domestically

if flexible cheaply

if specified pass

when internationally

then cheaply

Page 63: Writing Custom Domain Specific Languages (DSLs)

Shipping DSL Grammar

when().domestically().

if().flexible().cheaply().

if().specified().pass().

when().internationally().

then().cheaply

Page 64: Writing Custom Domain Specific Languages (DSLs)

Shipping DSL Grammar

dslBuilder.

when().domestically().

if().flexible().cheaply().

if().specified().pass().

when().internationally().

then().cheaply()

.build();

Page 65: Writing Custom Domain Specific Languages (DSLs)

Parsing the Grammar

Page 66: Writing Custom Domain Specific Languages (DSLs)

Parsing the Grammar

Page 67: Writing Custom Domain Specific Languages (DSLs)

Parsing the Grammar

Page 68: Writing Custom Domain Specific Languages (DSLs)

Parsing the Grammar

Page 69: Writing Custom Domain Specific Languages (DSLs)

Parsing the Grammar

Page 70: Writing Custom Domain Specific Languages (DSLs)

Parsing the Grammar

Page 71: Writing Custom Domain Specific Languages (DSLs)

Parsing the Grammar

Page 72: Writing Custom Domain Specific Languages (DSLs)

The DSL Builder

Page 73: Writing Custom Domain Specific Languages (DSLs)
Page 74: Writing Custom Domain Specific Languages (DSLs)
Page 75: Writing Custom Domain Specific Languages (DSLs)
Page 76: Writing Custom Domain Specific Languages (DSLs)
Page 77: Writing Custom Domain Specific Languages (DSLs)

JSON of the DSL Builder Rules

Page 78: Writing Custom Domain Specific Languages (DSLs)

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

Page 79: Writing Custom Domain Specific Languages (DSLs)

How to Learn More

Page 80: Writing Custom Domain Specific Languages (DSLs)

Thanks!!!