SlideShare a Scribd company logo
DSLs in JavaScript
                 Nathaniel T. Schutta
Who am I?
• Nathaniel T. Schutta
  http://www.ntschutta.com/jat/
• Foundations of Ajax & Pro Ajax and Java
  Frameworks
• UI guy
• Author, speaker, teacher
• More than a couple of web apps
The Plan
• DSLs?
• JavaScript? Seriously?
• Examples
• Lessons Learned
DS what now?
Domain Specific Language.
Every domain has its
   own language.
“Part of the benefit of being
"into" something is having an
insider lexicon.”
                                          Kathy Sierra
                             Creating Passionate Users




http://headrush.typepad.com/creating_passionate_users/2006/11/why_web_20_is_m.html
Three quarter, knock
  down, soft cut.
Scattered, smothered,
      covered.
Large skim mocha, no
   whip no froth.
Not general purpose.
Simpler, more limited.
Expressive.
Terse.
$$('.header').each(function(el) {el.observe("click", toggleSection)});
Not a new idea.
Unix.
Little languages.
Lisp.
Build the language up...
Lots of attention today.
Rails!
Ruby is very hospitable.
So are other languages ;)
Internal vs. External.
Internal.
Within an existing language.
More approachable.
Simpler.
No grammars, parsing, etc.
Constrained by host
    language.
Flexible syntax helps!
Ruby ;)
Fluent interface.
Embedded DSLs.
External.
Create your own language.
Grammars.
Need to parse.
ANTLR, yacc, JavaCC.
Harder.
More flexibility.
Language workbenches.
Tools for creating
 new languages.
Internal are more
 common today.
Language workbenches -
      shift afoot?
http://martinfowler.com/articles/mpsAgree.html
http://martinfowler.com/articles/languageWorkbench.html
Meta Programming System.


     http://www.jetbrains.com/mps/
Intentional Programming
     - Charles Simonyi.

               http://intentsoft.com/
http://www.technologyreview.com/Infotech/18047/?a=f
Oslo.


http://msdn.microsoft.com/en-us/oslo/default.aspx
Xtext.


http://wiki.eclipse.org/Xtext
Why are we seeing DSLs?
Easier to read.
Closer to the business.
Less friction, fewer
   translations.
Biz can review...
“Yesterday, I did a code
review. With a CEO...
Together, we found three
improvements, and a couple of
outright bugs.”
                                        Bruce Tate
                         Canaries in the Coal Mine



http://blog.rapidred.com/articles/2006/08/30/canaries-in-the-coal-mine
Don’t expect them to
  write it though!
Will we all write DSLs?
No.
Doesn’t mean we
 can’t use them.
General advice on
 building a DSL:
Write it as you’d
 like it to be...
Even on a napkin!
Use valid syntax.
Iterate, iterate, iterate.
Work on the
implementation.
http://martinfowler.com/dslwip/

  http://weblog.jamisbuck.org/2006/4/20/
     writing-domain-specific-languages

 http://memeagora.blogspot.com/2007/11/
  ruby-matters-frameworks-dsls-and.html

http://martinfowler.com/bliki/DslQandA.html
Not a toy!
JavaScript has been
around for a while.
Many dismissed it as
“toy for designers.”
It’s not the 90s anymore.
We have tools!
Developers care again!
Ajax.
Suffers from the “EJB issue.”
Powerful language.
“The Next Big Language”


    http://steve-yegge.blogspot.com/
    2007/02/next-big-language.html
Runs on lots of platforms
  - including the JVM.
Ruby like?
“Rhino on Rails”


http://steve-yegge.blogspot.com/
  2007/06/rhino-on-rails.html
Orto - JVM written
      in JavaScript.


http://ejohn.org/blog/running-java-in-javascript/
JS-909.


http://www.themaninblue.com/experiment/JS-909/
DSLs in JavaScript
JSSpec.
JavaScript testing DSL.
JSSpec? Really?
/**
 * Domain Specific Languages
 */
JSSpec.DSL = {};
BDD for JS.
Like RSpec.
Not quite as elegant.
describe('Plus operation', {
   'should concatenate two strings': function() {
     value_of("Hello " + "World").should_be("Hello World");
   },
   'should add two numbers': function() {
     value_of(2 + 2).should_be(4);
   }
})
value_of?
"Hello".should_be("Hello");
Sorry.
No method missing.
We’d need to modify
Object’s prototype.
Generally a no-no.
Though it’s been done.


     http://json.org/json.js
