Polyglot and Poly-paradigm
Programming
for Better Agility
Dean Wampler
dean@deanwampler.com
@deanwampler
1
2
Co-author,
Programming
Scala
programmingscala.com
Polyglot:
many languages
Poly-paradigm:
many modularity
paradigms 3
Today’s
applications:
•Are networked,
•Have graphical
and “service”
interfaces,
flickr.com/photos/jerryjohn 4
Today’s
applications:
•Persist data,
•Must be resilient
and secure,
•Must scale,
flickr.com/photos/jerryjohn 5
Today’s
applications:
•… and must do
all that by Friday.
flickr.com/photos/jerryjohn 6
flickr.com/photos/deanwampler
Mono-
paradigm:
Object-Oriented
Programming:
right for all
problems?
7
twitter.com/photos/watchsmart
Is one language
best for all
domains?
Monolingual
8
Symptoms of
Monocultures
• Why is there so much XML in my Java?
• Why do I have similar persistence code scattered
all ove...
switch (elementItem)
{
case "header1:SearchBox" :
{
__doPostBack('header1:goSearch','');
break;
}
case "Text1":
{
window.e...
Solutions
The symptoms reflect
common problems
with similar solutions.
11
I need
extensibility
and agility.
Specific problem #1
flickr.com/photos/arrrika 12
Symptoms
•Features take too long to
implement.
•We can’t react fast enough
to change.
•Uses want to customize the
system t...
Solution
Application
Kernel of Components
User Scripts Built-in Scripts
(C Components) + (Lisp scripts) = Emacs
14
Components + Scripts
=
Applications
see John Ousterhout, IEEE Computer, March ’98
15
Kernel Components
•Statically-typed language:
•C, C++, Java, C#, ...
•Compiled for speed, efficiency.
•Access OS services, ...
Scripts
•Dynamically-typed language:
•Ruby, Python, JavaScript, Lua, ...
•Interpreted for agility.
•Performance less impor...
In practice,
the boundaries between
components and scripts
are not so distinct...
18
Ola Bini’s Three Layers
•Domain layer
•Internal and External DSLs.
•Dynamic layer
•e.g., JRuby, application code
•Stable l...
Other Examples
of This Approach
•UNIX/Linux + shells.
•Also find, make, grep, ...
•Have their own DSLs.
•Tektronix Oscillos...
Other Examples...
• Adobe Lightroom: C++ + Lua.
• 40-50% written in Lua.
• NRAOTelescopes: C + Python.
• Google Android: L...
<view-state id="displayResults" view="/searchResults.jsp">
	

 	

 <render-actions>
	

 	

 	

 <bean-action bean="phonebo...
Other Examples:
MultilingualVM’s
• On the JVM:
• JRuby, Groovy, Jython,
Scala.
• Ruby on Rails on JRuby.
23
Other Examples:
MultilingualVM’s
• Dynamic Language Runtime
(DLR).
• Ruby, Python, ... on
the .NET CLR.
24
Benefits
•Optimize performance where it matters.
•Optimize productivity, extensibility and agility
everywhere else.
Applica...
Disadvantages
Application
Kernel of Components
User Scripts Built-in Scripts
26
•More complexity with 2+ languages
(idioms...
An underutilized
architecture!
Application
Kernel of Components
User Scripts Built-in Scripts
27
Parting Thought...
Cell phone makers are
drowning in C++.
(One reason the IPhone
and Android are interesting.)
28
I don’t
know what
my code is
doing.
flickr.com/photos/dominic99
Specific problem #2
The intent
of our code
is lost
in the noise.30
Symptoms
•Business logic is obscure when I
read the code.
•The system breaks when we
change it.
•Translating requirements ...
Solution #1
Write
less code.
Profound statement.
32
Less Code
• Means problems are smaller:
• Maintenance
• Duplication (DRY)
• Testing
• Performance
• etc. 33
How to Write
Less Code
• Root out duplication.
• Use economical designs.
• Functional vs. Object-Oriented?
• Use economica...
Solution #2
Separate
implementation details
from business logic.
35
Domain Specific
Languages
Make the code read like
“structured” domain prose.
36
Example DSLinternal {
case extension
when 100...200
callee = User.find_by_extension extension
unless callee.busy? then dia...
DSL Advantages
• Code looks like domain prose:
• Is easier to understand by
everyone,
• Is easier to align with the
requir...
DSL Disadvantages
• DSLs are hard to design, test
and debug.
• Some people are bad API
designers.
39
Brueghel the Elder
A DSL Tower of Babel?
40
Parting Thought...
Perfection is achieved,
not when there is nothing left to add,
but when there is nothing left to remove...
Parting Thought #2...
Everything should be made as simple
as possible, but not simpler.
-- Albert Einstein
42
Corollary:
Entia non sunt multiplicanda
praeter necessitatem.
-- Occam’s Razor
43
Corollary:
All other things being equal,
the simplest solution is the best.
-- Occam’s Razor
44
We have
code duplication
everywhere.
Specific
problem #3
deanwampler
Symptoms
• Persistence logic is embedded
in every “domain” class.
• Error handling and logging is
inconsistent.
Cross-Cutt...
Solution
Aspect-Oriented
Programming
47
Removing Duplication
• In order, use:
• Object or functional decomposition.
• DSLs.
• Aspects.
48
An Example...
49
class BankAccount
	 attr_reader :balance
	 def credit(amount)
@balance += amount
end
	 def debit(amount)
@balance -= amoun...
But, real applications need:
def BankAccount
	 attr_reader :balance
	 def credit(amount)
...
end
	 def debit(amount)
...
e...
def credit(amount)
raise “…” if unauthorized()
save_balance = @balance
begin
begin_transaction()
@balance += amount
persis...
…
rescue => error
log(error)
@balance = saved_balance
ensure
end_transaction()
end
end
53
We’re mixing multiple domains,
Transactions
Persistence
Security
with fine-grained intersections.
“Problem Domain”
“tangled...
Objects alone don’t
prevent tangling.
55
Aspect-Oriented
Programming:
restore modularity for
cross-cutting concerns.
56
Aspects restore modularity by
encapsulating the intersections.
Transactions
Persistence
Security
Transaction
Aspect
Persis...
If you have used the
Spring Framework,
you have
used aspects.
58
• Ruby
• Aquarium
• Facets
• AspectR
Aspect-Oriented Tools
• Java
• AspectJ
• Spring AOP
• JBoss AOP
shameless plug
59
I would like to write…
Before returning the balance, read the
current value from the database.
Before accessing the BankAc...
I would like to write…
Before returning the balance, read the
current value from the database.
Before accessing the BankAc...
require ‘aquarium’
class BankAccount
…
after :writing => :balance 
do |context, account, *args|
persist_balance account
en...
def credit(amount)
@balance += amount
end
Back to clean code
63
Parting Thought...
Metaprogramming can be used
for some aspect-like functionality.
DSLs can solve some
cross-cutting conce...
Specific problem #4
flickr.com/photos/wolfro54
Our service
must be
available 24x7
and highly
scalable.
Symptoms
• Only one of our developers
really knows how to write
thread-safe code.
• The system freezes every few
weeks or ...
Solution
Functional
Programming
67
Functional Programming
•Works like mathematical functions.
Fibonacci Numbers:
F(n) = F(n-1) + F(n-2)
where: F(1) = 1 and F...
Functional Programming
• Variables are assigned once.
• Functions are side-effect free.
• They don’t alter state.
y = sin(...
Functional Programming
Makes Concurrency
Easier
• Nothing to synchronize.
• Hence no locks, semaphores,
mutexes...
70
Which fits your needs?
Object Oriented
71
Account
deposit(...)
withdraw(...)
CheckingAccount
deposit(...)
withdraw(...)
Sav...
list map
fold/
reduce
filter
Which fits your needs?
Functional
72
flickr.com/photos/deanwampler
What if
you’re
doing cloud
computing?
73 deanwampler
Is it object-oriented
or functional?
FP Code is
declarative
rather than
imperative.
F(n) = F(n-1) + F(n-2)
where: F(1) = 1 and F(2) = 1
74
… and so are DSLs.
class Customer < ActiveRecord::Base
has_many :accounts
validates_uniqueness_of :name,
:on => create,
:m...
A Few
Functional Languages
76
Erlang
• Ericsson Functional Language.
• For distributed, reliable, soft real-time,
highly concurrent systems.
• Used in t...
Erlang
• No mutable variables and side effects.
• All IPC is optimized message passing.
• Very lightweight and fast proces...
Scala
• Hybrid: object and functional.
• Targets the JVM and .NET.
• “Endorsed” by James Gosling at JavaOne.
• Used at Twi...
Clojure
• Functional, with principled support for
mutability.
• Targets the JVM and .NET.
• Best buzz?
• Too many good ide...
Functional Languages in
Production
• Erlang
• CouchDB
• GitHub
• Jabber/XMPP server ejabberd.
• Amazon’s Simple DB.
81
Functional Languages in
Production
• OCaml
• Jane Street Capital
• Scala
• Twitter
• LinkedIn
82
Parting Thought...
Is a hybrid object-functional language
better than using an object language
with a functional language?...
Polyglot and Poly-paradigm
Programming (PPP)
for Better Agility
Recap:
84
Disadvantages of PPP
• N tool chains, languages, libraries,
“ecosystems”, ...
• Impedance mismatch between tools.
• Differ...
Advantages of PPP
• Can use the best tool for a particular job.
• Can minimize the amount of code
required.
• Can keep cod...
Everything old
is new again.
• Functional Programming Comes of Age.
• Dr. Dobbs, 1994
• Scripting: Higher Level Programmin...
Why go mainstream now?
•Rapidly increasing pace of development,
➔ Scripting (dynamic languages), DSLs.
•Pervasive concurre...
Common Themes
• Less code is more.
• Keep the code close to the domain:
DSLs.
• Be declarative rather than imperative.
• M...
ThankYou!
• dean@deanwampler.com
• Watch for the IEEE Software
special issue, Sept/Oct 2010.
• polyglotprogramming.com
90
Upcoming SlideShare
Loading in...5
×

Polyglot and Poly-paradigm Programming for Better Agility

2,721

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
2,721
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
15
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Polyglot and Poly-paradigm Programming for Better Agility

  1. 1. Polyglot and Poly-paradigm Programming for Better Agility Dean Wampler dean@deanwampler.com @deanwampler 1
  2. 2. 2 Co-author, Programming Scala programmingscala.com
  3. 3. Polyglot: many languages Poly-paradigm: many modularity paradigms 3
  4. 4. Today’s applications: •Are networked, •Have graphical and “service” interfaces, flickr.com/photos/jerryjohn 4
  5. 5. Today’s applications: •Persist data, •Must be resilient and secure, •Must scale, flickr.com/photos/jerryjohn 5
  6. 6. Today’s applications: •… and must do all that by Friday. flickr.com/photos/jerryjohn 6
  7. 7. flickr.com/photos/deanwampler Mono- paradigm: Object-Oriented Programming: right for all problems? 7
  8. 8. twitter.com/photos/watchsmart Is one language best for all domains? Monolingual 8
  9. 9. Symptoms of Monocultures • Why is there so much XML in my Java? • Why do I have similar persistence code scattered all over my code base? • How can I scale my application by a factor of 1000! • My application is hard to extend! • I can’t respond quickly when requirements change! 9
  10. 10. switch (elementItem) { case "header1:SearchBox" : { __doPostBack('header1:goSearch',''); break; } case "Text1": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } ... thedailywtf.com Pervasive IT problem: Too much code! 10
  11. 11. Solutions The symptoms reflect common problems with similar solutions. 11
  12. 12. I need extensibility and agility. Specific problem #1 flickr.com/photos/arrrika 12
  13. 13. Symptoms •Features take too long to implement. •We can’t react fast enough to change. •Uses want to customize the system themselves.13
  14. 14. Solution Application Kernel of Components User Scripts Built-in Scripts (C Components) + (Lisp scripts) = Emacs 14
  15. 15. Components + Scripts = Applications see John Ousterhout, IEEE Computer, March ’98 15
  16. 16. Kernel Components •Statically-typed language: •C, C++, Java, C#, ... •Compiled for speed, efficiency. •Access OS services, 3rd-party libraries. •Lower developer productivity. 16
  17. 17. Scripts •Dynamically-typed language: •Ruby, Python, JavaScript, Lua, ... •Interpreted for agility. •Performance less important. •Glue together components. •Raise developer productivity.17
  18. 18. In practice, the boundaries between components and scripts are not so distinct... 18
  19. 19. Ola Bini’s Three Layers •Domain layer •Internal and External DSLs. •Dynamic layer •e.g., JRuby, application code •Stable layer •JVM + generic libraries19
  20. 20. Other Examples of This Approach •UNIX/Linux + shells. •Also find, make, grep, ... •Have their own DSLs. •Tektronix Oscilloscopes: C + Smalltalk. 20
  21. 21. Other Examples... • Adobe Lightroom: C++ + Lua. • 40-50% written in Lua. • NRAOTelescopes: C + Python. • Google Android: Linux + libraries (C) + Java. 21
  22. 22. <view-state id="displayResults" view="/searchResults.jsp"> <render-actions> <bean-action bean="phonebook" method="search"> <method-arguments> <argument expression="searchCriteria"/> </method-arguments> <method-result name="results" scope="flash"/> </bean-action> </render-actions> <transition on="select" to="browseDetails"/> <transition on="newSearch" to="enterCriteria"/> </view-state> </flow> XML in Java Why not replace XML with JavaScript , Groovy or JRuby?? 22
  23. 23. Other Examples: MultilingualVM’s • On the JVM: • JRuby, Groovy, Jython, Scala. • Ruby on Rails on JRuby. 23
  24. 24. Other Examples: MultilingualVM’s • Dynamic Language Runtime (DLR). • Ruby, Python, ... on the .NET CLR. 24
  25. 25. Benefits •Optimize performance where it matters. •Optimize productivity, extensibility and agility everywhere else. Application Kernel of Components User Scripts Built-in Scripts 25
  26. 26. Disadvantages Application Kernel of Components User Scripts Built-in Scripts 26 •More complexity with 2+ languages (idioms, tools, ...).
  27. 27. An underutilized architecture! Application Kernel of Components User Scripts Built-in Scripts 27
  28. 28. Parting Thought... Cell phone makers are drowning in C++. (One reason the IPhone and Android are interesting.) 28
  29. 29. I don’t know what my code is doing. flickr.com/photos/dominic99 Specific problem #2
  30. 30. The intent of our code is lost in the noise.30
  31. 31. Symptoms •Business logic is obscure when I read the code. •The system breaks when we change it. •Translating requirements to code is error prone. 31
  32. 32. Solution #1 Write less code. Profound statement. 32
  33. 33. Less Code • Means problems are smaller: • Maintenance • Duplication (DRY) • Testing • Performance • etc. 33
  34. 34. How to Write Less Code • Root out duplication. • Use economical designs. • Functional vs. Object-Oriented? • Use economical languages. 34
  35. 35. Solution #2 Separate implementation details from business logic. 35
  36. 36. Domain Specific Languages Make the code read like “structured” domain prose. 36
  37. 37. Example DSLinternal { case extension when 100...200 callee = User.find_by_extension extension unless callee.busy? then dial callee else voicemail extension when 111 then join 111 when 888 play weather_report('Dallas, Texas') when 999 play %w(a-connect-charge-of 22 cents-per-minute will-apply) sleep 2.seconds play 'just-kidding-not-upset' check_voicemail end } Adhearsion = Ruby DSL + Asterisk + Jabber/XMPP + ... 37
  38. 38. DSL Advantages • Code looks like domain prose: • Is easier to understand by everyone, • Is easier to align with the requirements, • Is more succinct. 38
  39. 39. DSL Disadvantages • DSLs are hard to design, test and debug. • Some people are bad API designers. 39
  40. 40. Brueghel the Elder A DSL Tower of Babel? 40
  41. 41. Parting Thought... Perfection is achieved, not when there is nothing left to add, but when there is nothing left to remove. -- Antoine de Saint-Exupery 41
  42. 42. Parting Thought #2... Everything should be made as simple as possible, but not simpler. -- Albert Einstein 42
  43. 43. Corollary: Entia non sunt multiplicanda praeter necessitatem. -- Occam’s Razor 43
  44. 44. Corollary: All other things being equal, the simplest solution is the best. -- Occam’s Razor 44
  45. 45. We have code duplication everywhere. Specific problem #3 deanwampler
  46. 46. Symptoms • Persistence logic is embedded in every “domain” class. • Error handling and logging is inconsistent. Cross-Cutting Concerns. 46
  47. 47. Solution Aspect-Oriented Programming 47
  48. 48. Removing Duplication • In order, use: • Object or functional decomposition. • DSLs. • Aspects. 48
  49. 49. An Example... 49
  50. 50. class BankAccount attr_reader :balance def credit(amount) @balance += amount end def debit(amount) @balance -= amount end … end Clean Code 50
  51. 51. But, real applications need: def BankAccount attr_reader :balance def credit(amount) ... end def debit(amount) ... end end Transactions Persistence Security 51
  52. 52. def credit(amount) raise “…” if unauthorized() save_balance = @balance begin begin_transaction() @balance += amount persist_balance(@balance) … So credit becomes… 52
  53. 53. … rescue => error log(error) @balance = saved_balance ensure end_transaction() end end 53
  54. 54. We’re mixing multiple domains, Transactions Persistence Security with fine-grained intersections. “Problem Domain” “tangled” code “scattered” logic 54
  55. 55. Objects alone don’t prevent tangling. 55
  56. 56. Aspect-Oriented Programming: restore modularity for cross-cutting concerns. 56
  57. 57. Aspects restore modularity by encapsulating the intersections. Transactions Persistence Security Transaction Aspect Persistence Aspect Security Aspect 57
  58. 58. If you have used the Spring Framework, you have used aspects. 58
  59. 59. • Ruby • Aquarium • Facets • AspectR Aspect-Oriented Tools • Java • AspectJ • Spring AOP • JBoss AOP shameless plug 59
  60. 60. I would like to write… Before returning the balance, read the current value from the database. Before accessing the BankAccount, authenticate and authorize the user. After setting the balance, write the current value to the database. 60
  61. 61. I would like to write… Before returning the balance, read the current value from the database. Before accessing the BankAccount, authenticate and authorize the user. After setting the balance, write the current value to the database. 61
  62. 62. require ‘aquarium’ class BankAccount … after :writing => :balance do |context, account, *args| persist_balance account end … reopen class add new behavior Aquarium aquarium.rubyforge.org62
  63. 63. def credit(amount) @balance += amount end Back to clean code 63
  64. 64. Parting Thought... Metaprogramming can be used for some aspect-like functionality. DSLs can solve some cross-cutting concerns. (We’ll come back to that.) 64
  65. 65. Specific problem #4 flickr.com/photos/wolfro54 Our service must be available 24x7 and highly scalable.
  66. 66. Symptoms • Only one of our developers really knows how to write thread-safe code. • The system freezes every few weeks or so. 66
  67. 67. Solution Functional Programming 67
  68. 68. Functional Programming •Works like mathematical functions. Fibonacci Numbers: F(n) = F(n-1) + F(n-2) where: F(1) = 1 and F(2) = 1 68
  69. 69. Functional Programming • Variables are assigned once. • Functions are side-effect free. • They don’t alter state. y = sin(x) 69
  70. 70. Functional Programming Makes Concurrency Easier • Nothing to synchronize. • Hence no locks, semaphores, mutexes... 70
  71. 71. Which fits your needs? Object Oriented 71 Account deposit(...) withdraw(...) CheckingAccount deposit(...) withdraw(...) SavingsAccount deposit(...) withdraw(...) ?? deposit(...) withdraw(...)
  72. 72. list map fold/ reduce filter Which fits your needs? Functional 72
  73. 73. flickr.com/photos/deanwampler What if you’re doing cloud computing? 73 deanwampler Is it object-oriented or functional?
  74. 74. FP Code is declarative rather than imperative. F(n) = F(n-1) + F(n-2) where: F(1) = 1 and F(2) = 1 74
  75. 75. … and so are DSLs. class Customer < ActiveRecord::Base has_many :accounts validates_uniqueness_of :name, :on => create, :message => ‘Evil twin!’ end 75
  76. 76. A Few Functional Languages 76
  77. 77. Erlang • Ericsson Functional Language. • For distributed, reliable, soft real-time, highly concurrent systems. • Used in telecom switches. • 9-9’s reliability for AXD301 switch. 77
  78. 78. Erlang • No mutable variables and side effects. • All IPC is optimized message passing. • Very lightweight and fast processes. • Lighter than most OS threads. • Let it fail philosophy. 78
  79. 79. Scala • Hybrid: object and functional. • Targets the JVM and .NET. • “Endorsed” by James Gosling at JavaOne. • Used at Twitter, Linkedin, ... • Could be the most popular replacement for Java. 79
  80. 80. Clojure • Functional, with principled support for mutability. • Targets the JVM and .NET. • Best buzz? • Too many good ideas to name here... 80
  81. 81. Functional Languages in Production • Erlang • CouchDB • GitHub • Jabber/XMPP server ejabberd. • Amazon’s Simple DB. 81
  82. 82. Functional Languages in Production • OCaml • Jane Street Capital • Scala • Twitter • LinkedIn 82
  83. 83. Parting Thought... Is a hybrid object-functional language better than using an object language with a functional language?? e.g., Scala vs. Java + Erlang?? 83
  84. 84. Polyglot and Poly-paradigm Programming (PPP) for Better Agility Recap: 84
  85. 85. Disadvantages of PPP • N tool chains, languages, libraries, “ecosystems”, ... • Impedance mismatch between tools. • Different meta-models. • Overhead of calls between languages. 85
  86. 86. Advantages of PPP • Can use the best tool for a particular job. • Can minimize the amount of code required. • Can keep code closer to the domain. • Encourages thinking about architectures. • E.g., decoupling between “components”. 86
  87. 87. Everything old is new again. • Functional Programming Comes of Age. • Dr. Dobbs, 1994 • Scripting: Higher Level Programming for the 21st Century. • IEEE Computer, 1998 • In Praise of Scripting: Real Programming Pragmatism. • IEEE Computer, 2008
  88. 88. Why go mainstream now? •Rapidly increasing pace of development, ➔ Scripting (dynamic languages), DSLs. •Pervasive concurrency (e.g., Multicore CPUs) ➔ Functional programming. •Cross-cutting concerns ➔ Aspect-oriented programming. 88
  89. 89. Common Themes • Less code is more. • Keep the code close to the domain: DSLs. • Be declarative rather than imperative. • Minimize side effects and mutable data. 89
  90. 90. ThankYou! • dean@deanwampler.com • Watch for the IEEE Software special issue, Sept/Oct 2010. • polyglotprogramming.com 90
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×