Code Generation: The Safety Scissors of Metaprogramming

156
1

description

Metaprogramming is cool. Metaprogramming is hip. Metaprogramming has performance costs and is a serious pain in the butt to debug.You can get a lot of the benefits of metaprogramming from using code generation instead. In fact, most metaprogramming is really just dynamic code generation. Or maybe it’s the other way around. You could say that metaprogramming is really just dynamic code generation, or that code generation is just compiled metaprogramming. Code generation is code which creates text strings of code and saves them to the filesystem; metaprogramming is code which creates text strings of code and passes them directly to eval() or one of its variants.Either way you slice it, code generation is a powerful strategy. Using Ryan Davis’ awe-inspiring library Ruby2Ruby, it’s technically possible to unit test metaprogramming, but unit testing code generation is easier to do. Its results are also a lot easier to debug. Jack Herrington’s brilliant book “Code Generation In Action” demonstrates how you can generate the literally hundreds of Java files a non-trivial EJB application needs with a much smaller number of Ruby programs, slashing development time by months. Several aspects of Rails use code generation extensively, including scaffolding and the rails command itself. Rails’ core strategy of “convention over configuration” would be impossible without code generation.In this talk, I’ll demonstrate both how to unit-test metaprogramming with Ruby2Ruby, nd how to automate EJB application development with Herrington’s code generation techniques. Bring a nice Tupperware bowl to catch your brain in after it leaks out your ears.

Transcript of Code Generation: The Safety Scissors of Metaprogramming

Page 1: Code Generation: The Safety Scissors of Metaprogramming

1

Page 2: Code Generation: The Safety Scissors of Metaprogramming

Ἀρχιμήδης2

Page 3: Code Generation: The Safety Scissors of Metaprogramming

π3

Page 4: Code Generation: The Safety Scissors of Metaprogramming

δος µοι πα στω, καιταν γαν κινάσω

4

Page 5: Code Generation: The Safety Scissors of Metaprogramming

λ5

Page 6: Code Generation: The Safety Scissors of Metaprogramming

λthe ultimate

6

Page 7: Code Generation: The Safety Scissors of Metaprogramming

μετά7

Page 8: Code Generation: The Safety Scissors of Metaprogramming

meta8

Page 9: Code Generation: The Safety Scissors of Metaprogramming

metaprogramming?

9

Page 10: Code Generation: The Safety Scissors of Metaprogramming

metaOO

10

Page 11: Code Generation: The Safety Scissors of Metaprogramming

Code GenerationThe Safety Scissors

Of Metaprogramming

11

Page 12: Code Generation: The Safety Scissors of Metaprogramming

Ruby2Ruby12

Page 13: Code Generation: The Safety Scissors of Metaprogramming

(lambda do puts “hello world”end).to_ruby

13

Page 14: Code Generation: The Safety Scissors of Metaprogramming

class Widget < ActiveRecord::Baseend

14

Page 15: Code Generation: The Safety Scissors of Metaprogramming

Ruby2Ruby.translate(Widget)

15

Page 16: Code Generation: The Safety Scissors of Metaprogramming

class Widget < ActiveRecord::Base has_one :fooend

16

Page 17: Code Generation: The Safety Scissors of Metaprogramming

Ruby2Ruby.translate(Widget)

17

Page 18: Code Generation: The Safety Scissors of Metaprogramming

18

Page 19: Code Generation: The Safety Scissors of Metaprogramming

19

Page 20: Code Generation: The Safety Scissors of Metaprogramming

20

Page 21: Code Generation: The Safety Scissors of Metaprogramming

21

Page 22: Code Generation: The Safety Scissors of Metaprogramming

overkil l

22

Page 23: Code Generation: The Safety Scissors of Metaprogramming

monkeypatching

23

Page 24: Code Generation: The Safety Scissors of Metaprogramming

tangledobject graph

24

Page 25: Code Generation: The Safety Scissors of Metaprogramming

stop monkey-patching!

25

Page 26: Code Generation: The Safety Scissors of Metaprogramming

nothing can ever protect you

from bad programming

26

Page 27: Code Generation: The Safety Scissors of Metaprogramming

nothing can ever protect you

from bad programming

27

Page 28: Code Generation: The Safety Scissors of Metaprogramming

28

Page 29: Code Generation: The Safety Scissors of Metaprogramming

29

Page 30: Code Generation: The Safety Scissors of Metaprogramming

tests

specs30

Page 31: Code Generation: The Safety Scissors of Metaprogramming

tests

specsy u need

31

Page 32: Code Generation: The Safety Scissors of Metaprogramming

32

Page 33: Code Generation: The Safety Scissors of Metaprogramming

tests rawk

33

Page 34: Code Generation: The Safety Scissors of Metaprogramming