Null, undefined objects.
Design choice - consistency.
describe('Plus operation', {
   'should concatenate two strings': function() {
     value_of("Hello " + "World").should_be("Hello World");
   },
   'should add two numbers': function() {
     value_of(2 + 2).should_be(4);
   }
})
describe - global
defined in JSSpec.js.
Creates a new
JSSpec.Spec()...
And adds it to an
 array of specs.
value_of - global
defined in JSSpec.js.
value_of - converts parm
  to JSSpec.DSL.Subject
Handles arbitrary objects.
JSSpec.DSL.Subject
contains should_*.
Added to prototype.
JSSpec.DSL.Subject.prototype.should_be = function(expected) {
  var matcher =
  JSSpec.EqualityMatcher.createInstance(expected,this.target);
  if(!matcher.matches()) {
    JSSpec._assertionFailure = {message:matcher.explain()};
    throw JSSpec._assertionFailure;
  }
}
this.target?
JSSpec.DSL.Subject = function(target) {
    this.target = target;
};
this in JS is...interesting.
Why is everything
  JSSpec.Foo?
JS lacks packages
 or namespaces.
Keeps it clean.
Doesn’t collide...unless
 you have JSSpec too!
Not just a DSL of course.
Defines a number
  of matchers.
Also the runner
and the logger.
DSLs in JavaScript
Some CSS to make it pretty.
~1500 lines of code.
Clean code.
Why would you use it?
Easier to read.
function testStringConcat() {
  assertEquals("Hello World", "Hello " + "World");
}

function testNumericConcat() {
  assertEquals(4, 2 + 2);
}
var oTestCase = new YAHOO.tool.TestCase({

  name: "Plus operation",

      testStringConcat : function () {
         YAHOO.util.Assert.areEqual("Hello World", "Hello " + "World", "Should be 'Hello World'");
      },

      testNumericConcat : function () {
        YAHOO.util.Assert.areEqual(4, 2 + 2, "2 + 2 should be 4");
      }
});
describe('Plus operation', {
   'should concatenate two strings': function() {
      value_of("Hello " + "World").should_be("Hello World");
   },
   'should add two numbers': function() {
      value_of(2 + 2).should_be(4);
   }
})
Better? Worse?
What would you rather
 read 6 months later?
http://jania.pe.kr/aw/moin.cgi/JSSpec
ActiveRecord.js
JavaScript ORM.
Seriously?
Yep.
Let’s you use a DB
 from JavaScript.
Client or server ;)
Gears, AIR, W3C
HTML5 SQL spec.
In-memory option too.
Some free finder methods.
Base find method.
Migrations.
ActiveRecord.Migrations.migrations = {  
  1: {  
     up: function(schema){  
        schema.createTable('one',{  
          a: '',  
         b: {  
          type: 'TEXT',  
          value: 'default'  
        } });  
     },
   down: function(schema){  
        schema.dropTable('one');  
     }  
  }
};  
Validations.
User.validatesPresenceOf('password'); 
More to come...
Supports basic relationships.
Early stages...
On GitHub, contribute!
http://activerecordjs.org/
Objective-J
Objective-C...for JS.
JavaScript superset.
Cheating...kind of.
Native and
Objective-J classes.
Allows for instance methods.
Parameters are
separated by colons.
- (void)setJobTitle: (CPString)aJobTitle
    company: (CPString)aCompany
Bracket notation for
   method calls.
[myPerson setJobTitle: "Founder" company: "280 North"];
Does allow for
   method_missing.

http://cappuccino.org/discuss/2008/12/08/
  on-leaky-abstractions-and-objective-j/
http://cappuccino.org/
Coffee DSL.
Lessons learned.
Viable option.
Widely used language.
Not necessarily easy.
Syntax isn’t as flexible.
Lots of reserved words.
Freakn ;
Hello prototype!
Verbs as first class citizens.
Object literals.
DSL vs. named parameters
 vs. constructor parms.
new Ajax.Request('/DesigningForAjax/validate', {
   asynchronous: true,
   method: "get",
   parameters: {zip: $F('zip'), city: $F('city'), state: $F('state')},
   onComplete: function(request) {
     showResults(request.responseText);
   }
})
Fail fast vs. fail silent.
Method chaining is trivial.
Context can be a challenge.
Documentation key.
PDoc,YUI Doc.


  https://www.ohloh.net/p/pdoc_org
http://developer.yahoo.com/yui/yuidoc/
JavaScript isn’t a toy.
Not quite as flexible.
Plenty of metaprogramming
         goodness!
Questions?!?
Thanks!
Please complete your surveys.

More Related Content

