89
Radical Simplification through Polyglot and Poly-paradigm Programming Dean Wampler [email protected] Object Mentor, Inc. 1 Thursday, November 20, 2008

Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

  • Upload
    others

  • View
    12

  • Download
    0

Embed Size (px)

Citation preview

Page 2: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

Polyglot:

many languages

Poly-paradigm:

many modularity paradigms

2Thursday, November 20, 2008

Page 3: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

Why a talk on PPP?

• It reflects some industry trends,

• which reflect some real problems,

• where our monocultures are letting us down.

3Thursday, November 20, 2008

Page 4: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

twitter.com/photos/jerryjohn

• ... and must do all that by next Friday.

• Are networked,

• Have graphical and “service” interfaces,

• Persist information,

• Must be resilient and secure,

• Must scale,

Today’s applications:

4Thursday, November 20, 2008

Page 5: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

twitter.com/photos/deanwampler

Mono-paradigm:

Object-Oriented Programming:

right for all problems?

5Thursday, November 20, 2008

Page 6: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

twitter.com/photos/watchsmart

Is one language

best for all domains?

Monolingual

6Thursday, November 20, 2008

Page 7: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

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?

• I can’t scale my application by a factor of 1000!

• My application isn’t extensible enough!

• I can’t respond quickly enough when requirements change!

7Thursday, November 20, 2008

Page 8: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

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!

8Thursday, November 20, 2008

Page 9: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

SolutionsThe symptoms reflect common root problemswith similar solutions.

9Thursday, November 20, 2008

Page 10: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

I need extensibility and agility.

Specific problem #1

twitter.com/photos/arrrika10Thursday, November 20, 2008

Page 11: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

Symptoms

• Features take too long to implement.

• We can’t react fast enough to change.

• Uses want to customize the system themselves.

11Thursday, November 20, 2008

Page 12: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

SolutionApplication

Kernel of Components

User Scripts Built-in Scripts

(C Components) + (Lisp scripts) = Emacs

12Thursday, November 20, 2008

Page 13: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

Components + Scripts =

Applications

see John Ousterhout, IEEE Computer, March ’98

13Thursday, November 20, 2008

Page 14: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

Kernel Components

• Written in a statically-typed language.

• C, C++, Java, C#, ...

• Compiled for speed, efficiency.

• Access OS services, 3rd-party libraries.

• Lower developer productivity.

14Thursday, November 20, 2008

Page 15: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

Scripts

• Written in a dynamically-typed language.

• Ruby, Python, JavaScript, Lua, Perl, Tcl, ...

• Interpreted for extensibility and agility.

• Runtime performance is less important.

• Glue together components.

• Higher developer productivity.

15Thursday, November 20, 2008

In practice, the divide between components and scripts is not so distinct.

Page 16: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

To be clear about typing,

• Static typing ➔ checking at compile time.

• Dynamic typing ➔ checking at run time.

16Thursday, November 20, 2008

Page 17: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

In practice, the boundaries between components and scripts

are not so distinct...

17Thursday, November 20, 2008

Page 18: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

Ola Bini’s Three Layers

• Domain layer

• Internal and External DSLs.

• Dynamic layer

• e.g., JRuby and most application code

• Stable layer

• JVM + generic libraries

18Thursday, November 20, 2008

Page 19: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

Other Examples

• UNIX/Linux + shells.

• Also find, make, grep, ...

• Have their own DSL’s.

• Tektronix Oscilloscopes: C + Smalltalk.

19Thursday, November 20, 2008

Page 20: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

Other Examples

• Adobe Lightroom: C++ + Lua.

• 40-50% written in Lua.

• NRAO Telescopes: C + Python.

• Google Android: Linux+libraries (C) + Java.

20Thursday, November 20, 2008

Lightroom: Lua API used for 3rd-party plugins.Lots of games combine C++ and Lua, too.

Page 21: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

<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??

21Thursday, November 20, 2008

De facto “scripting language” in Java.Not an optimal choice:- All data.- No behavior (to speak of...).- Verbose.

Page 22: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

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

SpringSource just acquired G2One

(Groovy and Grails).Will they switch to

Groovy for configuration?

22Thursday, November 20, 2008

Hopefully, SpringSource will de-emphasize XML and emphasize Groovy for configuration “wiring”.

Page 23: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

