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

More Related Content

PPT
.NET Overview
PPTX
Net serialization
PPTX
Presentation of Python, Django, DockerStack
PPTX
.NET and C# introduction
PPTX
Android with kotlin course
PPT
C++ to java
PDF
Implementing DSLs in practice
.NET Overview
Net serialization
Presentation of Python, Django, DockerStack
.NET and C# introduction
Android with kotlin course
C++ to java
Implementing DSLs in practice

What's hot (20)

PPTX
Introduction to python
PDF
Training python (new Updated)
PPSX
Kotlin Language powerpoint show file
PDF
Internal domain-specific languages
PDF
Xtext: code generation
PPTX
.NET and C# Introduction
PPTX
Namespaces in C#
PPT
Building scalable and language independent java services using apache thrift
PDF
.Net passé, présent et futur
ODP
TDoc - Bringing Documentation to Tool
PPTX
Introduction to Python Basics Programming
KEY
Language Engineering in the Cloud
PPT
Intro to Python with GPIO
PPTX
GDG Helwan Introduction to python
PDF
Functional programming with Xtend
PPT
Visual Studio .NET2010
PPTX
NDC Sydney 2019 - Microservices for building an IDE – The innards of JetBrain...
PPT
Why I Love Python
PPTX
C++vs java
ODP
Code Analysis and Refactoring with CDT
Introduction to python
Training python (new Updated)
Kotlin Language powerpoint show file
Internal domain-specific languages
Xtext: code generation
.NET and C# Introduction
Namespaces in C#
Building scalable and language independent java services using apache thrift
.Net passé, présent et futur
TDoc - Bringing Documentation to Tool
Introduction to Python Basics Programming
Language Engineering in the Cloud
Intro to Python with GPIO
GDG Helwan Introduction to python
Functional programming with Xtend
Visual Studio .NET2010
NDC Sydney 2019 - Microservices for building an IDE – The innards of JetBrain...
Why I Love Python
C++vs java
Code Analysis and Refactoring with CDT
Ad

Viewers also liked (10)

PPT
IntelliJ IDEA Architecture and Performance
PPTX
Android & Kotlin - The code awakens #03
PPTX
Android & Kotlin - The code awakens #01
PPTX
Exploring Anko Components, Kotlin, Android
PDF
Getting started-kotlin-android
PDF
No excuses, switch to kotlin
PDF
Little Helpers for Android Development with Kotlin
PPTX
Android Workshop
PDF
Kotlin in action
PDF
First few months with Kotlin - Introduction through android examples
IntelliJ IDEA Architecture and Performance
Android & Kotlin - The code awakens #03
Android & Kotlin - The code awakens #01
Exploring Anko Components, Kotlin, Android
Getting started-kotlin-android
No excuses, switch to kotlin
Little Helpers for Android Development with Kotlin
Android Workshop
Kotlin in action
First few months with Kotlin - Introduction through android examples
Ad

Similar to Smoothing Your Java with DSLs (20)

PPTX
Scam 08
PPT
Mps Presentation
PDF
DSLs in JavaScript
PDF
Language-Oriented Programming and Language Workbenches: Building Domain Langu...
ODP
Concepts of JetBrains MPS
PPT
Domain Specific Languages
PDF
Domain Specific Languages
PDF
groovy rules
PDF
Groovy On Trading Desk (2010)
PPT
GTAC Boosting your Testing Productivity with Groovy
PPT
Polyglot Programming in the JVM
PDF
GR8Conf 2011: STS DSL Support
PDF
Better DSL Support for Groovy-Eclipse
PPTX
AestasIT - Internal DSLs in Scala
PDF
Venkat Subramaniam Building DSLs In Groovy
PDF
Writing a DSL for the Dense with Scala - JVMCon
PDF
Groovy in the Enterprise - Case Studies - TSSJS Prague 2008 - Guillaume Laforge
PDF
DSL's with Groovy
PDF
Metamorphic Domain-Specific Languages
Scam 08
Mps Presentation
DSLs in JavaScript
Language-Oriented Programming and Language Workbenches: Building Domain Langu...
Concepts of JetBrains MPS
Domain Specific Languages
Domain Specific Languages
groovy rules
Groovy On Trading Desk (2010)
GTAC Boosting your Testing Productivity with Groovy
Polyglot Programming in the JVM
GR8Conf 2011: STS DSL Support
Better DSL Support for Groovy-Eclipse
AestasIT - Internal DSLs in Scala
Venkat Subramaniam Building DSLs In Groovy
Writing a DSL for the Dense with Scala - JVMCon
Groovy in the Enterprise - Case Studies - TSSJS Prague 2008 - Guillaume Laforge
DSL's with Groovy
Metamorphic Domain-Specific Languages

More from intelliyole (7)

PDF
Idiomatic Kotlin
PDF
Kotlin Bytecode Generation and Runtime Performance
PDF
Kotlin: Why Do You Care?
PDF
Feel of Kotlin (Berlin JUG 16 Apr 2015)
PDF
From Renamer Plugin to Polyglot IDE
PDF
How to Build Developer Tools on Top of IntelliJ Platform
PPT
The Kotlin Programming Language
Idiomatic Kotlin
Kotlin Bytecode Generation and Runtime Performance
Kotlin: Why Do You Care?
Feel of Kotlin (Berlin JUG 16 Apr 2015)
From Renamer Plugin to Polyglot IDE
How to Build Developer Tools on Top of IntelliJ Platform
The Kotlin Programming Language

Recently uploaded (20)

PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
PPTX
1. Introduction to Computer Programming.pptx
PDF
Accuracy of neural networks in brain wave diagnosis of schizophrenia
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPT
Teaching material agriculture food technology
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
A Presentation on Artificial Intelligence
PPTX
cloud_computing_Infrastucture_as_cloud_p
PDF
Getting Started with Data Integration: FME Form 101
PPTX
SOPHOS-XG Firewall Administrator PPT.pptx
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
A comparative analysis of optical character recognition models for extracting...
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Univ-Connecticut-ChatGPT-Presentaion.pdf
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Encapsulation_ Review paper, used for researhc scholars
Advanced methodologies resolving dimensionality complications for autism neur...
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
1. Introduction to Computer Programming.pptx
Accuracy of neural networks in brain wave diagnosis of schizophrenia
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Teaching material agriculture food technology
Assigned Numbers - 2025 - Bluetooth® Document
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
A Presentation on Artificial Intelligence
cloud_computing_Infrastucture_as_cloud_p
Getting Started with Data Integration: FME Form 101
SOPHOS-XG Firewall Administrator PPT.pptx
NewMind AI Weekly Chronicles - August'25-Week II
Network Security Unit 5.pdf for BCA BBA.
A comparative analysis of optical character recognition models for extracting...
Building Integrated photovoltaic BIPV_UPV.pdf
Univ-Connecticut-ChatGPT-Presentaion.pdf
Digital-Transformation-Roadmap-for-Companies.pptx
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Encapsulation_ Review paper, used for researhc scholars

Smoothing Your Java with DSLs

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