PPTX
How Does Organic SEO Work?
PPTX
How to unlock the secrets of effortless keyword research with ChatGPT.pptx
PDF
Quick & Dirty Tips for : Better PowerPoint Presentations Faster
PPTX
[DSC DACH 23] ChatGPT and Beyond: How generative AI is Changing the way peopl...
PDF
Lernermotivation beim Digitalen Lernen. Herausforderungen und Ziele
PPSX
Asp.net mvc
PDF
How to Build an App with ChatGPT.pdf
PPTX
HTML Basic, CSS Basic, JavaScript basic.
How Does Organic SEO Work?
How to unlock the secrets of effortless keyword research with ChatGPT.pptx
Quick & Dirty Tips for : Better PowerPoint Presentations Faster
[DSC DACH 23] ChatGPT and Beyond: How generative AI is Changing the way peopl...
Lernermotivation beim Digitalen Lernen. Herausforderungen und Ziele
Asp.net mvc
How to Build an App with ChatGPT.pdf
HTML Basic, CSS Basic, JavaScript basic.

What's hot (20)

PDF
Generative Models and ChatGPT
PDF
AI and Machine Learning for Testers
PDF
Jetpack Compose beginner.pdf
PDF
Introduction to LLMs, Prompt Engineering fundamentals,
PDF
Deprecating the state machine: building conversational AI with the Rasa stack...
PDF
Using the power of Generative AI at scale
PDF
Generative AI for the rest of us
PDF
Leveraging Generative AI & Best practices
PDF
Data mining 3 - Data Models and Data Warehouse Design (cheat sheet - printable)
PPTX
OpenAI Chatgpt.pptx
PDF
Awesome Prompts Naveed.pdf
PPTX
Introduction to WordPress
PPTX
Meta tags
PDF
Suresh Poopandi_Generative AI On AWS-MidWestCommunityDay-Final.pdf
PDF
How to train your robot (with Deep Reinforcement Learning)
PDF
generative-ai-fundamentals and Large language models
PPTX
GitHub Copilot.pptx
PPTX
Bootstrap
PDF
Implications of GPT-3
PPTX
Colab workshop (for Computer vision Students)
Generative Models and ChatGPT
AI and Machine Learning for Testers
Jetpack Compose beginner.pdf
Introduction to LLMs, Prompt Engineering fundamentals,
Deprecating the state machine: building conversational AI with the Rasa stack...
Using the power of Generative AI at scale
Generative AI for the rest of us
Leveraging Generative AI & Best practices
Data mining 3 - Data Models and Data Warehouse Design (cheat sheet - printable)
OpenAI Chatgpt.pptx
Awesome Prompts Naveed.pdf
Introduction to WordPress
Meta tags
Suresh Poopandi_Generative AI On AWS-MidWestCommunityDay-Final.pdf
How to train your robot (with Deep Reinforcement Learning)
generative-ai-fundamentals and Large language models
GitHub Copilot.pptx
Bootstrap
Implications of GPT-3
Colab workshop (for Computer vision Students)
Ad

Viewers also liked (12)

PDF
Using Scala for building DSLs
PDF
Fantastic DSL in Python
PPT
Accessing loosely structured data from F# and C#
PDF
A Field Guide to DSL Design in Scala
PPT
Multiagent systems (and their use in industry)
PPTX
Creating Domain Specific Languages in F#
PPT
Internal DSLs For Automated Functional Testing
PDF
Adaptive Relaying,Report
PPTX
DSL in test automation
PDF
20 examples on Domain-Specific Modeling Languages
PPTX
Multi agenten-systeme
PDF
Creating Domain Specific Languages in Python
Using Scala for building DSLs
Fantastic DSL in Python
Accessing loosely structured data from F# and C#
A Field Guide to DSL Design in Scala
Multiagent systems (and their use in industry)
Creating Domain Specific Languages in F#
Internal DSLs For Automated Functional Testing
Adaptive Relaying,Report
DSL in test automation
20 examples on Domain-Specific Modeling Languages
Multi agenten-systeme
Creating Domain Specific Languages in Python
Ad

Similar to DSLs in JavaScript (20)

