DSLs in JavaScript

5,448 views

Published on

Published in: Technology

DSLs in JavaScript

  1. 1. DSLs in JavaScript Nathaniel T. Schutta
  2. 2. 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
  3. 3. The Plan • DSLs? • JavaScript? Seriously? • Examples • Lessons Learned
  4. 4. DS what now?
  5. 5. Domain Specific Language.
  6. 6. Every domain has its own language.
  7. 7. “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
  8. 8. Three quarter, knock down, soft cut.
  9. 9. Scattered, smothered, covered.
  10. 10. Large skim mocha, no whip no froth.
  11. 11. Not general purpose.
  12. 12. Simpler, more limited.
  13. 13. Expressive.
  14. 14. Terse.
  15. 15. $$('.header').each(function(el) {el.observe("click", toggleSection)});
  16. 16. Not a new idea.
  17. 17. Unix.
  18. 18. Little languages.
  19. 19. Lisp.
  20. 20. Build the language up...
  21. 21. Lots of attention today.
  22. 22. Rails!
  23. 23. Ruby is very hospitable.
  24. 24. So are other languages ;)
  25. 25. Internal vs. External.
  26. 26. Internal.
  27. 27. Within an existing language.
  28. 28. More approachable.
  29. 29. Simpler.
  30. 30. No grammars, parsing, etc.
  31. 31. Constrained by host language.
  32. 32. Flexible syntax helps!
  33. 33. Ruby ;)
  34. 34. Fluent interface.
  35. 35. Embedded DSLs.
  36. 36. External.
  37. 37. Create your own language.
  38. 38. Grammars.
  39. 39. Need to parse.
  40. 40. ANTLR, yacc, JavaCC.
  41. 41. Harder.
  42. 42. More flexibility.
  43. 43. Language workbenches.
  44. 44. Tools for creating new languages.
  45. 45. Internal are more common today.
  46. 46. Language workbenches - shift afoot?
  47. 47. http://martinfowler.com/articles/mpsAgree.html http://martinfowler.com/articles/languageWorkbench.html
  48. 48. Meta Programming System. http://www.jetbrains.com/mps/
  49. 49. Intentional Programming - Charles Simonyi. http://intentsoft.com/ http://www.technologyreview.com/Infotech/18047/?a=f
  50. 50. Oslo. http://msdn.microsoft.com/en-us/oslo/default.aspx
  51. 51. Xtext. http://wiki.eclipse.org/Xtext
  52. 52. Why are we seeing DSLs?
  53. 53. Easier to read.
  54. 54. Closer to the business.
  55. 55. Less friction, fewer translations.
  56. 56. Biz can review...
  57. 57. “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
  58. 58. Don’t expect them to write it though!
  59. 59. Will we all write DSLs?
  60. 60. No.
  61. 61. Doesn’t mean we can’t use them.
  62. 62. General advice on building a DSL:
  63. 63. Write it as you’d like it to be...
  64. 64. Even on a napkin!
  65. 65. Use valid syntax.
  66. 66. Iterate, iterate, iterate.
  67. 67. Work on the implementation.
  68. 68. 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
  69. 69. Not a toy!
  70. 70. JavaScript has been around for a while.
  71. 71. Many dismissed it as “toy for designers.”
  72. 72. It’s not the 90s anymore.
  73. 73. We have tools!
  74. 74. Developers care again!
  75. 75. Ajax.
  76. 76. Suffers from the “EJB issue.”
  77. 77. Powerful language.
  78. 78. “The Next Big Language” http://steve-yegge.blogspot.com/ 2007/02/next-big-language.html
  79. 79. Runs on lots of platforms - including the JVM.
  80. 80. Ruby like?
  81. 81. “Rhino on Rails” http://steve-yegge.blogspot.com/ 2007/06/rhino-on-rails.html
  82. 82. Orto - JVM written in JavaScript. http://ejohn.org/blog/running-java-in-javascript/
  83. 83. JS-909. http://www.themaninblue.com/experiment/JS-909/
  84. 84. JSSpec.
  85. 85. JavaScript testing DSL.
  86. 86. JSSpec? Really?
  87. 87. /** * Domain Specific Languages */ JSSpec.DSL = {};
  88. 88. BDD for JS.
  89. 89. Like RSpec.
  90. 90. Not quite as elegant.
  91. 91. 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); } })
  92. 92. value_of?
  93. 93. "Hello".should_be("Hello");
  94. 94. Sorry.
  95. 95. No method missing.
  96. 96. We’d need to modify Object’s prototype.
  97. 97. Generally a no-no.
  98. 98. Though it’s been done. http://json.org/json.js
  99. 99. Null, undefined objects.
  100. 100. Design choice - consistency.
  101. 101. 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); } })
  102. 102. describe - global defined in JSSpec.js.
  103. 103. Creates a new JSSpec.Spec()...
  104. 104. And adds it to an array of specs.
  105. 105. value_of - global defined in JSSpec.js.
  106. 106. value_of - converts parm to JSSpec.DSL.Subject
  107. 107. Handles arbitrary objects.
  108. 108. JSSpec.DSL.Subject contains should_*.
  109. 109. Added to prototype.
  110. 110. 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; } }
  111. 111. this.target?
  112. 112. JSSpec.DSL.Subject = function(target) { this.target = target; };
  113. 113. this in JS is...interesting.
  114. 114. Why is everything JSSpec.Foo?
  115. 115. JS lacks packages or namespaces.
  116. 116. Keeps it clean.
  117. 117. Doesn’t collide...unless you have JSSpec too!
  118. 118. Not just a DSL of course.
  119. 119. Defines a number of matchers.
  120. 120. Also the runner and the logger.
  121. 121. Some CSS to make it pretty.
  122. 122. ~1500 lines of code.
  123. 123. Clean code.
  124. 124. Why would you use it?
  125. 125. Easier to read.
  126. 126. function testStringConcat() { assertEquals("Hello World", "Hello " + "World"); } function testNumericConcat() { assertEquals(4, 2 + 2); }
  127. 127. 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"); } });
  128. 128. 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); } })
  129. 129. Better? Worse?
  130. 130. What would you rather read 6 months later?
  131. 131. http://jania.pe.kr/aw/moin.cgi/JSSpec
  132. 132. ActiveRecord.js
  133. 133. JavaScript ORM.
  134. 134. Seriously?
  135. 135. Yep.
  136. 136. Let’s you use a DB from JavaScript.
  137. 137. Client or server ;)
  138. 138. Gears, AIR, W3C HTML5 SQL spec.
  139. 139. In-memory option too.
  140. 140. Some free finder methods.
  141. 141. Base find method.
  142. 142. Migrations.
  143. 143. ActiveRecord.Migrations.migrations = {   1: {   up: function(schema){   schema.createTable('one',{   a: '',       b: {             type: 'TEXT',             value: 'default'           } });   },    down: function(schema){   schema.dropTable('one');   }   } };  
  144. 144. Validations.
  145. 145. User.validatesPresenceOf('password'); 
  146. 146. More to come...
  147. 147. Supports basic relationships.
  148. 148. Early stages...
  149. 149. On GitHub, contribute!
  150. 150. http://activerecordjs.org/
  151. 151. Objective-J
  152. 152. Objective-C...for JS.
  153. 153. JavaScript superset.
  154. 154. Cheating...kind of.
  155. 155. Native and Objective-J classes.
  156. 156. Allows for instance methods.
  157. 157. Parameters are separated by colons.
  158. 158. - (void)setJobTitle: (CPString)aJobTitle company: (CPString)aCompany
  159. 159. Bracket notation for method calls.
  160. 160. [myPerson setJobTitle: "Founder" company: "280 North"];
  161. 161. Does allow for method_missing. http://cappuccino.org/discuss/2008/12/08/ on-leaky-abstractions-and-objective-j/
  162. 162. http://cappuccino.org/
  163. 163. Coffee DSL.
  164. 164. Lessons learned.
  165. 165. Viable option.
  166. 166. Widely used language.
  167. 167. Not necessarily easy.
  168. 168. Syntax isn’t as flexible.
  169. 169. Lots of reserved words.
  170. 170. Freakn ;
  171. 171. Hello prototype!
  172. 172. Verbs as first class citizens.
  173. 173. Object literals.
  174. 174. DSL vs. named parameters vs. constructor parms.
  175. 175. 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); } })
  176. 176. Fail fast vs. fail silent.
  177. 177. Method chaining is trivial.
  178. 178. Context can be a challenge.
  179. 179. Documentation key.
  180. 180. PDoc,YUI Doc. https://www.ohloh.net/p/pdoc_org http://developer.yahoo.com/yui/yuidoc/
  181. 181. JavaScript isn’t a toy.
  182. 182. Not quite as flexible.
  183. 183. Plenty of metaprogramming goodness!
  184. 184. Questions?!?
  185. 185. Thanks! Please complete your surveys.

×