the blog for developers

Weak versus strong languages, who wins the fight?

With all the buzz in scripting languages (again :-) often programming languages are divided into weak versus strong typed or dynamic versus static typed languages. I don’t know how often some of us have seen this discussion, the first time for me was when I had to argue with C++ developers because I wanted to use Python (and then did) for web/database development nearly ten years ago.

Static versus dynamic

What does weak versus strong mean? This is based on the type system of the programming language. Languages where the type of references can change are usually called dynamic (weak), languages where the type of a reference is fixed are called static (strong). Take these two examples:

   String name = "stephan";

and

   def name = "stephan"

The first one is in Java and the second one is in Groovy. In the Java example a reference (variable) name is created. Then a data structure which contains stephan is created and assigned to the reference.

StaticReference
In the second example the same is done in Groovy. Where is the difference? Consider this:

   String name = "stephan";
   ...
   name = new Name("stephan");

and

   def name = "stephan"
   ...
   name = new Name("stephan")

The first example doesn’t work, but the second does. This is because Java is statically typed which means the reference is statically typed. A reference possesses a type, in this case String. A reference with type String can only point to data structures which contain data of type String. In Groovy the reference is dynamically typed. So the type of the reference can change from String to Name when the data which it points to changes.

Another example in Groovy:

   String name = "stephan"

Hey, this is Java right? Didn’t I say, Groovy is dynamic not static typed? Yes, you’re right. But Groovy is more than this. Groovy is both dynamic and static. So the developer can choose a reference to be dynamic or static, just as needed.

Although categorizing languages into dynamic versus static (which the dynamic camp uses) describes languages better than weak versus strong (which the static camp uses), those categorizations are too simple to be useful, as seen already in the case of Groovy.

And there are at least two dimensions for typing, reference typing and data structure typing. In programming languages both references and data structures can be typed. This matrix divides languages between dynamic and static typed languages for references and for data structures.

DynVsStatTyped

Ruby, Python, Smalltalk and Java have all statically typed data structures (you could argue Ruby has dynamically typed data structures). But Ruby and Python use dynamic references while Java uses static references.

In the bottom right, there is C which has typed references but untyped data structures. And C doesn’t enforce that typed references point to the correct data structures (because those are not typed). C data structures can be typed by adding a type field to the structure and checking the type before any operations on the data structure. Some C developers did this for more reference safety.

Other languages are even more free. Javascript for example uses prototyping for object creation, not classes in the traditional way. Maps and objects do not differ in Javascript and are essentially the same. Javascript does not know methods but executable attributes are used as methods. Libraries like Prototype or Base extend Javascript with an inheritance based class system.

Some languages can change the data structures although the data structures are typed. In languages with Meta support (Ruby, Groovy, …) data structures can be changed, fields and methods can be added at runtime.

Scripting languages

I think there is more to this than the simple 2×2 matrix from above. Sometimes people use the terms dynamic and static, but mean scripting and compiled. What is scripting? Traditionally scripting languages were interpreted languages. In web devlopment scripting languages like Ruby and PHP make development easier. No compilation is needed and after changing your programm you can hit reload in the browser and see the new result. This way turn around times are minimized and development is speed up. But the line gets blurred. Scripting languages get virtual machines and are compiled to byte code for better performance. Java compiles to byte code which is interpreted. New Java VMs then don’t interpret all byte code but compile the byte code to native code on the fly and as needed. Also compiled languages like Java get new classloaders and containers which compile new source code to byte code on the fly. And VMs like the Java VM run new languages like Groovy which can be compiled on the fly. For example in the excellent Grails framework.

Functional languages

Some people use dynamic versus static and mean object oriented versus functional. Consider these two examples (taken from Cedric Beust):

List result = new ArrayList();
File f = new File(directory);
for (String fileName : f.list()) {
  if (fileName.endsWith(".gz")) {
    result.add(fileName);
  }
}
Collections.sort(result, new Comparator() {
  public int compare(String o1, String o2) {
    return o2.compareTo(o1);
  }
  public boolean equals(Object o) {
    return super.equals(o);
  }
});

and

zippedFiles = Dir.new(dir).
  entries.sort.reverse.delete_if { |x| ! (x =~ /gz$/) }

Both code fragments do the same thing which is sorting all zipped files in a directory.
The first is Java style object oriented (though this could be written shorter) and the second
is Ruby functional style. As can be seen, the second is easier to write, understand and
maintain. Especially in applications which have to deal with lots of lists functional programming makes code considerably shorter. Web applications often use lists (customers, projects, todo items) and so profit from languages which support functional style programming.

Conclusion

The whole discussion about dynamic and static languages is senseless. There are more dimensions which characterize a language than dynamic versus static. Scripting/compiled and functional/OO are other important things to consider. Languages like Groovy support object oriented and functional programming styles, can be used as a scripting language and compiled to byte code, support both dynamic and static typing of references and with meta programming capabilites support modifying static typed data structures.

I think there is still more to say about dynamic versus static. But this post needs to end here.

Who wins the fight? The is no fight, know your tools and use the best tool for the job. But at least the static/dynamic dimension is less usefull than it seams at first.

You can leave a Reply here. Of course, you should follow me on twitter here.

You can share this post!
Do you want to tell others about this article? Use the social bookmark icons to submit this artice to the service of your choice. Thanks.