PDF
Domain Specific Languages
PDF
Building DSLs: Marriage of High Essence and Groovy Metaprogramming
PDF
Construction Techniques For Domain Specific Languages
PDF
GR8Conf 2011: STS DSL Support
PDF
Better DSL Support for Groovy-Eclipse
PDF
Real-world polyglot programming on the JVM - Ben Summers (ONEIS)
KEY
Exciting JavaScript - Part II
PDF
Building DSLs On CLR and DLR (Microsoft.NET)
PPTX
AestasIT - Internal DSLs in Scala
PDF
GGUG:Practical DSL Design
PDF
Metamorphic Domain-Specific Languages
PDF
Clojure Lightning Talk
PDF
groovy DSLs from beginner to expert
PDF
DSL Construction with Ruby - ThoughtWorks Masterclass Series 2009
PDF
Programming Languages: some news for the last N years
PDF
DSLs: what, why, how
PDF
JSLab.Руслан Шевченко."JavaScript как платформа компиляции"
PDF
Jslab rssh: JS as language platform
PDF
Language-Oriented Programming and Language Workbenches: Building Domain Langu...
PPT
Jet presentation
Domain Specific Languages
Building DSLs: Marriage of High Essence and Groovy Metaprogramming
Construction Techniques For Domain Specific Languages
GR8Conf 2011: STS DSL Support
Better DSL Support for Groovy-Eclipse
Real-world polyglot programming on the JVM - Ben Summers (ONEIS)
Exciting JavaScript - Part II
Building DSLs On CLR and DLR (Microsoft.NET)
AestasIT - Internal DSLs in Scala
GGUG:Practical DSL Design
Metamorphic Domain-Specific Languages
Clojure Lightning Talk
groovy DSLs from beginner to expert
DSL Construction with Ruby - ThoughtWorks Masterclass Series 2009
Programming Languages: some news for the last N years
DSLs: what, why, how
JSLab.Руслан Шевченко."JavaScript как платформа компиляции"
Jslab rssh: JS as language platform
Language-Oriented Programming and Language Workbenches: Building Domain Langu...
Jet presentation

More from elliando dias (20)

PDF
Clojurescript slides
PDF
Why you should be excited about ClojureScript
PDF
Functional Programming with Immutable Data Structures
PPT
Nomenclatura e peças de container
PDF
Geometria Projetiva
PDF
Polyglot and Poly-paradigm Programming for Better Agility
PDF
Javascript Libraries
PDF
How to Make an Eight Bit Computer and Save the World!
PDF
Ragel talk
PDF
A Practical Guide to Connecting Hardware to the Web
PDF
Introdução ao Arduino
PDF
Minicurso arduino
PDF
Incanter Data Sorcery
PDF
PDF
Fab.in.a.box - Fab Academy: Machine Design
PDF
The Digital Revolution: Machines that makes
PDF
Hadoop + Clojure
PDF
Hadoop - Simple. Scalable.
PDF
Hadoop and Hive Development at Facebook
PDF
Multi-core Parallelization in Clojure - a Case Study
Clojurescript slides
Why you should be excited about ClojureScript
Functional Programming with Immutable Data Structures
Nomenclatura e peças de container
Geometria Projetiva
Polyglot and Poly-paradigm Programming for Better Agility
Javascript Libraries
How to Make an Eight Bit Computer and Save the World!
Ragel talk
A Practical Guide to Connecting Hardware to the Web
Introdução ao Arduino
Minicurso arduino
Incanter Data Sorcery
Fab.in.a.box - Fab Academy: Machine Design
The Digital Revolution: Machines that makes
Hadoop + Clojure
Hadoop - Simple. Scalable.
Hadoop and Hive Development at Facebook
Multi-core Parallelization in Clojure - a Case Study

Recently uploaded (20)

PDF
Spectral efficient network and resource selection model in 5G networks
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
A comparative analysis of optical character recognition models for extracting...
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Approach and Philosophy of On baking technology
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
MYSQL Presentation for SQL database connectivity
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
cuic standard and advanced reporting.pdf
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PPTX
Cloud computing and distributed systems.
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Assigned Numbers - 2025 - Bluetooth® Document
Spectral efficient network and resource selection model in 5G networks
20250228 LYD VKU AI Blended-Learning.pptx
Building Integrated photovoltaic BIPV_UPV.pdf
A comparative analysis of optical character recognition models for extracting...
Review of recent advances in non-invasive hemoglobin estimation
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Approach and Philosophy of On baking technology
Agricultural_Statistics_at_a_Glance_2022_0.pdf
MYSQL Presentation for SQL database connectivity
“AI and Expert System Decision Support & Business Intelligence Systems”
Mobile App Security Testing_ A Comprehensive Guide.pdf
Network Security Unit 5.pdf for BCA BBA.
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
cuic standard and advanced reporting.pdf
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Cloud computing and distributed systems.
MIND Revenue Release Quarter 2 2025 Press Release
Assigned Numbers - 2025 - Bluetooth® Document

DSLs in JavaScript