debuggers suck

34

Page 35: Code Generation: The Safety Scissors of Metaprogramming

debuggers suck

35

Page 36: Code Generation: The Safety Scissors of Metaprogramming

monkey-patchmonkey-patching

itself

36

Page 37: Code Generation: The Safety Scissors of Metaprogramming

meta-monkey-patching

37

Page 38: Code Generation: The Safety Scissors of Metaprogramming

rubinius38

Page 39: Code Generation: The Safety Scissors of Metaprogramming

nodebox

39

Page 40: Code Generation: The Safety Scissors of Metaprogramming

code == data

40

Page 41: Code Generation: The Safety Scissors of Metaprogramming

at this point, I just demoed code from my blog.below is a link to the post:

http://gilesbowkett.blogspot.com/2008/03/simple-metaprogramming-logger-with.html(code samples are included)

41

Page 42: Code Generation: The Safety Scissors of Metaprogramming

42

Page 43: Code Generation: The Safety Scissors of Metaprogramming

simple object graph

43

Page 44: Code Generation: The Safety Scissors of Metaprogramming

nodebox

44

Page 45: Code Generation: The Safety Scissors of Metaprogramming

tangledobject graph

45

Page 46: Code Generation: The Safety Scissors of Metaprogramming

node box

46

Page 47: Code Generation: The Safety Scissors of Metaprogramming

same tool

47

Page 48: Code Generation: The Safety Scissors of Metaprogramming

different output

48

Page 49: Code Generation: The Safety Scissors of Metaprogramming

proof of concept49

Page 50: Code Generation: The Safety Scissors of Metaprogramming

50

Page 51: Code Generation: The Safety Scissors of Metaprogramming

foo

51

Page 52: Code Generation: The Safety Scissors of Metaprogramming

foo

bar

52

Page 53: Code Generation: The Safety Scissors of Metaprogramming

foo

barbaz

53

Page 54: Code Generation: The Safety Scissors of Metaprogramming

foo

barbaz

foofoo

foo

foo

foo

foo

foo

bar

bar

bar

bar

bar

bar

barbaz

baz

baz

baz

baz

54

Page 55: Code Generation: The Safety Scissors of Metaprogramming

debuggers suck

55

Page 56: Code Generation: The Safety Scissors of Metaprogramming

some people want debuggers

56

Page 57: Code Generation: The Safety Scissors of Metaprogramming

57

Page 58: Code Generation: The Safety Scissors of Metaprogramming

“Ruby has no tool support!”

58

Page 59: Code Generation: The Safety Scissors of Metaprogramming

1 day

59

Page 60: Code Generation: The Safety Scissors of Metaprogramming

shock the monkey

60

Page 61: Code Generation: The Safety Scissors of Metaprogramming

Powerful Tools

61

Page 62: Code Generation: The Safety Scissors of Metaprogramming

Powerful Tools

• Rubinius

• Nodebox

• code == data

62

Page 63: Code Generation: The Safety Scissors of Metaprogramming

Powerful Tools

• Rubinius

• Nodebox

• code == data

63

Page 64: Code Generation: The Safety Scissors of Metaprogramming

Powerful Tools

• Rubinius

• Nodebox

• code == data

64

Page 65: Code Generation: The Safety Scissors of Metaprogramming

• Rubinius

• Nodebox

• code == data

65

Page 66: Code Generation: The Safety Scissors of Metaprogramming

code == data

66

Page 67: Code Generation: The Safety Scissors of Metaprogramming

1998

67

Page 68: Code Generation: The Safety Scissors of Metaprogramming

generated HTML

68

Page 69: Code Generation: The Safety Scissors of Metaprogramming

automated cvs and rcs

69

Page 70: Code Generation: The Safety Scissors of Metaprogramming

70

Page 71: Code Generation: The Safety Scissors of Metaprogramming

(2003)EJB

71

Page 72: Code Generation: The Safety Scissors of Metaprogramming

legacy app: 150 tables

72

Page 73: Code Generation: The Safety Scissors of Metaprogramming

ejb: 7 files per entity

73

Page 74: Code Generation: The Safety Scissors of Metaprogramming

7 * 150 = 1050 Java files

74

Page 75: Code Generation: The Safety Scissors of Metaprogramming

by hand: 3+ man-years

75

Page 76: Code Generation: The Safety Scissors of Metaprogramming

generators: 2 man-months

76

Page 77: Code Generation: The Safety Scissors of Metaprogramming

77

Page 78: Code Generation: The Safety Scissors of Metaprogramming

four generator types

• code munger generator• inline code expansion generator• mixed-code generator• partial-class generator• tier generator

78

Page 79: Code Generation: The Safety Scissors of Metaprogramming

generating UIs

