dsls in javascript

Download DSLs in JavaScript

Post on 06-May-2015

3.675 views

Category:

Technology

1 download

Embed Size (px)

TRANSCRIPT

  • 1.DSLs in JavaScriptNathaniel T. Schutta

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