/* Prototype JavaScript framework, version 1.6.0.1 * (c) 2005-2007 Sam Stephenson * * Prototype is freely distributable under the terms of an MIT-style license. * For details, see the Prototype web site: http://www.prototypejs.org/ * *--------------------------------------------------------------------------*/

var Prototype = { Version: '1.6.0.1',

Browser: { IE: !!(window.attachEvent && !window.opera), Opera: !!window.opera, WebKit: navigator.userAgent.indexOf('AppleWebKit/') > -1, Gecko: navigator.userAgent.indexOf('Gecko') > -1 && navigator.userAgent.indexOf('KHTML') == -1, MobileSafari: !!navigator.userAgent.match(/Apple.*Mobile.*Safari/) },

BrowserFeatures: { XPath: !!document.evaluate, ElementExtensions: !!window.HTMLElement, SpecificElementExtensions: document.createElement('div').__proto__ && document.createElement('div').__proto__ !==

Property-based Programming

• Excellent for malleable objects.

• See Steve Yegge’s blog

• http://steve-yegge.blogspot.com/2008/10/universal-design-pattern.html

• JavaScript, Lua, Self, ...

23Thursday, November 20, 2008

“Malleable” objects are those whose properties and behaviors may not be so clear cut. They may need to change over the life of the object.

Page 24: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

Multilingual VM’s

• Jython, JRuby, Groovy, Scala.

• On the JVM.

• Ruby on Rails on JRuby (Oracle Mix).

• Dynamic Language Runtime (DLR).

• Ruby, Python, ... on the .NET CLR.

24Thursday, November 20, 2008

Another realization of C+S=A is to put several languages on the same VM, rather than using the OS as the component layer.

Page 25: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

Benefits

• Optimize performance where it matters.

• Optimize productivity, extensibility and agility everywhere else.

Application

Kernel of Components

User Scripts Built-in Scripts

25Thursday, November 20, 2008

This is an underutilized architecture.

Page 26: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

Benefits

An underutilized architecture!

Application

Kernel of Components

User Scripts Built-in Scripts

26Thursday, November 20, 2008

Page 27: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

Parting Thought...

Cell phone makers are drowning in C++.

(Why IPhone and Android are interesting.)

27Thursday, November 20, 2008

Page 28: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

I don’t know what my code is doing.

Specific problem #2

twitter.com/photos/dominic9928Thursday, November 20, 2008

Page 29: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

The intent of our code

is lost in the noise.

29Thursday, November 20, 2008

Page 30: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

Symptoms

•The Business logic doesn’t jump out at me when I read the code.

•The system breaks when we change it.

•Translating requirements to code is error prone.

30Thursday, November 20, 2008

Page 31: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

Solution #1

Write less code.

Profound statement.

31Thursday, November 20, 2008

Page 32: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

Less Code

• Means problems are smaller:

• Maintenance

• Duplication (DRY)

• Testing

• Performance

• etc.

32Thursday, November 20, 2008

Page 33: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

How to WriteLess Code

• Root out duplication.

• Use economical designs.

• Functional vs. Object-Oriented?

• Use economical languages.

33Thursday, November 20, 2008

Page 34: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

Solution #2

Separate implementation details from business logic.

34Thursday, November 20, 2008

Page 35: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

Domain Specific Languages

Make the code read like “structured” domain prose.

35Thursday, November 20, 2008

Page 36: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

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+...

36Thursday, November 20, 2008

Page 37: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

DSL Advantages

• When code looks like domain prose,

• It is easier to understand by everyone,

• It is easier to align with the requirements,

• It is more succinct.

37Thursday, November 20, 2008

Page 38: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

DSL Disadvantages

• DSL’s are hard to design, test and debug.

• Some people are bad API designers,

• They will be even worse DSL designers!

38Thursday, November 20, 2008

Page 39: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

Brueghel the Elder

A DSL Tower of Babel?

39Thursday, November 20, 2008

Page 40: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

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

40Thursday, November 20, 2008

Page 41: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

Parting Thought #2...

Everything should be made as simple as possible, but not simpler.

-- Albert Einstein

41Thursday, November 20, 2008

Page 42: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

Corollary:

Entia non sunt multiplicanda praeter necessitatem.

(Entities must not be multiplied beyond necessity.)

-- Occam’s Razor

42Thursday, November 20, 2008

a.k.a. “Law of Parsimony” or “Law of Succinctness”.

Page 43: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

Corollary:

Entia non sunt multiplicanda praeter necessitatem.

(All other things being equal, the simplest solution is the best.)

-- Occam’s Razor

43Thursday, November 20, 2008

a.k.a. “Law of Parsimony” or “Law of Succinctness”. Paraphrased translation.

Page 44: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

We have code duplication everywhere.

Specific problem #3

twitter.com/photos/maxblack44Thursday, November 20, 2008

Page 45: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

Symptoms

• Persistence logic is embedded in every “domain” class.

• Error handling and logging is inconsistent.

Cross-Cutting Concerns.

45Thursday, November 20, 2008

Page 46: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

SolutionAspect-Oriented Programming

46Thursday, November 20, 2008

Page 47: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

Removing Duplication

• In order, use:

• Object or functional decomposition.

• DSL’s.

• Aspects.

47Thursday, November 20, 2008

Make sure your object and functional decomposition is right first, then use DSL’s appropriately. Finally, use aspects.

Page 48: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

An Example...

48Thursday, November 20, 2008

Page 49: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

class BankAccount attr_reader :balance def credit(amount) @balance += amount end def debit(amount) @balance -= amount end … end

Clean Code

49Thursday, November 20, 2008

Page 50: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

But, real applications need:def BankAccount attr_reader :balance def credit(amount) ... end def debit(amount) ... endend

Transactions

Persistence

Security

50Thursday, November 20, 2008

Page 51: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

def credit(amount) raise “…” if unauthorized() save_balance = @balance begin begin_transaction() @balance += amount persist_balance(@balance) …

So credit becomes…

51Thursday, November 20, 2008

Page 52: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

… rescue => error log(error) @balance = saved_balance ensure end_transaction() endend

52Thursday, November 20, 2008

Page 53: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

We’re mixing multiple domains,

Transactions

Persistence

Security

with fine-grained intersections.

“Problem Domain”

“tangled” code

“scattered” logic53Thursday, November 20, 2008

In principle, I can reason about transactions, etc. in isolation, but in reality, the code for transactions is scattered over the whole system. Similarly, the once-clean domain model code is tangled with code from the other concerns.Objects don’t prevent this problem (in most cases).

Page 54: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

Objects alone don’t prevent tangling.

54Thursday, November 20, 2008

Page 55: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

Aspect-Oriented Programming:

restore modularity for cross-cutting concerns.

55Thursday, November 20, 2008

Page 56: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

Aspects restore modularity byencapsulating the intersections.

Transactions

Persistence

Security

TransactionAspect

PersistenceAspect

SecurityAspect

56Thursday, November 20, 2008

Page 57: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

If you have used Spring, you have probably used

aspects.

57Thursday, November 20, 2008

Page 58: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

• Ruby

• Aquarium

• Facets

• AspectR

Aspect-Oriented Tools

• Java

• AspectJ

• Spring AOP

• JBoss AOP

shameless plug

58Thursday, November 20, 2008

Options for Java and Ruby. Some other languages have AOP toolkits.

Page 59: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

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.

59Thursday, November 20, 2008

Page 60: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

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.

60Thursday, November 20, 2008

Page 61: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

require ‘aquarium’class BankAccount … after :writing => :balance \ do |context, account, *args| persist_balance account end …

reopen class

add new behavior

Aquarium

aquarium.rubyforge.org61Thursday, November 20, 2008

Page 62: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

def credit(amount) @balance += amountend

Back to clean code

62Thursday, November 20, 2008

Page 63: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

Parting Thought...

Metaprogramming can be used for some aspect-like functionality.

DSL’s can solve some CCC.(We’ll come back to that.)

63Thursday, November 20, 2008

Page 64: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

Our application must be available 24 x 7 and highly concurrent.

Specific problem #4

twitter.com/photos/wolfro5464Thursday, November 20, 2008

Page 65: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

Symptoms

• Only one of our developers really knows how to write thread-safe code.

• The system freezes every few weeks or so.

65Thursday, November 20, 2008

Page 66: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

SolutionFunctional Programming

66Thursday, November 20, 2008

(At least, it’s one solution...)

Page 67: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

Functional Programming

• Works like mathematical functions.

Fibonacci Numbers:

F(n) = F(n-1) + F(n-2)where: F(1) = 1 and F(2) = 1

67Thursday, November 20, 2008

Page 68: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

Functional Programming

• Variables are assigned once.

• Functions are side-effect free.

• They don’t alter state.

y = sin(x)

68Thursday, November 20, 2008

Page 69: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

Functional Programming Makes Concurrency Easier

• Nothing to synchronize.

• Hence no locks, semaphores, mutexes...

69Thursday, November 20, 2008

Page 70: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

Account

deposit(...)

withdraw(...)

CheckingAccount

deposit(...)

withdraw(...)

SavingsAccount

deposit(...)

withdraw(...)

??

deposit(...)

withdraw(...)

Which fits your needs?

Object Oriented

70Thursday, November 20, 2008

Page 71: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

list map

fold/

reduce

filter

Which fits your needs?

Functional

71Thursday, November 20, 2008

Page 72: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

twitter.com/photos/deanwampler

What if you’re doingcloud computing?

72Thursday, November 20, 2008

Page 73: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

Declarative rather thanimperative.

F(n) = F(n-1) + F(n-2)where: F(1) = 1 and F(2) = 1

73Thursday, November 20, 2008

I tell the system what I want (e.g., what are the relationships between data, the constraints, etc.) and let the system figure out how to do it.

Page 74: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

… and so are DSL’s.

class Customer < ActiveRecord::Base has_many :accounts

validates_uniqueness_of :name, :on => create, :message => ‘Evil twin!’end

74Thursday, November 20, 2008

By hiding the implementation details, we have much more leeway in implementing aspect behavior, etc.

Page 75: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

A FewFunctional Languages

75Thursday, November 20, 2008

Page 76: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

Haskell

module Main where-- Function f returns the n'th Fibonacci number.-- It uses binary recursion.f n | n <= 2 = 1 | n > 2 = f (n-1) + f (n-2)-- Print the Fibonacci number F(8)main = print(show (f 8))

76Thursday, November 20, 2008

Note how closely the definition reads compared to the mathematical definition I presented earlier.

Page 77: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

Erlang

• Ericsson Functional Language.

• For distributed, reliable, soft real-time, highly concurrent systems.

• Used in telecom switches.

• 9-9’s reliability for AXD301 switch.

77Thursday, November 20, 2008

Page 78: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

Erlang

• No mutable variables and side effects.

• All IPC is optimized message passing.

• Very lightweight and fast processes.

• Lighter than most OS threads.

78Thursday, November 20, 2008

Page 79: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

Scala

• Hybrid: object and functional.

• Targets the JVM.

• Interoperates with Java.

• “Endorsed” by James Gosling at JavaOne.

• Could be a popular replacement for Java.

shameless plug79Thursday, November 20, 2008

I’m co-writing a book on Scala with Alex Payne of Twitter.

Page 80: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

Functional Languages in Production

• Erlang

• Jabber/XMPP server ejabberd.

• Amazon’s Simple DB.

• Yahoo’s del.icio.us.

80Thursday, November 20, 2008

Page 81: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

Functional Languages in Production

• OCaml

• Jane Street Capital

• Scala

• Twitter

81Thursday, November 20, 2008

Page 82: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

Parting Thought...

Is a hybrid object-functional language better than using an object language

with a functional language??

e.g., Scala vs. Java + Erlang??

82Thursday, November 20, 2008

Scala is more complex than “mono-paradigm” languages, so it’s harder to master. However, using multiple languages has it’s own challenges.

Page 83: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

Simplification through Polyglot and Poly-paradigm

Programming (PPP)

Recap:

83Thursday, November 20, 2008

Page 84: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

Disadvantages of PPP

• N tool chains, languages, libraries, “ecosystems”, ...

• Impedance mismatch between tools.

• Different meta-models.

• Overhead of calls between languages.

84Thursday, November 20, 2008

Page 85: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

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”.

85Thursday, November 20, 2008

Page 86: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

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

86Thursday, November 20, 2008

Page 87: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

Why go mainstream now?

• Rapidly increasing pace of development,

➔ Scripting with dynamic languages?

• Pervasive concurrency (e.g., Multicore CPUs)

➔ Functional programming?

• Cross-cutting concerns

➔ Aspect-oriented programming?

87Thursday, November 20, 2008

Page 88: Radical Simplification through Polyglot and Poly-paradigm … › polyglotprogramming › › papers › ... · Radical Simplification through Polyglot and Poly-paradigm Programming

Common Threads

• Less code is more.

• Keep the code close to the domain: DSL’s.

• Be declarative rather than imperative.

• Minimize side effects and mutable data.

88Thursday, November 20, 2008