79

Page 80: Code Generation: The Safety Scissors of Metaprogramming

generating unit tests

80

Page 81: Code Generation: The Safety Scissors of Metaprogramming

embedding SQLwith generators

81

Page 82: Code Generation: The Safety Scissors of Metaprogramming

creating databaseaccess generators

82

Page 83: Code Generation: The Safety Scissors of Metaprogramming

generating webservices layers

83

Page 84: Code Generation: The Safety Scissors of Metaprogramming

generating models froma business logic DSL

84

Page 85: Code Generation: The Safety Scissors of Metaprogramming

sound familiar?

85

Page 86: Code Generation: The Safety Scissors of Metaprogramming

86

Page 87: Code Generation: The Safety Scissors of Metaprogramming

script/generate scaffold

87

Page 88: Code Generation: The Safety Scissors of Metaprogramming

script/generate resource

88

Page 89: Code Generation: The Safety Scissors of Metaprogramming

script/generate plugin

89

Page 90: Code Generation: The Safety Scissors of Metaprogramming

script/generate migration

90

Page 91: Code Generation: The Safety Scissors of Metaprogramming

script/generate

91

Page 92: Code Generation: The Safety Scissors of Metaprogramming

rails Command

92

Page 93: Code Generation: The Safety Scissors of Metaprogramming

acts_as_authenticated

93

Page 94: Code Generation: The Safety Scissors of Metaprogramming

restful_authentication

94

Page 95: Code Generation: The Safety Scissors of Metaprogramming

ActiveRecord SQL

95

Page 96: Code Generation: The Safety Scissors of Metaprogramming

ActionPack Helpers•ActiveRecordHelper•AssetTagHelper•AtomFeedHelper•BenchmarkHelper•CacheHelper•CaptureHelper•DateHelper•DebugHelper•FormHelper•FormOptionsHelper

•FormTagHelper•JavascriptHelper•NumberHelper•PrototypeHelper•RecordTagHelper•SanitizeHelper•ScriptaculousHelper•TagHelper•TextHelper•UrlHelper

Text

•RecordIdentificationHelper

96

Page 97: Code Generation: The Safety Scissors of Metaprogramming

code generation FTW

97

Page 98: Code Generation: The Safety Scissors of Metaprogramming

98

Page 99: Code Generation: The Safety Scissors of Metaprogramming

code generation FTW

99

Page 100: Code Generation: The Safety Scissors of Metaprogramming

code == data

100

Page 101: Code Generation: The Safety Scissors of Metaprogramming

(eq (code (data)))

101

Page 102: Code Generation: The Safety Scissors of Metaprogramming

102

Page 103: Code Generation: The Safety Scissors of Metaprogramming

functions which return functions which return functions which return functions which return functions which return functions which return functions which return functions which return functions which return functions which return functions which return functions which return functions

103

Page 104: Code Generation: The Safety Scissors of Metaprogramming

code which generates data which runs as code which generates data which runs as code which generates data which runs as code which generates data which runs as code which generates data which runs as code which generates data which runs as code

104

Page 105: Code Generation: The Safety Scissors of Metaprogramming

105

Page 106: Code Generation: The Safety Scissors of Metaprogramming

lisp macros pwn“metaprogramming”

106

Page 107: Code Generation: The Safety Scissors of Metaprogramming

and you can dothem in Perl!

107

Page 108: Code Generation: The Safety Scissors of Metaprogramming

108

Page 109: Code Generation: The Safety Scissors of Metaprogramming

shocked? perl!

109

Page 110: Code Generation: The Safety Scissors of Metaprogramming

110

Page 111: Code Generation: The Safety Scissors of Metaprogramming

shocked? perl!

111

Page 112: Code Generation: The Safety Scissors of Metaprogramming

λthe ultimate

112

Page 113: Code Generation: The Safety Scissors of Metaprogramming

combinator113

Page 114: Code Generation: The Safety Scissors of Metaprogramming

$114

Page 115: Code Generation: The Safety Scissors of Metaprogramming

$small teams

115

Page 116: Code Generation: The Safety Scissors of Metaprogramming

$small teams lisp macros

116

Page 117: Code Generation: The Safety Scissors of Metaprogramming

great programmers canwrite better programmersthan they can hire

117

Page 118: Code Generation: The Safety Scissors of Metaprogramming

great programmers canwrite better programmersthan they can hire

metaprogramming118

Page 119: Code Generation: The Safety Scissors of Metaprogramming

great?119

Page 120: Code Generation: The Safety Scissors of Metaprogramming

120

Page 121: Code Generation: The Safety Scissors of Metaprogramming

intergalactic voluntarily baldclone wizards of time and space

who the womens are be lusting fors

121

