Writing Custom Domain Specific Languages (DSLs)

Post on 05-Dec-2014

448 Views

Category:

Software

0 Downloads

Preview:

Click to see full reader

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.

Transcript

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• Implementing your DSL

The Evolutionary History of DSLs

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

?

Punch CardsHow 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 domain2. Model your domain3. Implement your domain

Understand Your Domain

Speak with the customer

Develop a mutual language

Implement the Model

Diagram a Model

NounsClasses

VerbsMethods

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 Driven Development

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. Grammar2. Implementation3. Rules

Common DSLs

General DSLs• SQL• Regular Expression• Unix shell scripts

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

DSLs Make Complex Problems Simpler

select * from Table

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)

Business DSLs get their Grammar from the customer NOT THE PROGRAMMER

Business DSLs are for customers Technical DSLs are for programmers

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

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.

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.

Grammar

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

• Not• Should• Set• Off• On

VerbsNouns

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)

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

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)

Grammar

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

* Weekdays = Every Monday - Friday

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.

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. Also, the alarm should be on for federal holidays, but not for Columbus Day

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)

Grammar

• Alarm • Not• On• Off

VerbsNouns (things)

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

Nouns (time)

(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:00amAlarm on federal holidaysAlarm off Columbus Day

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

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

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 Customer2. Remove meaningless words3. Separate nouns and verbs 4. Identify Idioms 5. Review grammar with Customer6. Implement the Grammar

Implementing your DSL

New Domain: Shipping

Shipping Problem Domain

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

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

Shipping Domain Model

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

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

then().cheaply

Shipping DSL Grammar

dslBuilder.

when().domestically().

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

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

when().internationally().

then().cheaply()

.build();

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

How to Learn More

Thanks!!!

top related