of 101
8/13/2019 Polyglot Poly Paradigm
1/101
Polyglot and
Poly-paradigmProgramming
Dean [email protected]
@deanwampler
1
Monday, June 14, 2010
http://creativecommons.org/licenses/by/3.0/http://creativecommons.org/licenses/by/3.0/http://creativecommons.org/licenses/by/3.0/http://creativecommons.org/licenses/by/3.0/http://creativecommons.org/licenses/by/3.0/http://creativecommons.org/licenses/by/3.0/http://creativecommons.org/licenses/by/3.0/http://creativecommons.org/licenses/by/3.0/http://twitter.com/deanwamplerhttp://twitter.com/deanwampler8/13/2019 Polyglot Poly Paradigm
2/101
2
Co-author,Programming
Scala
programmingscala.com
Monday, June 14, 2010
8/13/2019 Polyglot Poly Paradigm
3/101
8/13/2019 Polyglot Poly Paradigm
4/101
4
Monday, June 14, 2010
8/13/2019 Polyglot Poly Paradigm
5/101
8/13/2019 Polyglot Poly Paradigm
6/101
Todays
applications:
Are networked,
Have graphicaland serviceinterfaces,
6
Monday, June 14, 2010
8/13/2019 Polyglot Poly Paradigm
7/101
Todays
applications:
Persistdata,Must be resilient
andsecure,
Must scale,7
Monday, June 14, 2010
8/13/2019 Polyglot Poly Paradigm
8/101
Todays
applications:
and must doall that by Friday.
8
Monday, June 14, 2010
8/13/2019 Polyglot Poly Paradigm
9/101
Polyglot or
Multilingual:
many languages
9
Monday, June 14, 2010
8/13/2019 Polyglot Poly Paradigm
10/101
Poly-paradigmor
Multiparadigm:many modularity
paradigms
10
Monday, June 14, 2010
8/13/2019 Polyglot Poly Paradigm
11/101
Thesis:
modern problemsare poorly served by
Monocultures
11
Monday, June 14, 2010
monocultures => monoglot and mono-paradigm programming.
8/13/2019 Polyglot Poly Paradigm
12/101
Mono-
paradigm:
Object-OrientedProgramming:
right for allrequirements?
12
Monday, June 14, 2010
8/13/2019 Polyglot Poly Paradigm
13/101
Is one language
best for all domains?
Monolingual
13
Monday, June 14, 2010
domains: e.g., the problem domain for the app (usually an object model), the security model, the network/web topology, therelational or other data model, ...
8/13/2019 Polyglot Poly Paradigm
14/101
Symptomsof
Monocultures
Why is there so muchXMLin myJava? Why do I have similar code for persistence,
transactions,security,etc. scattered all over my
code base?
14
Monday, June 14, 2010
8/13/2019 Polyglot Poly Paradigm
15/101
Symptomsof
Monocultures
How can I scalemy application to internet scales? Why is my application so hard to extend?
Why cant I respondquickly when requirements
change?
15
Monday, June 14, 2010
8/13/2019 Polyglot Poly Paradigm
16/101
Pervasive Symptom:
Toomuch
code!16
Monday, June 14, 2010
8/13/2019 Polyglot Poly Paradigm
17/101
Lets examine some
common problems
withPPPsolutions:
17
Monday, June 14, 2010
8/13/2019 Polyglot Poly Paradigm
18/101
Change
is slowand painful.
Problem #1
18
Monday, June 14, 2010
8/13/2019 Polyglot Poly Paradigm
19/101
Symptoms
Featurestake toolongtoimplement.
We cant reactfast enoughto change.
Uses want to customizethe
system themselves.19Monday, June 14, 2010
8/13/2019 Polyglot Poly Paradigm
20/101
Solution
Application
Kernelof Components
User Scripts Built-in Scripts
(C Components) + (Lisp scripts) = Emacs
20
Monday, June 14, 2010
8/13/2019 Polyglot Poly Paradigm
21/101
Components+ Scripts
=Applications
see John Ousterhout, IEEE Computer, March 98
21
Monday, June 14, 2010
Pronunciation: OH-stir-howt
8/13/2019 Polyglot Poly Paradigm
22/101
Kernel Components
Statically-typed language:
C, C++, Java, C#, ...
Compiledfor speed, efficiency.
Access OS services, 3rd
-partylibraries.
Lower developer productivity.22Monday, June 14, 2010
8/13/2019 Polyglot Poly Paradigm
23/101
Scripts
Dynamically-typed language:
Ruby, Lisp, JavaScript, Lua, ...
Interpretedfor agility.
Performanceless important.
Gluetogether components.
Raise developer productivity.23Monday, June 14, 2010In practice, the divide between components and scripts is not so distinct.
8/13/2019 Polyglot Poly Paradigm
24/101
In practice,
the boundariesbetweencomponentsand scripts
are not so distinct...
24
Monday, June 14, 2010
8/13/2019 Polyglot Poly Paradigm
25/101
Other Examples:
UNIX/Linux+ shells.Also find, make,grep, ...
Have their own DSLs.
25
Monday, June 14, 2010
8/13/2019 Polyglot Poly Paradigm
26/101
C++/Lua Examples:
Adobe Lightroom 40-50% written in Lua.
Game Engines
26
Monday, June 14, 2010
Lightroom: Lua API used for 3rd-party plugins.Lots of games combine C++ and Lua, too.
8/13/2019 Polyglot Poly Paradigm
27/101
Embedded Systems:
Tektronix Oscilloscopes: C +
Smalltalk. NRAO Telescopes: C + Python.
Google Android: Linux +libraries (C) + Java.
27
Monday, June 14, 2010
Oth E l
8/13/2019 Polyglot Poly Paradigm
28/101
Other Examples:
Multilingual VMs
On theJVM: JRuby, Groovy, Jython,
Scala. Ruby on Rails on JRuby.
28
Monday, June 14, 2010
Another realization of C+S=A is to put several languages on the same VM, rather than using the OS as the component layer.
Oth E l
8/13/2019 Polyglot Poly Paradigm
29/101
Other Examples:
Multilingual VMs Dynamic Language Runtime
(DLR).
Ruby, Python, ... on
the .NET CLR.
29
Monday, June 14, 2010
Another realization of C+S=A is to put several languages on the same VM, rather than using the OS as the component layer.
8/13/2019 Polyglot Poly Paradigm
30/101
XMLin Java
Why not replaceXML
withJavaScript , GroovyorJRuby??
30
Monday, June 14, 2010
De facto scripting language in Java.Not an optimal choice:- All data.- No behavior (to speak of...).- Verbose.
8/13/2019 Polyglot Poly Paradigm
31/101
Benefits
Optimizeperformancewhere it matters. Optimize productivity,extensibility, agility and
end-user customization everywhere else.
Application
Kernelof Components
User Scripts Built-in Scripts
31
Monday, June 14, 2010
This is an underutilized architecture.
8/13/2019 Polyglot Poly Paradigm
32/101
8/13/2019 Polyglot Poly Paradigm
33/101
Anunderutilizedarchitecture!
Application
Kernelof Components
User Scripts Built-in Scripts
33
Monday, June 14, 2010
8/13/2019 Polyglot Poly Paradigm
34/101
Parting Thought...
Why dont Eclipse, IntelliJ, etc.have built-in scriptingengines?
34
Monday, June 14, 2010
8/13/2019 Polyglot Poly Paradigm
35/101
8/13/2019 Polyglot Poly Paradigm
36/101
8/13/2019 Polyglot Poly Paradigm
37/101
The intent
of our code
is lostin the noise.37
Monday, June 14, 2010
8/13/2019 Polyglot Poly Paradigm
38/101
Symptoms
New team members have along learning curve.
The system breakswhen wechangeit.
Translating requirementsto codeis errorprone.
38
Monday, June 14, 2010
8/13/2019 Polyglot Poly Paradigm
39/101
Solution #1
Writeless code!
Youre welcome.
39
Monday, June 14, 2010
8/13/2019 Polyglot Poly Paradigm
40/101
Less Code
Means problemsare smaller:
Maintenance
Duplication
Testing
Performance
etc. 40Monday, June 14, 2010
8/13/2019 Polyglot Poly Paradigm
41/101
S 2
8/13/2019 Polyglot Poly Paradigm
42/101
Solution #2
Separateimplementationdetails
from businesslogic.
42
Monday, June 14, 2010
8/13/2019 Polyglot Poly Paradigm
43/101
Domain Specific
LanguagesMake the code read like
structured domain prose.
43
Monday, June 14, 2010
Example DSLinternal{ caseextension
8/13/2019 Polyglot Poly Paradigm
44/101
p when100...200
callee=User.find_by_extensionextension
unlesscallee.busy?thendialcallee
else
voicemailextension
when111 thenjoin111
when888playweather_report('Dallas, Texas')
when999
play%w(a-connect-charge-of 22
cents-per-minute will-apply) sleep2.seconds
play'just-kidding-not-upset'
check_voicemail
end
}
Adhearsion=
Ruby DSL+
Asterisk+
Jabber/XMPP+...
44
Monday, June 14, 2010
PBX = Private Branch Exchange, the telephony exchange that serves a business or other o!ce, etc.
DSL Ad
8/13/2019 Polyglot Poly Paradigm
45/101
DSL Advantages
Code lookslike domain prose:
Is easier to understand byeveryone,
Is easier to alignwith the
requirements,
Is more succinct.45
Monday, June 14, 2010
DSL Di d
8/13/2019 Polyglot Poly Paradigm
46/101
DSL Disadvantages
46
Many people arepoorAPI designers.
DSLs are harderto design.
Monday, June 14, 2010
DSL Di d
8/13/2019 Polyglot Poly Paradigm
47/101
DSL Disadvantages
47
DSLs can be hardtoimplement, test, and debug.
Monday, June 14, 2010
8/13/2019 Polyglot Poly Paradigm
48/101
48
Monday, June 14, 2010
Not too many of this examples yet, but one comes to mind: mocking (for testing) frameworks in Ruby, BDD tools in severallanguages.
8/13/2019 Polyglot Poly Paradigm
49/101
Parting Thought...Perfectionis achieved,
not when there is nothing left to add,but when there is nothing left to remove.
-- Antoine de Saint-Exupery
49
Monday, June 14, 2010
He wrote The Little Prince, among other books. He was an aviator who disappeared over the Mediterranean in 1944, flying forFree French Forces.
8/13/2019 Polyglot Poly Paradigm
50/101
Parting Thought #2...
Everything should be made as simpleas possible, but not simpler.
-- Albert Einstein
50
Monday, June 14, 2010
8/13/2019 Polyglot Poly Paradigm
51/101
Corollary:Entia non sunt multiplicanda
praeter necessitatem.
-- Occams Razor
51
Monday, June 14, 2010
a.k.a. Law of Parsimony or Law of Succinctness. Paraphrased translation.
8/13/2019 Polyglot Poly Paradigm
52/101
Corollary:All other things being equal,
the simplestsolutionis the best.
-- Occams Razor
52
Monday, June 14, 2010
a.k.a. Law of Parsimony or Law of Succinctness. Paraphrased translation.
We have
8/13/2019 Polyglot Poly Paradigm
53/101
codeduplicationever where.
Problem #3
Monday, June 14, 2010
Symptoms
8/13/2019 Polyglot Poly Paradigm
54/101
Symptoms
Persistence logicis embeddedin everydomain class.
Error handling and logging isinconsistent.
Cross-Cutting Concerns.
54
Monday, June 14, 2010
8/13/2019 Polyglot Poly Paradigm
55/101
Solution
Aspect-Oriented
Programming
55
Monday, June 14, 2010
Removing Duplication
8/13/2019 Polyglot Poly Paradigm
56/101
Removing Duplication
In order, use:
Objector functionaldecomposition.
DSLs.
Aspects.
56
Monday, June 14, 2010
Make sure your object and functional decomposition is right first, then use DSLs appropriately. Finally, use aspects.
8/13/2019 Polyglot Poly Paradigm
57/101
classBankAccount
8/13/2019 Polyglot Poly Paradigm
58/101
attr_reader :balance
def credit(amount)
@balance+= amount
end def debit(amount) @balance-= amount
end
end
Clean Code
58
Monday, June 14, 2010
But, real applications need:
8/13/2019 Polyglot Poly Paradigm
59/101
But, real applications need:
def BankAccount
attr_reader :balancedef credit(amount)
... end
def debit(amount)...
end
end
Transactions
Persistence
Security
59
Monday, June 14, 2010
So creditbecomes
8/13/2019 Polyglot Poly Paradigm
60/101
defcredit(amount) raise if unauthorized()
save_balance = @balance
begin
begin_transaction()
@balance+= amount persist_balance(@balance)
60
Monday, June 14, 2010
8/13/2019 Polyglot Poly Paradigm
61/101
rescue => error log(error)
@balance= saved_balance
ensure
end_transaction()
endend
61
Monday, June 14, 2010
Were mixing multipledomains,
8/13/2019 Polyglot Poly Paradigm
62/101
g p
Transactions
Persistence
Security
with fine-grained intersections.
Problem Domain
tangled code
scattered logic62
Monday, June 14, 2010
In principle, I can reason about transactions, etc. in isolation, but in reality, the code for transactions is scattered over the wholesystem. Similarly, the once-clean domain model code is tangled with code from the other concerns.Objects dont prevent this problem (in most cases).
8/13/2019 Polyglot Poly Paradigm
63/101
Objectsalonedontpreventtangling.
63
Monday, June 14, 2010
8/13/2019 Polyglot Poly Paradigm
64/101
Aspect-OrientedProgramming:
restore modularityforcross-cutting concerns.
64
Monday, June 14, 2010
Aspectsrestore modularityby
8/13/2019 Polyglot Poly Paradigm
65/101
p y y
encapsulating the intersections.Transactions
Persistence
Security
TransactionAspect
PersistenceAspect
SecurityAspect
65
See extra slides
Monday, June 14, 2010
8/13/2019 Polyglot Poly Paradigm
66/101
If you have used the
Spring Framework,you have
usedaspects.
66
Monday, June 14, 2010
Parting Thought...
8/13/2019 Polyglot Poly Paradigm
67/101
a t g oug t...
Metaprogrammingcan be usedfor some aspect-likefunctionality.
DSLscan solve somecross-cutting concerns, by localizingbehaviors expressed by the DSL.
67
Monday, June 14, 2010
Our service
8/13/2019 Polyglot Poly Paradigm
68/101
Problem #4
flickr.com/photos/wolfro54
Our service
must beavailable 24x7
and highlyscalable.
Monday, June 14, 2010
Symptoms
8/13/2019 Polyglot Poly Paradigm
69/101
y p
Only oneof our developers
reallyknowshow to writethread-safecode.
The system freezesevery fewweeksor so.
69
Monday, June 14, 2010
8/13/2019 Polyglot Poly Paradigm
70/101
Solution
Functional
Programming
70Monday, June 14, 2010
(At least, its one solution...)
Functional Programming
8/13/2019 Polyglot Poly Paradigm
71/101
g g
y = sin(x)
71
Modeled after mathematics.
Monday, June 14, 2010
Functional Programming
8/13/2019 Polyglot Poly Paradigm
72/101
g g
y = sin(x)
72
Values are immutable.Variables are assigned once.
Monday, June 14, 2010
Functional Programming
8/13/2019 Polyglot Poly Paradigm
73/101
g g
y = sin(x)
73
Functions are side-effect free.Functions dont alter state.
The resultdepends solelyon the arguments.
Monday, June 14, 2010
Functional Programming:
8/13/2019 Polyglot Poly Paradigm
74/101
g g
ConcurrencyIsEasier
74
No writes,so no synchronization.Hence, nolocks, semaphores, mutexes...
y = sin(x)
Monday, June 14, 2010
Functional Programming:
8/13/2019 Polyglot Poly Paradigm
75/101
g g
ReasoningisEasier
75
Without side effects,functions are easierto test, understand, ...
y = sin(x)
and reuse!
Monday, June 14, 2010
Which fits your needs?
8/13/2019 Polyglot Poly Paradigm
76/101
Object Oriented
76
Account
deposit(...)
withdraw(...)
CheckingAccount
deposit(...)
withdraw(...)
SavingsAccount
deposit(...)
withdraw(...)
??
deposit(...)
withdraw(...)
Monday, June 14, 2010
Do operations vary significantly, depending on data type or ...
Which fits your needs?
8/13/2019 Polyglot Poly Paradigm
77/101
list map
fold/
reduce
filter
Functional
77
Monday, June 14, 2010
or do operations more or less work the same independent of the data type?
What if
8/13/2019 Polyglot Poly Paradigm
78/101
What ifyoure
doing cloudcomputing?
78
E.g., is map-reduceobject-orientedor functional?
Monday, June 14, 2010
FP Code:
8/13/2019 Polyglot Poly Paradigm
79/101
more declarativethan imperative.
79
F(n) = F(n-1) + F(n-2)
where: F(0) = 0 and F(1) = 1
0, 1, 1, 2, 3, 5, 8, 13, ...
Monday, June 14, 2010
The Fibonacci Sequence.I tell the system what I want (e.g., what are the relationships between data, the constraints, etc.) and let the system figure outhow to do it.
and so are DSLs.
8/13/2019 Polyglot Poly Paradigm
80/101
classCustomer < ActiveRecord::Base
has_many :accounts
validates_uniqueness_of :name,
:on =>create,
:message=>Evil twin!
end
80Monday, June 14, 2010
By hiding the implementation details, we have much more leeway in implementing aspect behavior, etc.
8/13/2019 Polyglot Poly Paradigm
81/101
A FewFunctionalLanguages
81Monday, June 14, 2010
8/13/2019 Polyglot Poly Paradigm
82/101
Erlang
8/13/2019 Polyglot Poly Paradigm
83/101
No mutable variablesand side effects.
Uses the actor modelof concurrency.
All IPC is optimized message passing.
Let it fail philosophy.
Verylightweight and fast processes. Lighter than most OS threads.
83
Monday, June 14, 2010
Scala
8/13/2019 Polyglot Poly Paradigm
84/101
Hybrid: object and functional.
Targets theJVMand .NET. EndorsedbyJames Goslingat JavaOne.
Could be the most popular
replacementfor Java.
84
Monday, June 14, 2010
Times Change...
8/13/2019 Polyglot Poly Paradigm
85/101
...
Monday, June 14, 2010
Clojure
8/13/2019 Polyglot Poly Paradigm
86/101
Functional, with principledsupport formutability.
Targets theJVMand .NET.
Bestbuzz?
Too manygood ideasto name here...
86
Monday, June 14, 2010
Functional Languages inIndustry
8/13/2019 Polyglot Poly Paradigm
87/101
Industry
Erlang
CouchDB, Basho Riak, andAmazonsSimple DB.
GitHub
Jabber/XMPP server ejabberd.
87
Monday, June 14, 2010
8/13/2019 Polyglot Poly Paradigm
88/101
Parting Thought...
8/13/2019 Polyglot Poly Paradigm
89/101
Which is better:A hybrid object-functionallanguage
for everything?An objectlanguage for some code anda functionallanguage for other code?
e.g., Scalavs.Java +Erlang??
89
Monday, June 14, 2010
Scala is more complex than mono-paradigm languages, so its harder to master. However, using multiple languages has itsown challenges.
8/13/2019 Polyglot Poly Paradigm
90/101
PolyglotandPoly-paradigmProgramming(PPP)
Recap:
90
Monday, June 14, 2010
Disadvantagesof PPP
8/13/2019 Polyglot Poly Paradigm
91/101
Ntool chains, languages, libraries,ecosystems, idioms, ...
Impedance mismatchbetween tools.
Different meta-models.
Overheadof calls between languages.
91
Monday, June 14, 2010
Advantagesof PPP
8/13/2019 Polyglot Poly Paradigm
92/101
Can use the best toolfor a particular job.
Can minimize the amountof code
required.
Can keep code closerto the domainusing DSLs.
Encourages thinking about architecture.
92
Monday, June 14, 2010
Is This New?
8/13/2019 Polyglot Poly Paradigm
93/101
Functional Programming Comes of Age.
Dr. Dobbs, 1994
Scripting: Higher Level Programming forthe 21stCentury.
IEEE Computer, 1998
In Praise of Scripting: Real ProgrammingPragmatism.
IEEE Computer, 2008Monday, June 14, 2010
Why go mainstreamnow?
8/13/2019 Polyglot Poly Paradigm
94/101
Rapidly increasing pace of development,!Scripting (dynamic languages), DSLs.
Pervasive concurrency(e.g.,Multicore CPUs)!Functional programming.
Cross-cutting concerns
!Aspect-oriented programming.
94
Monday, June 14, 2010
Thank You!
8/13/2019 Polyglot Poly Paradigm
95/101
Watch for the IEEE Softwarespecial issue, Sept/Oct 2010. polyglotprogramming.com
95
Monday, June 14, 2010
http://creativecommons.org/licenses/by/3.0/http://creativecommons.org/licenses/by/3.0/http://creativecommons.org/licenses/by/3.0/http://creativecommons.org/licenses/by/3.0/http://creativecommons.org/licenses/by/3.0/http://creativecommons.org/licenses/by/3.0/http://creativecommons.org/licenses/by/3.0/http://creativecommons.org/licenses/by/3.0/http://creativecommons.org/licenses/by/3.0/mailto:[email protected]:[email protected]8/13/2019 Polyglot Poly Paradigm
96/101
Extra Slides
96
Monday, June 14, 2010
8/13/2019 Polyglot Poly Paradigm
97/101
I would like to write
8/13/2019 Polyglot Poly Paradigm
98/101
Beforereturning the balance, readthecurrent valuefrom the database.
Beforeaccessing the BankAccount,authenticateandauthorizethe user.
Aftersetting the balance, writethecurrent valueto the database.
98
Monday, June 14, 2010
8/13/2019 Polyglot Poly Paradigm
99/101
Aftersetting the balance, writethecurrent valueto the database.
99
Monday, June 14, 2010
Aquariumuse aquarium lib.
8/13/2019 Polyglot Poly Paradigm
100/101
require aquariumclassBankAccount
after :writing => :balance \ do |context, account, *args|
persist_balanceaccount
end
reopen class
event to trigger on
aquarium.rubyforge.org100
new behavior
Monday, June 14, 2010
8/13/2019 Polyglot Poly Paradigm
101/101