Thought Works building dsl’s 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 memeagora.blogspot.com
Thought Works
Thought Works what i cover motivation types of dsls building dsls in: java groovy ruby external dsls best practices
Thought Works burning questions why is there so much xml mixed in with my java code? why do things like aspects exist? why won’t everyone shut up already about ruby on rails? is there an evolutionary step beyond object- oriented programming?
Thought Works
Thought Works
Thought Works 2.days.from.today Superclass Superclass Subclass Subclass Subclass Subclass Subclass Subclass objects, aspects, generics become the building blocks for dsls
Thought Works declarative vs imperative code
Thought Works why dsls? “Iced Decaf Triple Grande Vanilla Skim with whip latte.” “Scattered, smothered, covered” “Route 66, swinging, easy on the chorus, extra solo at the coda, and bump at the end” “OMFG D00d Bob is t3h UBER 1337 R0XX0RZ LOL” waffle house hash brown language: scattered, smothered, covered, chunked, topped, diced, peppered, & capped
Thought Works Every non-trivial human behavior has a domain specific language.
Thought Works including your job all businesses have their own dsl why developers tend to stay within domains how non-developers talk about work
Thought Works nomenclature coined by martin fowler domain specific language a limited form of computer language designed for a specific class of problems language oriented programming general style of development which operates about the idea of building software around a set of domain specific languages
Thought Works api: explicit context
Thought Works dsl: implicit context Venti half-caf, non-fat, extra hot, no foam, no whip latte once context is established, repeating it over and over is just noise
Thought Works types: internal aka “embedded” sit atop a base language must follow syntax rules why dynamic languages tend to make better bases
Thought Works types: external build your own language must be able to lex and parse your language let your imagination run wild!
Thought Works internal dsl’s
Thought Works fluent interface treat lines of code as sentences example: jmock expectation
Thought Works car api
Thought Works car fluent interface as simple as writing set methods that return this
Thought Works method chaining Make modifier methods return the host object so that multiple modifiers can be invoked in a single expression.
Thought Works example: logging writing a fluent interface around log4j properties file creation
Thought Works the target
Thought Works dsl syntax
Thought Works
Thought Works expression builder a layer that provides a fluent interface over a regular api
Thought Works wrapping api’s wrap existing api’s in fluent interfaces to improve readability example: wrapping ibatis
Thought Works ibatis xml configuration
Thought Works sqlmap
Thought Works
Thought Works java appointment calendar using fluent interface & method chaining
Thought Works
Thought Works
Thought Works
Thought Works the finishing problem when does the call “finish”? how can we make sure things happen at the right time?
Thought Works method invocation chained method calls nested method calls
Thought Works
Thought Works
Thought Works mitigating the finishing problem build fluent interfaces with a mixture of chained and nested method invocations
Thought Works use method chaining for stateless object construction use nested methods to control completion
Thought Works good citizenship? building objects with chained methods can create “bad citizens” imposes order validation semantics on dependent method calls in the nested add() method allows invalid state
Thought Works
Thought Works dynamic building blocks closures open classes dynamic typing looser syntax rules than java
Thought Works closures as containers call a new method on String
Thought Works open classes via categories
Thought Works
Thought Works expando metaclass
Thought Works time spans returns a java.util.Calendar for the proper date
Thought Works integer with time support 4.pm
Thought Works calendar support
Thought Works time span demo
Thought Works who returns what? 2 Integer days Integer fromToday Calendar at Calendar 4.pm Integer
Thought Works appointment calendar
Thought Works t e s t i n g
Thought Works
Thought Works ruby
Thought Works open classes
Thought Works calendar in ruby added an expression builder support for days of the week
Thought Works using named parameters array as value
Thought Works literal collection expression Form language expressions using literal collection syntax
Thought Works switching contexts
Thought Works declarative advantages declarative code reveals semantic intent imperative code reveals implementation dsl’s allow you to specify intent without coupling in implementation
Thought Works
Thought Works
Thought Works
Thought Works
Thought Works
Thought Works
Thought Works external dsls
Thought Works roll your own language create your own parser & lexer lex/yacc www.antlr.org
Thought Works antlrworks
Thought Works antlrworks
Thought Works language workbenches a tool that supports language oriented programming intentional software (charles simonyi) software factories (microsoft) mps (jetbrains)
Thought Works compilation since cs-101 Parse Compile class Foo { private int id; . . . . . . } Editable Representation Executable Storage Representation Representation
Thought Works
Thought Works “post- ide’s” first java ide to edit the abstract syntax directly enables refactoring
Thought Works workbenches Versioning Storage class Foo { private int id; . . . . . . } Workbench Projections Document Window Tab Tab Tab Menu Label Executable Editable Representation Text Field Label Representation Storage Representation • Label Label Text Field Text Field Text Field Text Field Text Field Text Field
Thought Works jetbrains mps
Thought Works best practices
Thought Works envision the perfect result what is the ideal dsl syntax? build towards it the rake napkin
Thought Works test, test, test
Thought Works narrow the problem domain keep your dsl as cohesive as possible create solutions by composing dsls... ...not creating more complicated languages jetbrain’s use of mps
Thought Works resources http://martinfowler.com/bliki/DomainSpecificLanguage.html http://martinfowler.com/articles/languageWorkbench.html http://www.theserverside.com/news/thread.tss?thread_id=46674 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
Thought Works questions? please fill out the session evaluations slides & samples available at nealford.com NEAL FORD thoughtworker / meme wrangler Thought Works 14 Wall St, Suite 2019, New York, NY 10005 This work is licensed under the Creative Commons Attribution-Noncommercial-Share Alike 2.5 License. nford@thoughtworks.com www.nealford.com www.thoughtworks.com http://creativecommons.org/licenses/by-nc-sa/2.5/ memeagora.blogspot.com
Recommend
More recommend