building dsl s in static dynamic languages
play

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


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

  2. Thought Works

  3. Thought Works what i cover motivation types of dsls building dsls in: java groovy ruby external dsls best practices

  4. 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?

  5. Thought Works

  6. Thought Works

  7. Thought Works 2.days.from.today Superclass Superclass Subclass Subclass Subclass Subclass Subclass Subclass objects, aspects, generics become the building blocks for dsls

  8. Thought Works declarative vs imperative code

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

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

  11. Thought Works including your job all businesses have their own dsl why developers tend to stay within domains how non-developers talk about work

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

  13. Thought Works api: explicit context

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

  15. Thought Works types: internal aka “embedded” sit atop a base language must follow syntax rules why dynamic languages tend to make better bases

  16. Thought Works types: external build your own language must be able to lex and parse your language let your imagination run wild!

  17. Thought Works internal dsl’s

  18. Thought Works fluent interface treat lines of code as sentences example: jmock expectation

  19. Thought Works car api

  20. Thought Works car fluent interface as simple as writing set methods that return this

  21. Thought Works method chaining Make modifier methods return the host object so that multiple modifiers can be invoked in a single expression.

  22. Thought Works example: logging writing a fluent interface around log4j properties file creation

  23. Thought Works the target

  24. Thought Works dsl syntax

  25. Thought Works

  26. Thought Works expression builder a layer that provides a fluent interface over a regular api

  27. Thought Works wrapping api’s wrap existing api’s in fluent interfaces to improve readability example: wrapping ibatis

  28. Thought Works ibatis xml configuration

  29. Thought Works sqlmap

  30. Thought Works

  31. Thought Works java appointment calendar using fluent interface & method chaining

  32. Thought Works

  33. Thought Works

  34. Thought Works

  35. Thought Works the finishing problem when does the call “finish”? how can we make sure things happen at the right time?

  36. Thought Works method invocation chained method calls nested method calls

  37. Thought Works

  38. Thought Works

  39. Thought Works mitigating the finishing problem build fluent interfaces with a mixture of chained and nested method invocations

  40. Thought Works use method chaining for stateless object construction use nested methods to control completion

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

  42. Thought Works

  43. Thought Works dynamic building blocks closures open classes dynamic typing looser syntax rules than java

  44. Thought Works closures as containers call a new method on String

  45. Thought Works open classes via categories

  46. Thought Works

  47. Thought Works expando metaclass

  48. Thought Works time spans returns a java.util.Calendar for the proper date

  49. Thought Works integer with time support 4.pm

  50. Thought Works calendar support

  51. Thought Works time span demo

  52. Thought Works who returns what? 2 Integer days Integer fromToday Calendar at Calendar 4.pm Integer

  53. Thought Works appointment calendar

  54. Thought Works t e s t i n g

  55. Thought Works

  56. Thought Works ruby

  57. Thought Works open classes

  58. Thought Works calendar in ruby added an expression builder support for days of the week

  59. Thought Works using named parameters array as value

  60. Thought Works literal collection expression Form language expressions using literal collection syntax

  61. Thought Works switching contexts

  62. Thought Works declarative advantages declarative code reveals semantic intent imperative code reveals implementation dsl’s allow you to specify intent without coupling in implementation

  63. Thought Works

  64. Thought Works

  65. Thought Works

  66. Thought Works

  67. Thought Works

  68. Thought Works

  69. Thought Works external dsls

  70. Thought Works roll your own language create your own parser & lexer lex/yacc www.antlr.org

  71. Thought Works antlrworks

  72. Thought Works antlrworks

  73. Thought Works language workbenches a tool that supports language oriented programming intentional software (charles simonyi) software factories (microsoft) mps (jetbrains)

  74. Thought Works compilation since cs-101 Parse Compile class Foo { private int id; . . . . . . } Editable Representation Executable Storage Representation Representation

  75. Thought Works

  76. Thought Works “post- ide’s” first java ide to edit the abstract syntax directly enables refactoring

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

  78. Thought Works jetbrains mps

  79. Thought Works best practices

  80. Thought Works envision the perfect result what is the ideal dsl syntax? build towards it the rake napkin

  81. Thought Works test, test, test

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

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

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

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend