The Case for Dynamic Languages
Sam Ruby
Case for Dynamic Languages
a.k.a.
Reinventing Smalltalk,
one decade at a time
Preface
What do these have in common?
- BitTorrent
- Slashdot
- Yahoo!
Hints
- Michael Tiemann “G++ 40% faster than…”
- Bud Siddhisena “less than 8% degredation…”
- Anuradha Ratnaweera “shaving seconds off of boot times”
Preface
So… what do these have in common?
- BitTorrent
- Slashdot
Yahoo!
Answer:
- Each are synonymous with scalability
- Each are implemented in scripting languages
- Macro-performance vs Micro-performance
- Modest hardware running Perl can easily saturate your bandwidth
Comparison
Printing out all arguments in uppercase in a few languages:
- Perl
print join(' ',map {uc} @ARGV) . "\n";
- Python
print ' '.join(map(string.upper,sys.argv[1:]))
- Ruby
puts ARGV.map{|arg| arg.upcase}.join(' ')
To minimize distraction: I’m going to focus primarily on one (Ruby)
Part I
Personal retrospective
BASIC
10 print "Enter your name:"
20 input a$
30 print "Hello, "; a$; "!"
1980's
- Programming
- JOVIAL
- 370 Assembler
- Ada
- Pascal
- Scripting
1990's
- Programming
- Scripting
- Shell Scripts
- Batch files
- REXX
Other activities
- Bean Scripting Framework
- ECMAScript (JavaScript) Editor
- ECMA CLI working group Conveener
- C# working group member
- PHP 4.0 + Java
- Python on Parrot
2010's?
- Anybody’s guess
- Languages come and go
- It is getting harder to differentiate “real” and “scripting” languages
Part II
Meanwhile…
C derived languages
- 1970’s: (C) Ken Thompson and Dennis Ritchie
- 1980’s: (C++) Bjarne Stroustrup
- 1990’s: (Java) James Gosling
- 2000’s: (C#) Anders Hejlsberg
SmallTalk
- 1970’s
- Alan Kay, Dan Ingalls, Ted Kaehler, Adele Goldberg
- Smalltalk-80
- Extreme Programming
Smalltalk
- Everything is an object
- type-checking is dynamic
- message-sending
- garbage collection
- everything is modifyable
- bytecodes
- MVC
- JITs (80’s)
Source: wikipedia
Everything is an object
Integer.parseInt(“5”)
“5”.to_i
Math.abs(-5)
-5.abs
type-checking is dynamic
def dump(results)
results << "abc"
results << "xyz"
end
Examples of classes that implement ”<<” :
message-sending
Facilities enabled by being able to capture messages:
- Remote Proxies
- Auto Loaders
- Decorators
- Mock Objects
- Builders
everything is modifyable
Facilities enabled by being able to modify existing classes:
- Bug fixes
- Features
- Application Specific logic on generic (e.g. DOM) classes
MVC(Model View Controller)
Ruby on Rails
Part V
Blocks and Continuations
Block Example
sorted_employees =
employees.sort_by {|e| e.lastname}
Block Usages
- Iteration
- Sort and comparison
- Ensuring post process
- Conditionals
- Callbacks
- Enumerable
- Enumerator
Ensuring post process
Scanner sc = new Scanner(new File(args[0]));
try {
while (sc.hasNextLine()) {
System.out.println(sc.nextLine());
}
} finally {
sc.close();
}
open(ARGV[0]) do |f|
f.readlines.each {|line| puts line}
end
Closure Example
def high_paid_employees(employees, limit)
employees.collect{|e| e.salary > limit}
end
Part III
Incredible shrinking code
Hello World!
class Greeting {
public static void main(String args[]) {
System.out.println(“Hello World!”);
}
}
Kernel::puts(“Hello World!”);
Hello World!
class Greeting {
public static void main(String args[]) {
System.out.println(“Hello World!”);
}
}
puts(“Hello World!”);
Hello World!
class Greeting {
public static void main(String args[]) {
System.out.println(“Hello World!”);
}
}
puts(“Hello World!”)
Hello World!
class Greeting {
public static void main(String args[]) {
System.out.println(“Hello World!”);
}
}
puts “Hello World!”
Beans
class Point {
private int x;
public int getX() {
return this.x;
}
public void setX(int x) {
this.x = x;
}
}
Beans
class Point
def x()
return @x
end
def x=(x)
@x=x
end
end
Beans
class Point {
private float x;
public float getX() { return this.x; }
public void setX(float x) { this.x = x; }
}
class Point
def x() @x end
def x=(x) @x=x end
end
Beans
class Point {
private float x;
public float getX() { return this.x; }
public void setX(float x) { this.x = x; }
}
class Point
attr_accessor :x
end
Beans
class Point {
private float x,y;
public float getX() { return this.x; }
public void setX(float x) { this.x = x; }
public float getY() { return this.y; }
public void setY(float y) { this.y = y; }
}
class Point
attr_accessor :x, :y
end
Rails
class Entry < ActiveRecord::Base
acts_as_tree :order => “updated”
validates_presence_of :updated
end
Exceptions
Scanner sc = new Scanner(new File(args[0]));
while (sc.hasNextLine()) {
System.out.println(sc.nextLine());
}
sc.close();
Exceptions
Scanner sc = new Scanner(new File(args[0]));
try {
while (sc.hasNextLine()) {
System.out.println(sc.nextLine());
}
} finally {
sc.close();
}
Exceptions
f = open(ARGV[0])
begin
for line in f.readlines
puts line
end
ensure
f.close
end
f.close();
Exceptions
open(ARGV[0]) do |f|
f.readlines.each {|line| puts line}
end
Summary
Dynamic languages
- Focus on the task at hand
- Executable pseudocode
- Enable domain specific grammars
- IDEs available, not required
- Eclipse plug-ins
- ActiveState
- FreeRide