About the author: Stephan Schmidt is head of development at brands4friends. He has more than 15 years of internet technology experience and 10 years experience in agile. He was head of development, consultant and CTO and is a speaker, author and blog writer. He specializes in organizing and optimizing software development helping companies by increasing productivity with lean software development and agile methodologies. Want to know more? All views are only his own.
Leave a reply.

Comments

Isaac Gouy

Just follow textbook definitions and avoid the confusion: type-safety.

‘So what is “strong typing”? This appears to be a meaningless phrase, and people often use it in a non-sensical fashion. To some it seems to mean “The language has a type checker”. To others it means “The language is sound” (that is, the type checker and run-time system are related). To most, it seems to just mean, “A language like Pascal, C or Java, related in a way I can’t quite make precise”. If someone uses this phrase, be sure to ask them to define it for you. (For amusement, watch them squirm.)

p263 “Programming Languages: Application and Interpretation”
http://www.cs.brown.edu/~sk/Publications/Books/ProgLangs/

stephan

Well, as I tried to make clear, the dynamic versus static discussion is not only about type-safety.

“If someone uses this phrase, be sure to ask them to define it for you. (For amusement, watch them squirm.)”

Yes that usually happens. Then ask them, if “type inference” is “strong typing”.

Nice post! Very good explanations of concepts that not all developers know. It’s also helpful to understand the various things that the word “dynamic” can mean.

Isaac Gouy

“the type of references can change” “the type of a reference is fixed”
This isn’t a useful way to categorize programming languages.
There is a universal type and all other types are a subtype of the universal type, so it’s both true that “the type of a reference is fixed” (to the universal type) and the subtype of references can change.

If we want to discuss “dynamic versus static” we need to be clear if we mean dynamic type checking versus static type checking; or do we mean we mean we can execute code generated on-the-fly at runtime; or do we mean…

stephan

@Isaac: I think I clearly wrote what I meant with dynamic vs. static, which is


String name = "stephan"

vs.


def name = "stephan"
...
name = new Name("stephan")

which doesn’t work if the reference is static.

You’re right that we could also distinguish between dyn and stat for compile time and run time, but I consider this not very useful, especially as Java for example does checking at compile and at runtime as do most other “static” languages.

Isaac Gouy

“doesn’t work if the reference is static”
Really?
Object name = “stephen”; // universal type
name = new Name(forename: “stephan”);

“checking at compile and at runtime”
The distinction is between languages that /do not/ provide static type checking, and languages that /do/ provide static type checking. Languages that /do/ provide static type checking can still defer type checking until runtime.

The distinction is between languages that /do not/ provide early binding, and languages that /do/ provide early binding. Languages that /do/ provide early binding can still defer binding until runtime.

“reference typing and data structure typing”
Talking about data structure typing like this seems likely to cause confusion between nominal typing and structural typing.

“Ruby, Python, Smalltalk and Java have all statically typed data structures”
You can of course invent your own definitions but no one else talks about Ruby, Python, or Smalltalk runtime data structures as being statically typed.

+á+Ã+áòÀÚ+à+Õ+á+Â+à+*+à+Å +à +à+

Hi Stephan, congratulations for you post.
I am very interested to read the reference documentation that you used to write this post. Can you send me link’s?

Thank’s

Sergio Junior

PS: I’m brazilian, and my english is terrible =). Forgive-me.

Leave a Reply

What people wrote somewhere else:

Additional comments powered by BackType

Guide to CodeMonkeyism

Over the last 4 years I wrote many articles on this blog. To make it easier for you to find the relevant ones, I've organized them into topics.

Top 10

6 reasons why my VC funded startup did fail

Go Ahead: Next Generation Java Programming Style

Java Interview questions: Write a String Reverser

The dark side of NoSQL

7 Bad Signs not to Work for a Software Company or Startup

Is Java dead?

Scala vs. Clojure

Never, never, never use String in Java

No future for functional programming in 2008 – Scala, F# and Nu

Clojure vs Scala, Part 2

Java Developer

Is Java Dead?

Go Ahead: Next Generation Java Programming Style

Be careful with magical code

All variables in Java must be final

Never, never, never use String in Java

Bending Java: More readable code with methods that do nothing?

NoSQL Guy

NoSQL: The Dawn of Polyglot Persistence

The dark side of NoSQL

Essential storage tradeoff: Simple Reads vs. Simple Writes

Sharding destroys the goals of your relational database

The unholy legacy of databases

Startup/CTO

Development Dream Teams

6 reasons why my VC funded startup did fail

American vs. European style of Software Development

12 Things to Reduce Your Lead Time and Time to Market

The high cost of overhead when working in parallel

Essential storage tradeoff: Simple Reads vs. Simple Writes

Job Seeker

Another Good (Java) Interview Question

7 Bad Signs not to Work for a Software Company or Startup

Java Interview questions: Write a String Reverser (and use Recursion!)

Java Interview questions: Multiple Inheritance

As a Manager: What I value in developers

Top 10 Tips (+1) to Get a Pay Raise

Agilist

What Developers Need to Know About Agile

5 Practices Better to Change in Your Scrum Implementation

Scrum is not about engineering practices

ScrumMaster and ZenMaster: The joke of certification

What is Trans-Scrum?