dsls in javascript - · pdf file•foundations of ajax & pro ajax and java ... 51....

187
DSLs in JavaScript Nathaniel T. Schutta 1

Upload: ngotruc

Post on 09-Mar-2018

222 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

DSLs in JavaScriptNathaniel T. Schutta

1

Page 2: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Who am I?

• Nathaniel T. Schuttahttp://www.ntschutta.com/jat/

• Foundations of Ajax & Pro Ajax and Java Frameworks

• UI guy

• Author, speaker, teacher

• More than a couple of web apps

2

Page 3: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

The Plan• DSLs?

• JavaScript? Seriously?

• Examples

• Lessons Learned

3

Page 4: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

DS what now?

4

Page 5: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Domain Specific Language.

5

Page 6: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Every domain has its own language.

6

Page 7: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

“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

7

Page 8: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Three quarter, knock down, soft cut.

8

Page 9: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Scattered, smothered, covered.

9

Page 10: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Large skim mocha, no whip no froth.

10

Page 11: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Not general purpose.

11

Page 12: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Simpler, more limited.

12

Page 13: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Expressive.

13

Page 14: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Terse.

14

Page 15: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

$$('.header').each(function(el) {el.observe("click", toggleSection)});

15

Page 16: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Not a new idea.

16

Page 17: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Unix.

17

Page 18: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Little languages.

18

Page 19: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Lisp.

19

Page 20: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Build the language up...

20

Page 21: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Lots of attention today.

21

Page 22: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Rails!

22

Page 23: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Ruby is very hospitable.

23

Page 24: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

So are other languages ;)

24

Page 25: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Internal vs. External.

25

Page 26: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Internal.

26

Page 27: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Within an existing language.

27

Page 28: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

More approachable.

28

Page 29: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Simpler.

29

Page 30: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

No grammars, parsing, etc.

30

Page 31: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Constrained by host language.

31

Page 32: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Flexible syntax helps!

32

Page 33: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Ruby ;)

33

Page 34: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Fluent interface.

34

Page 35: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Embedded DSLs.

35

Page 36: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

External.

36

Page 37: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Create your own language.

37

Page 38: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Grammars.

38

Page 39: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Need to parse.

39

Page 40: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

ANTLR, yacc, JavaCC.

40

Page 41: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Harder.

41

Page 42: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

More flexibility.

42

Page 43: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Language workbenches.

43

Page 44: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Tools for creating new languages.

44

Page 45: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Internal are more common today.

45

Page 46: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Language workbenches - shift afoot?

46

Page 47: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

http://martinfowler.com/articles/languageWorkbench.html

http://martinfowler.com/articles/mpsAgree.html

47

Page 48: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Meta Programming System.

http://www.jetbrains.com/mps/

48

Page 49: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Intentional Programming - Charles Simonyi.

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

49

Page 50: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Oslo.

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

50

Page 51: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Xtext.

http://wiki.eclipse.org/Xtext

51

Page 52: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Why are we seeing DSLs?

52

Page 53: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Easier to read.

53

Page 54: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Closer to the business.

54

Page 55: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Less friction, fewer translations.

55

Page 56: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Biz can review...

56

Page 57: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

“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 Mine

http://blog.rapidred.com/articles/2006/08/30/canaries-in-the-coal-mine57

Page 58: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Don’t expect them to write it though!

58

Page 59: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Will we all write DSLs?

59

Page 60: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

No.

60

Page 61: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Doesn’t mean we can’t use them.

61

Page 62: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

General advice on building a DSL:

62

Page 63: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Write it as you’d like it to be...

63

Page 64: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Even on a napkin!

64

Page 65: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Use valid syntax.

65

Page 66: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Iterate, iterate, iterate.

66

Page 67: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Work on the implementation.

67

Page 68: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

http://martinfowler.com/bliki/DslQandA.html

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

http://martinfowler.com/dslwip/

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

68

Page 69: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Not a toy!

69

Page 70: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

JavaScript has been around for a while.

70

Page 71: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Many dismissed it as “toy for designers.”

71

Page 72: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

It’s not the 90s anymore.

72

Page 73: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

We have tools!

73

Page 74: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Developers care again!

74

Page 75: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Ajax.

75

Page 76: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Suffers from the “EJB issue.”

76

Page 77: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Powerful language.

77

Page 78: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

“The Next Big Language”

http://steve-yegge.blogspot.com/2007/02/next-big-language.html

78

Page 79: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Runs on lots of platforms - including the JVM.

79

Page 80: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Ruby like?

80

Page 81: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

“Rhino on Rails”

http://steve-yegge.blogspot.com/2007/06/rhino-on-rails.html

81

Page 82: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Orto - JVM written in JavaScript.

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

82

Page 83: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

JS-909.

http://www.themaninblue.com/experiment/JS-909/

83

Page 84: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

84

Page 85: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

JSSpec.

85

Page 86: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

JavaScript testing DSL.

86

Page 87: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

JSSpec? Really?

87

Page 88: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

/** * Domain Specific Languages */JSSpec.DSL = {};

88

Page 89: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

BDD for JS.

89

Page 90: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Like RSpec.

90

Page 91: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Not quite as elegant.

91

Page 92: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

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

Page 93: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

value_of?

93

Page 94: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

"Hello".should_be("Hello");

94

Page 95: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Sorry.

95

Page 96: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

No method missing.

96

Page 97: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

We’d need to modify Object’s prototype.

97

Page 98: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Generally a no-no.

98

Page 99: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Though it’s been done.

http://json.org/json.js

99

Page 100: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Null, undefined objects.

100

Page 101: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Design choice - consistency.

101

Page 102: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

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

Page 103: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

describe - global defined in JSSpec.js.

103

Page 104: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Creates a new JSSpec.Spec()...

104

Page 105: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

And adds it to an array of specs.

105

Page 106: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

value_of - global defined in JSSpec.js.

106

Page 107: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

value_of - converts parm to JSSpec.DSL.Subject

107

Page 108: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Handles arbitrary objects.

108

Page 109: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

JSSpec.DSL.Subject contains should_*.

109

Page 110: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Added to prototype.

110

Page 111: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

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

Page 112: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

this.target?

112

Page 113: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

JSSpec.DSL.Subject = function(target) { this.target = target;};

113

Page 114: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

this in JS is...interesting.

114

Page 115: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Why is everything JSSpec.Foo?

115

Page 116: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

JS lacks packages or namespaces.

116

Page 117: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Keeps it clean.

117

Page 118: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Doesn’t collide...unless you have JSSpec too!

118

Page 119: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Not just a DSL of course.

119

Page 120: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Defines a number of matchers.

120

Page 121: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Also the runner and the logger.

121

Page 122: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

122

Page 123: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Some CSS to make it pretty.

123

Page 124: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

~1500 lines of code.

124

Page 125: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Clean code.

125

Page 126: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Why would you use it?

126

Page 127: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Easier to read.

127

Page 128: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

function testStringConcat() { assertEquals("Hello World", "Hello " + "World");} function testNumericConcat() { assertEquals(4, 2 + 2);}

128

Page 129: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

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"); }});

129

Page 130: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

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); }})

130

Page 131: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Better? Worse?

131

Page 132: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

What would you rather read 6 months later?

132

Page 133: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

http://jania.pe.kr/aw/moin.cgi/JSSpec

133

Page 134: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

ActiveRecord.js

134

Page 135: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

JavaScript ORM.

135

Page 136: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Seriously?

136

Page 137: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Yep.

137

Page 138: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Let’s you use a DB from JavaScript.

138

Page 139: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Client or server ;)

139

Page 140: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Gears, AIR, W3C HTML5 SQL spec.

140

Page 141: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

In-memory option too.

141

Page 142: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Some free finder methods.

142

Page 143: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Base find method.

143

Page 144: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Migrations.

144

Page 145: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

ActiveRecord.Migrations.migrations = {   1: {   up: function(schema){   schema.createTable('one',{   a: '',       b: {            type: 'TEXT',            value: 'default'          } });   },   down: function(schema){   schema.dropTable('one');   }   }};  

145

Page 146: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Validations.

146

Page 147: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

User.validatesPresenceOf('password'); 

147

Page 148: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

More to come...

148

Page 149: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Supports basic relationships.

149

Page 150: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Early stages...

150

Page 151: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

On GitHub, contribute!

151

Page 152: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

http://activerecordjs.org/

152

Page 153: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Objective-J

153

Page 154: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Objective-C...for JS.

154

Page 155: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

JavaScript superset.

155

Page 156: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Cheating...kind of.

156

Page 157: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Native and Objective-J classes.

157

Page 158: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Allows for instance methods.

158

Page 159: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Parameters are separated by colons.

159

Page 160: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

- (void)setJobTitle: (CPString)aJobTitle company: (CPString)aCompany

160

Page 161: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Bracket notation for method calls.

161

Page 162: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

[myPerson setJobTitle: "Founder" company: "280 North"];

162

Page 163: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Does allow for method_missing.

http://cappuccino.org/discuss/2008/12/08/on-leaky-abstractions-and-objective-j/

163

Page 164: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

http://cappuccino.org/

164

Page 165: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Coffee DSL.

165

Page 166: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Lessons learned.

166

Page 167: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Viable option.

167

Page 168: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Widely used language.

168

Page 169: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Not necessarily easy.

169

Page 170: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Syntax isn’t as flexible.

170

Page 171: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Lots of reserved words.

171

Page 172: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Freakn ;

172

Page 173: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Hello prototype!

173

Page 174: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Verbs as first class citizens.

174

Page 175: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Object literals.

175

Page 176: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

DSL vs. named parameters vs. constructor parms.

176

Page 177: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

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); }})

177

Page 178: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Fail fast vs. fail silent.

178

Page 179: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Method chaining is trivial.

179

Page 180: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Context can be a challenge.

180

Page 181: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Documentation key.

181

Page 182: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

PDoc, YUI Doc.

https://www.ohloh.net/p/pdoc_org

http://developer.yahoo.com/yui/yuidoc/

182

Page 183: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

JavaScript isn’t a toy.

183

Page 184: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Not quite as flexible.

184

Page 185: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Plenty of metaprogramming goodness!

185

Page 186: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Questions?!?

186

Page 187: DSLs in JavaScript -   · PDF file•Foundations of Ajax & Pro Ajax and Java ...   51. Why are we seeing DSLs? 52. Easier to read. 53

Thanks!Please complete your surveys.

187