Page 122: Code Generation: The Safety Scissors of Metaprogramming

122

Page 123: Code Generation: The Safety Scissors of Metaprogramming

123

Page 124: Code Generation: The Safety Scissors of Metaprogramming

124

Page 125: Code Generation: The Safety Scissors of Metaprogramming

incredible power

125

Page 126: Code Generation: The Safety Scissors of Metaprogramming

simpleclarity

126

Page 127: Code Generation: The Safety Scissors of Metaprogramming

simplicity

magic127

Page 128: Code Generation: The Safety Scissors of Metaprogramming

skilled programmers canwrite better programmersthan they can hire

metaprogramming128

Page 129: Code Generation: The Safety Scissors of Metaprogramming

129

Page 130: Code Generation: The Safety Scissors of Metaprogramming

code which generates data which runs as code which generates data which runs as code which generates data which runs as code which generates data which runs as code which generates data which runs as code which generates data which runs as code

130

Page 131: Code Generation: The Safety Scissors of Metaprogramming

Unix.is_a? Lisp

131

Page 132: Code Generation: The Safety Scissors of Metaprogramming

# => true

132

Page 133: Code Generation: The Safety Scissors of Metaprogramming

133

Page 134: Code Generation: The Safety Scissors of Metaprogramming

δος µοι πα στω, καιταν γαν κινάσω

134

Page 135: Code Generation: The Safety Scissors of Metaprogramming

Ruby.is_a? πα στω

135

Page 136: Code Generation: The Safety Scissors of Metaprogramming

# => true

136

Page 137: Code Generation: The Safety Scissors of Metaprogramming

137

Page 138: Code Generation: The Safety Scissors of Metaprogramming

138

Page 139: Code Generation: The Safety Scissors of Metaprogramming

139

Page 140: Code Generation: The Safety Scissors of Metaprogramming

140

Page 141: Code Generation: The Safety Scissors of Metaprogramming

141

Page 142: Code Generation: The Safety Scissors of Metaprogramming

boba fezz

142

Page 143: Code Generation: The Safety Scissors of Metaprogramming

143

Page 144: Code Generation: The Safety Scissors of Metaprogramming

Caveats!

• read the book, the technique is easy

• leveraging it is the clever part

• you can use Ruby as well as Rubinius

• if Unix is a Lisp, the filesystem is a compiler

144

Page 145: Code Generation: The Safety Scissors of Metaprogramming

Caveats!

• read the book, the technique is easy

• leveraging it is the clever part

• you can use Ruby as well as Rubinius

• if Unix is a Lisp, the filesystem is a compiler

145

Page 146: Code Generation: The Safety Scissors of Metaprogramming

Caveats!

• read the book, the technique is easy

• leveraging it is the clever part

• you can use Ruby as well as Rubinius

• if Unix is a Lisp, the filesystem is a compiler

146

Page 147: Code Generation: The Safety Scissors of Metaprogramming

Caveats!

• read the book, the technique is easy

• leveraging it is the clever part

• you can use Ruby as well as Rubinius

• if Unix is a Lisp, the filesystem is a compiler

147

Page 148: Code Generation: The Safety Scissors of Metaprogramming

Caveats!

• read the book, the technique is easy

• leveraging it is the clever part

• you can use Ruby as well as Rubinius

• if Unix is a Lisp, the filesystem is a compiler

148

Page 149: Code Generation: The Safety Scissors of Metaprogramming

149

Page 150: Code Generation: The Safety Scissors of Metaprogramming

Essential Technique

• read the book, the technique is easy

• leveraging it is the clever part

• you can use Ruby as well as Rubinius

• if Unix is a Lisp, the filesystem is a compiler

150

Page 151: Code Generation: The Safety Scissors of Metaprogramming

Essential Technique

• puts

• leveraging it is the clever part

• you can use Ruby as well as Rubinius

• if Unix is a Lisp, the filesystem is a compiler

151

Page 152: Code Generation: The Safety Scissors of Metaprogramming

Essential Technique

• puts

• eval

• you can use Ruby as well as Rubinius

• if Unix is a Lisp, the filesystem is a compiler

152

Page 153: Code Generation: The Safety Scissors of Metaprogramming

Essential Technique

• puts

• eval

• ERB

• if Unix is a Lisp, the filesystem is a compiler

153

Page 154: Code Generation: The Safety Scissors of Metaprogramming

Essential Technique

• puts

• eval

• ERB

• if Unix is a Lisp, the filesystem is a compiler•(unix pipes)

154

Page 155: Code Generation: The Safety Scissors of Metaprogramming

Essential Technique

• open mouth

• make noise

• profit

• if Unix is a Lisp, the filesystem is a compiler

155

Page 156: Code Generation: The Safety Scissors of Metaprogramming

156