InfoQ

News

Try to get the best of your Statically Typed Language

Posted by Sadek Drobi on Apr 11, 2008

Community
Architecture
Topics
Design ,
Programming
Tags
Dependency Injection ,
Frameworks ,
Guice ,
Static Analysis ,
Spring

Echoing at a discussion on dynamic vs. static languages, Debasish Ghosh raises the issue of using dynamic type-checking while programming with static languages. He recalls the common generalization of Greenspun's 10th Law: "any sufficiently complicated program in a statically-typechecked language contains an ad-hoc, informally-specified bug-ridden slow implementation of a dynamically-checked language."  

Ghosh believes that this is not necessarily so today. He argues that Java generics, e.g. Guice and EasyMock, allow avoiding workarounds necessary to enforce runtime type checking: 

Using Java generics, these frameworks allow compile time type checking for cases which would earlier have to be implemented using a slow and bug ridden simulation of runtime type checking. Guice and EasyMock stand out as two frameworks I have been using that have used the power of generics to implement extraordinary typesafety. […] 

Have a look at this piece of code, which binds an implementation

SpecialServiceImpl to the interface Service using Guice Binder

public class MyModule implements Module {
public void configure(Binder binder) {
        binder.bind(Service.class)
                     .to(SpecialServiceImpl.class)
                     .in(Scopes.SINGLETON);
}
}”

Even though “it may seem that the "implements" relationship between Service and SpecialServiceImpl is done during runtime”, all type checking is actually done during compile time:

A peek at the source code of Guice reveals that BinderImpl.bind() returns BindingBuilderImpl .. 

public BindingBuilderImpl bind(Class clazz) {
return bind(Key.get(clazz));

and BindingBuilderImpl.to() takes as input Class - the bound on the wild card enforces the above "implements" relationship as part of compile time type checking of the arguments .. 

public ScopedBindingBuilder to(Classextends T> implementation) {
return to(TypeLiteral.get(implementation));
}

Debasish Ghosh advocates for using this kind of solutions rather than trying to achieve dynamic type checking. Not only does it allow avoiding the Greenspun's10th Law but it optimizes the advantages of static typing because it guarantees strong type-safety: 

When you are programming in a statically typed language, use appropriate language features to make most of your type checking at compile time. This way, before you hit the run button, you can be assured that your code is well-formed within the bounds of the type system. And you have the power of easier refactoring and cleaner evolution of your codebase.

No comments

Watch Thread Reply

Educational Content

Rails in the Large: How Agility Allows Us to Build One Of the World's Biggest Rails Apps

Neal Ford shows what ThoughtWorks learned from scaling Rails development: infrastructure, testing, messaging, optimization, performance.

Stuart Halloway on Clojure and Functional Programming

Stuart Halloway discusses Clojure and functional programing on the JVM in depth, and touches on the uses of a number of other modern JVM languages including JRuby, Groovy, Scala and Haskell.

Orion Henry and Blake Mizerany on Heroku

Orion Henry and Blake Mizerany talk about the technology behind Heroku and the benefits of the new add-on system.

Security for the Services World

Chris Riley presents security issues threatening service based systems, examining security threats, presenting measures to reduce the risks, and mentioning available security frameworks.

Navigating The Rapids:Real-World Lessons in Adopting Agile

This talk investigates technical issues encountered when moving to an Agile process.

Codename "M": Language, Data, and Modeling, Oh My!

Don Box and Amanda Laucher present “M”, a declarative language for building data models, domain models or external DSLs. Don Box's demos show some of M’s features and latest changes of the language.

SOA Manifesto - 4 Months After

It is four months since the SOA manifesto was announced; InfoQ interviewed the original author’s to get insight into the motivations and the process behind the initiative.

Memory Barriers and JVM Concurrency

This article explains the impact memory barriers, or fences, have on the determinism of multi-threaded programs.