Rockstar Engineering

Interfaces in Dynamic Languages

Posted in Uncategorized by Jose Asuncion on August 3, 2011

I have been introducing a lot of interfaces in our code base at work. At one point, this has led me to think if they are really useful.

Back when I was programming in Java, I used interfaces to aid in “decoupling” my code. By decouple I mean separating concerns — business logic shouldn’t be mixed with crud logic. The latter should delegated to another object where the appropriate method is called. Working with PHP, however, I realized that this can be done by simply creating separate classes for business logic and crud operations. No need for interfaces. It will work just fine.

So the only need I had left for interfaces was for constructing mock objects for my unit tests — specifically so that I wouldn’t be dependent on a database to unit test my service layers. I can just write a mock dao that returns mock data and then inject it wherever it is needed.

Yes mocks are needed for unit tests but was implementing the interface necessary? The code works just as well without having it implemented.

I have found though that one good argument for interfaces is to check if an object supports a set of methods. This is especially crucial if one expects an object to be able to support a large set of methods. An object injected into a dependent object that doesn’t implement the interface required is automatically rejected even before it is used a.k.a at compile time. In dynamic languages, I reckon this rejection happens at run time.

For a large team of developers who don’t know each other, interfaces are a good mechanism to formalize functional requirements. But I work in a three person team where I am the only one working on my module. The only reason I can think of right now to use interfaces is for the benefit of the developer after me who will be maintaining the code I write.

The question now is, are interfaces any useful in a dynamic language given that type checking isn’t done till runtime? What other value does that give you?

About these ads
Tagged with:

3 Responses

Subscribe to comments with RSS.

  1. Arnaud said, on March 3, 2012 at 12:43 pm

    I use “pseudo interface” in Javascript, it’s useful for 2 reasons:
    1) it documents which methods are expected (for someone who reads the code later)
    2) it allows easy assertion for _early_ detection of wrong parameter


    /** Pseudo-intf, never created. You don’t even need to inherit from it formally. */
    function Writer() { }

    Writer.prototype.print= function(str) {

    throw “Not implemented”;

    function Log(Writer output) {

    checkIntf(output, Writer); // <= check immediately that 'output' provides all methods of Writer pseudo-intf, to avoid later failure in log()

    this.mOutput = output;


    Log.prototype.log = function(msg) {

    // we are now sure that mOutput has all required methods
    this.mOutput.print("– " + msg);

    • Jose Asuncion said, on March 4, 2012 at 12:43 am

      Is checkIntF(…) a built in method in javascript?

      • Arnaud said, on March 4, 2012 at 8:55 am

        No, it’s not built in.
        I just use something like that:

        function checkIntf(obj, clazz) {

        if (obj == null)
        return; // ok, consider the parameter as “nullable”

        for (var prop in clazz.prototype) {

        if (!(prop in obj))
        throw “Wrong type”;

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: