Smoothing Your Java with DSLs


Published on

Published in: Technology
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Smoothing Your Java with DSLs

  1. 1. Smoothing Your Java with DSLs Dmit ry Jemerov Development Lead JetBrains, Inc. ht tp://
  2. 2. Learn about the multiple ways you can use domain-specific languages in your Java project to make your code more readable and maintainable
  3. 3. Agenda <ul><li>DSL Overview </li></ul><ul><li>DSLs in Java </li></ul><ul><li>DSLs in Scripting Languages </li></ul><ul><li>JetBrains MPS </li></ul>
  4. 4. What is a DSL? <ul><li>Computer programming language </li></ul><ul><li>Limited expressiveness </li></ul><ul><li>Domain focus </li></ul>
  5. 5. DSL Classification <ul><li>External </li></ul>Regular Expressions SQL CSS
  6. 6. DSL Classification <ul><li>External </li></ul><ul><ul><li>XML-based </li></ul></ul>Ant XSLT
  7. 7. DSL Classification <ul><li>External </li></ul><ul><ul><li>XML-based </li></ul></ul><ul><li>Internal </li></ul>Query integration: Squill Testing: JMock, EasyMock
  8. 8. DSL Classification <ul><li>External </li></ul><ul><ul><li>XML-based </li></ul></ul><ul><li>Internal </li></ul><ul><ul><li>Scripting </li></ul></ul>Build: Gradle, Buildr Testing: easyb UI: Glimmer, Monkeybars
  9. 9. DSL Classification <ul><li>External </li></ul><ul><ul><li>XML-based </li></ul></ul><ul><li>Internal </li></ul><ul><ul><li>Scripting </li></ul></ul><ul><li>Language Workbench </li></ul>MPS “Oslo”
  10. 10. Why Use DSLs? <ul><li>Raising level of abstraction </li></ul><ul><ul><li>Separating the “what” from the “how” </li></ul></ul><ul><li>Separating stable and frequently changing parts of the system </li></ul><ul><li>Enabling domain expert contributions </li></ul>
  11. 11. DSLs: Code or Data? <ul><li>Depends on where you look from </li></ul><ul><ul><li>TrueType fonts: actually code for a rather powerful VM </li></ul></ul>
  12. 12. Integrating DSLs <ul><li>Interpretation </li></ul><ul><ul><li>Model is built by executing internal DSL or parsing external DSL </li></ul></ul><ul><ul><li>Interpreter executes operations described by DSL </li></ul></ul><ul><li>Code Generation </li></ul><ul><ul><li>DSL drives generation of host language code </li></ul></ul><ul><ul><li>DSL-generated code compiled with rest of app </li></ul></ul>
  13. 13. Agenda <ul><li>DSL Overview </li></ul><ul><li>DSLs in Java </li></ul><ul><li>DSLs in Scripting Languages </li></ul><ul><li>JetBrains MPS </li></ul>
  14. 14. Squill <ul><li>squill .from(pets) .where(eq(pets.species, “Cat”)) .orderBy(asc( .selectList(, pets.birthDate)); </li></ul><ul><li> </li></ul>
  15. 15. EasyMock <ul><li>expect(mock.voteForRemoval(“A&quot;)) .andReturn((byte) 42) .atLeastOnce(); </li></ul><ul><li> </li></ul>
  16. 16. IntelliJ IDEA PSI Patterns
  17. 17. Regular vs Fluent API <ul><li>Regular: a method call executes an operation </li></ul><ul><ul><li>object.verbNoun(param1, …, paramN) </li></ul></ul><ul><li>Fluent: a method call tells one thing about an operation that will be executed </li></ul><ul><ul><li>object.noun1(param1).noun2(param2)… </li></ul></ul>
  18. 18. Fluent API Patterns <ul><li>Method chaining </li></ul><ul><li>Builder objects </li></ul><ul><li>Static factory methods </li></ul><ul><li>Static imports </li></ul><ul><li>Initializer blocks in anonymous classes </li></ul>
  19. 19. Method Chaining <ul><li>class C { </li></ul><ul><li>C value1(Object v1) { </li></ul><ul><li>this.value1 = v1; </li></ul><ul><li>return this; </li></ul><ul><li>} </li></ul><ul><li>C value2(Object v2) { … } </li></ul><ul><li>} </li></ul><ul><li>c.value1(o1).value2(o2) </li></ul>
  20. 20. Builder Objects <ul><li>class CBuilder { </li></ul><ul><li>CBuilder value1(Object v1) { … } </li></ul><ul><li>CBuilder value2(Object v2) { … } </li></ul><ul><li>C build() { </li></ul><ul><li>return new C(value1, value2); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  21. 21. Factory Methods, Static Imports <ul><li>class CFactory { </li></ul><ul><li>static <T> C<T> newC() { </li></ul><ul><li>return new C<T>(); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>import static CFactory.*; </li></ul><ul><li>C<String> c = newC(); </li></ul>
  22. 22. Initializer Blocks (JMock) <ul><li>context.checking(new Expectations() {{ oneOf (subscriber).receive(message); }}); </li></ul>
  23. 23. Java Internal DSL Advantages <ul><li>Low entry barrier, no external dependencies </li></ul><ul><li>Full IDE support (code completion etc.) </li></ul><ul><li>Strong typing and compile-time error checking </li></ul>
  24. 24. Java Internal DSL Drawbacks <ul><li>Readability is not as good as with other solutions </li></ul>
  25. 25. Agenda <ul><li>DSL Overview </li></ul><ul><li>DSLs in Java </li></ul><ul><li>DSLs in Scripting Languages </li></ul><ul><li>JetBrains MPS </li></ul>
  26. 26. easyb <ul><li>given &quot;a stack with one pushed value&quot;, { stack = new Stack() pushVal = &quot;foo&quot; stack.push(pushVal) } when &quot;pop is called&quot;, { popVal = stack.pop() } then &quot;that object should be returned&quot;, { popVal.shouldBe pushVal } </li></ul><ul><li> </li></ul>
  27. 27. Glimmer <ul><li>shell { </li></ul><ul><li>text &quot;Contact Manager“ </li></ul><ul><li>composite { </li></ul><ul><li>layout, false) </li></ul><ul><li>label { text &quot;First &Name: &quot;} </li></ul><ul><li>text { </li></ul><ul><li>text bind(presenter, :first_name) </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li> </li></ul>
  28. 28. RubyMine Parameter Definitions <ul><li>define_params 'validates_presence_of' , migration_ref , [migration_ref , { :message => nil , :on => one_of(:save , :create , :update) , :if => method_ref( 'ActiveRecord::Base' ) }] </li></ul>
  29. 29. Scripting Language Advantages <ul><li>Closures </li></ul><ul><li>Properties </li></ul><ul><li>List/map literals </li></ul><ul><li>Operator overloading </li></ul><ul><li>Handling of missing methods </li></ul>
  30. 30. Integrating DSL Scripts <ul><li>Joint compilation </li></ul><ul><ul><li>Script statically compiled to bytecode </li></ul></ul><ul><ul><li>Allows direct calls of script code from Java </li></ul></ul><ul><li>Dynamic invocation </li></ul><ul><ul><li>Script invoked via javax.scripting or language-specific embedding API </li></ul></ul><ul><ul><li>Allows dynamic discovery of scripts </li></ul></ul><ul><ul><li>Allows modifying scripts without app restart or recompilation </li></ul></ul>
  31. 31. Agenda <ul><li>DSL Overview </li></ul><ul><li>DSLs in Java </li></ul><ul><li>DSLs in Scripting Languages </li></ul><ul><li>JetBrains MPS </li></ul>
  32. 32. MPS History <ul><li>Started as a research project back in 2004 </li></ul><ul><li>JetBrains issue tracker project being developed entirely in MPS </li></ul><ul><ul><li>Web framework </li></ul></ul><ul><ul><li>Data access framework </li></ul></ul><ul><ul><li>End-user functionality </li></ul></ul><ul><li>Released as open-source public beta in December 2008 </li></ul>
  33. 33. Key Concepts of MPS <ul><li>Language defines a set of concepts and relationships between them </li></ul><ul><li>Models are instances of a particular concept </li></ul><ul><li>Editors define how models of a particular concept are represented in the MPS IDE </li></ul><ul><li>Generators define how models are translated into a different language </li></ul>
  34. 34. What MPS doesn’t have <ul><li>No text representation for code in a particular language </li></ul><ul><li>No grammar for languages </li></ul><ul><li>No way to import code in existing text-based language </li></ul><ul><li>No dependency on specific output language generated </li></ul><ul><ul><li>Could be Java, JavaScript, CSS, XML… </li></ul></ul>
  35. 35. Advanced MPS <ul><li>Reusing languages </li></ul><ul><ul><li>Business rules need formulas </li></ul></ul><ul><ul><li>Can reuse MPS expression language: editor, generation etc. </li></ul></ul><ul><li>Extending languages </li></ul><ul><ul><li>Closures, ‘using’ blocks, complex numbers… </li></ul></ul>
  36. 36. Summary <ul><li>You can use the fluent programming style in Java to make your code easier to read and less verbose </li></ul><ul><li>DSLs built with scripting languages like Groovy and JRuby bring even further readability benefits </li></ul><ul><li>JetBrains MPS is a whole new approach to DSLs </li></ul>
  37. 37. For More Information <ul><li>Martin Fowler’s DSL book: </li></ul><ul><ul><li> </li></ul></ul><ul><li>JetBrains MPS: </li></ul><ul><ul><li> </li></ul></ul>
  38. 38. Q&A Dmitry Jemerov [email_address]
  39. 39. Smoothing Your Java with DSLs Dmit ry Jemerov Development Lead JetBrains, Inc. ht tp://