building dsls in static & dynamic languages NEAL FORD - - PowerPoint PPT Presentation

building dsl s in static dynamic languages
SMART_READER_LITE
LIVE PREVIEW

building dsls in static & dynamic languages NEAL FORD - - PowerPoint PPT Presentation

Thought Works building dsls in static & dynamic languages NEAL FORD thoughtworker / meme wrangler Thought Works 14 Wall St, Suite 2019, New York, NY 10005 nford@thoughtworks.com www.nealford.com www.thoughtworks.com


slide-1
SLIDE 1

NEAL FORD thoughtworker / meme wrangler

ThoughtWorks

14 Wall St, Suite 2019, New York, NY 10005 nford@thoughtworks.com www.nealford.com www.thoughtworks.com memeagora.blogspot.com

ThoughtWorks

building dsl’s in static & dynamic languages

slide-2
SLIDE 2

ThoughtWorks

slide-3
SLIDE 3

ThoughtWorks

groovy java external dsls best practices ruby building dsls in: types of dsls motivation

what i cover

slide-4
SLIDE 4

ThoughtWorks

why won’t everyone shut up already about ruby

  • n rails?

why is there so much xml mixed in with my java code? why do things like aspects exist? is there an evolutionary step beyond object-

  • riented programming?

burning questions

slide-5
SLIDE 5

ThoughtWorks

slide-6
SLIDE 6

ThoughtWorks

slide-7
SLIDE 7

ThoughtWorks

Superclass Subclass Subclass Subclass Superclass Subclass Subclass Subclass

2.days.from.today

  • bjects, aspects, generics become the

building blocks for dsls

slide-8
SLIDE 8

ThoughtWorks

declarative vs imperative code

slide-9
SLIDE 9

ThoughtWorks

“Route 66, swinging, easy on the chorus, extra solo at the coda, and bump at the end” “Iced Decaf Triple Grande Vanilla Skim with whip latte.” “Scattered, smothered, covered” “OMFG D00d Bob is t3h UBER 1337 R0XX0RZ LOL”

why dsls?

waffle house hash brown language: scattered, smothered, covered, chunked, topped, diced, peppered, & capped

slide-10
SLIDE 10

ThoughtWorks

Every non-trivial human behavior has a domain specific language.

slide-11
SLIDE 11

ThoughtWorks

how non-developers talk about work why developers tend to stay within domains all businesses have their own dsl

including your job

slide-12
SLIDE 12

ThoughtWorks

a limited form of computer language designed for a specific class of problems coined by martin fowler domain specific language general style of development which operates about the idea of building software around a set of domain specific languages language oriented programming

nomenclature

slide-13
SLIDE 13

ThoughtWorks

api: explicit context

slide-14
SLIDE 14

ThoughtWorks

dsl: implicit context

Venti half-caf, non-fat, extra hot, no foam, no whip latte

  • nce context is established, repeating it over and
  • ver is just noise
slide-15
SLIDE 15

ThoughtWorks

must follow syntax rules aka “embedded” sit atop a base language why dynamic languages tend to make better bases

types: internal

slide-16
SLIDE 16

ThoughtWorks

let your imagination run wild! build your own language must be able to lex and parse your language

types: external

slide-17
SLIDE 17

ThoughtWorks

internal dsl’s

slide-18
SLIDE 18

ThoughtWorks

treat lines of code as sentences example: jmock expectation

fluent interface

slide-19
SLIDE 19

ThoughtWorks

car api

slide-20
SLIDE 20

ThoughtWorks

car fluent interface

as simple as writing set methods that return this

slide-21
SLIDE 21

ThoughtWorks

method chaining

Make modifier methods return the host object so that multiple modifiers can be invoked in a single expression.

slide-22
SLIDE 22

ThoughtWorks

writing a fluent interface around log4j properties file creation

example: logging

slide-23
SLIDE 23

ThoughtWorks

the target

slide-24
SLIDE 24

ThoughtWorks

dsl syntax

slide-25
SLIDE 25

ThoughtWorks

slide-26
SLIDE 26

ThoughtWorks

expression builder

a layer that provides a fluent interface

  • ver a regular api
slide-27
SLIDE 27

ThoughtWorks

wrap existing api’s in fluent interfaces to improve readability example: wrapping ibatis

wrapping api’s

slide-28
SLIDE 28

ThoughtWorks

ibatis xml configuration

slide-29
SLIDE 29

ThoughtWorks

sqlmap

slide-30
SLIDE 30

ThoughtWorks

slide-31
SLIDE 31

ThoughtWorks

java appointment calendar

using fluent interface & method chaining

slide-32
SLIDE 32

ThoughtWorks

slide-33
SLIDE 33

ThoughtWorks

slide-34
SLIDE 34

ThoughtWorks

slide-35
SLIDE 35

ThoughtWorks

when does the call “finish”? how can we make sure things happen at the right time?

the finishing problem

slide-36
SLIDE 36

ThoughtWorks

chained method calls nested method calls

method invocation

slide-37
SLIDE 37

ThoughtWorks

slide-38
SLIDE 38

ThoughtWorks

slide-39
SLIDE 39

ThoughtWorks

build fluent interfaces with a mixture of chained and nested method invocations

mitigating the finishing problem

slide-40
SLIDE 40

ThoughtWorks

use method chaining for stateless object construction use nested methods to control completion

slide-41
SLIDE 41

ThoughtWorks

in the nested add() method

  • n dependent method calls

validation building objects with chained methods can create “bad citizens”

good citizenship?

imposes order semantics

allows invalid state

slide-42
SLIDE 42

ThoughtWorks

slide-43
SLIDE 43

ThoughtWorks

dynamic typing closures

  • pen classes

looser syntax rules than java

dynamic building blocks

slide-44
SLIDE 44

ThoughtWorks

closures as containers

call a new method on String

slide-45
SLIDE 45

ThoughtWorks

  • pen classes via

categories

slide-46
SLIDE 46

ThoughtWorks

slide-47
SLIDE 47

ThoughtWorks

expando metaclass

slide-48
SLIDE 48

ThoughtWorks

returns a java.util.Calendar for the proper date

time spans

slide-49
SLIDE 49

ThoughtWorks

integer with time support

4.pm

slide-50
SLIDE 50

ThoughtWorks

calendar support

slide-51
SLIDE 51

ThoughtWorks

time span demo

slide-52
SLIDE 52

ThoughtWorks

who returns what?

2 Integer days Integer fromToday Calendar at Calendar 4.pm Integer

slide-53
SLIDE 53

ThoughtWorks

appointment calendar

slide-54
SLIDE 54

ThoughtWorks

t e s t i n g

slide-55
SLIDE 55

ThoughtWorks

slide-56
SLIDE 56

ThoughtWorks

ruby

slide-57
SLIDE 57

ThoughtWorks

  • pen classes
slide-58
SLIDE 58

ThoughtWorks

calendar in ruby

added an expression builder support for days of the week

slide-59
SLIDE 59

ThoughtWorks

using named parameters

array as value

slide-60
SLIDE 60

ThoughtWorks

literal collection expression

Form language expressions using literal collection syntax

slide-61
SLIDE 61

ThoughtWorks

switching contexts

slide-62
SLIDE 62

ThoughtWorks

dsl’s allow you to specify intent without coupling in implementation declarative code reveals semantic intent imperative code reveals implementation

declarative advantages

slide-63
SLIDE 63

ThoughtWorks

slide-64
SLIDE 64

ThoughtWorks

slide-65
SLIDE 65

ThoughtWorks

slide-66
SLIDE 66

ThoughtWorks

slide-67
SLIDE 67

ThoughtWorks

slide-68
SLIDE 68

ThoughtWorks

slide-69
SLIDE 69

ThoughtWorks

external dsls

slide-70
SLIDE 70

ThoughtWorks

create your own parser & lexer lex/yacc

roll your own language

www.antlr.org

slide-71
SLIDE 71

ThoughtWorks

antlrworks

slide-72
SLIDE 72

ThoughtWorks

antlrworks

slide-73
SLIDE 73

ThoughtWorks

software factories (microsoft) a tool that supports language oriented programming intentional software (charles simonyi) mps (jetbrains)

language workbenches

slide-74
SLIDE 74

ThoughtWorks

class Foo { private int id; . . . . . . }

Editable Representation Storage Representation Executable Representation Parse Compile

compilation since cs-101

slide-75
SLIDE 75

ThoughtWorks

slide-76
SLIDE 76

ThoughtWorks

first java ide to edit the abstract syntax directly enables refactoring

“post- ide’s”

slide-77
SLIDE 77

ThoughtWorks

workbenches

class Foo { private int id; . . . . . . }

Executable Representation Workbench Versioning Storage Editable Representation Storage Representation

Tab Tab Tab

Document Window

Label

Menu Text Field Text Field Text Field Text Field Text Field Text Field

Label Label

  • Label

Text Field

Projections

slide-78
SLIDE 78

ThoughtWorks

jetbrains mps

slide-79
SLIDE 79

ThoughtWorks

best practices

slide-80
SLIDE 80

ThoughtWorks

the rake napkin what is the ideal dsl syntax? build towards it

envision the perfect result

slide-81
SLIDE 81

ThoughtWorks

test, test, test

slide-82
SLIDE 82

ThoughtWorks

...not creating more complicated languages keep your dsl as cohesive as possible create solutions by composing dsls... jetbrain’s use of mps

narrow the problem domain

slide-83
SLIDE 83

ThoughtWorks

resources

http://martinfowler.com/bliki/DomainSpecificLanguage.html

http://www.theserverside.com/news/thread.tss?thread_id=46674

http://martinfowler.com/articles/languageWorkbench.html

http://homepages.cwi.nl/~arie/papers/dslbib/

upcoming book on building internal dsl’s in ruby zak tamsen, jeremy stell-smith, dan manges, neal ford

slide-84
SLIDE 84

ThoughtWorks

NEAL FORD thoughtworker / meme wrangler

ThoughtWorks

14 Wall St, Suite 2019, New York, NY 10005 nford@thoughtworks.com www.nealford.com www.thoughtworks.com memeagora.blogspot.com

This work is licensed under the Creative Commons Attribution-Noncommercial-Share Alike 2.5 License. http://creativecommons.org/licenses/by-nc-sa/2.5/

questions?

please fill out the session evaluations slides & samples available at nealford.com