Aside: Dynamic programming language on web development

  • What are dynamic programming languages, e.g., Ruby, Python?
  • How are they different from traditional programming languages, e.g., Java?
  •  Why are they more productive in web development?

This topic is one of my focus area several months ago when I was building a website. Also this serves as my assignment for the CSIS7306.

I will discuss

  • the definition of dynamic programming languages
  • difference from traditional programming language,
  • why are they more productive in web development
  • comparing some popular languages being used and their development to illustrate

What is Dynamic language

In  Using JavaScript as a Real Programming Language by Tommi Mikkonen and Antero Taivalsaari from Sun Labs 2007, the same problem has been studied with focus on improving javascript.

There suggested the following definition for dynamic language.

Dynamic language is a class of programming languages that share a number of common runtime characteristics that are available in static languages only during compilation, if at all. These behaviors can include the ability to extend the currently running program by loading or generating new code by extending the classes, objects and other definitions of the program, or even by modifying the internals of the language itself, all during program execution. While these behaviors can be emulated in almost any language of sufficient complexity, such behaviors are integral, built-in characteristics of dynamic languages.

The work suggested three main characteristics of dynamic language: Generic, Dynamic typing, Interpretation.

Regarding the characteristics  of dynamic languages comparing to traditional programming languages, I will illustrate by some examples and my experience.

There are numerous languages that are being considered as dynamic. JavaScript, Perl, PHP, Python, Ruby, Groovy, Scala…

Some features that are common but not applicable for all of them.

Features

Metaprogramming

According to wikipedia,

Metaprogramming is writing of programs that write or manipulate other programs (or themselves) as their data or that do part of the work that is otherwise done at run time during compile time.

As in Grooy, it is easy to change the class behavior. Methods can be manipulating at run-time.

For example, this snippets from the official website suggest ways to add a method to the class Book in run-time.

class Book { String title }

Book.metaClass.titleInUpperCase << {-> title.toUpperCase() }

def b = new Book(title:”The Stand”)

Besides, class names, existing methods for String.. can all be configured at run-time. It is also possible to add new classes / scripts.

This actually allow abstract configures from implementation, achieving part of what heavy-weight framework for dependency injection or Aspect-oriented programming frameworks such as Spring do.

It is also powerful when combined with those frameworks.

As in one of my project, I also managed to put groovy script in database and distribute them into target machine and run, with help of “refreshable bean” concept in Spring and activeMq.

This is inspired from the article Groovier Spring, Part 2 

This is available in Ruby, Python, Perl However, not  javascript in its narrow definition. List here

Dynamic typed

A programming language is said to be dynamically typed when the majority of its type checking is performed at run-time as opposed to at compile-time. [wikipedia]

dynamic typing allows duck typing, one is concerned with just those aspects of an object that are used, rather than with the type of the object itself. With this code are much more reusable.

Again not a must, but powerful in many context. There has been a long debate for static typed language vs dynamic typed language. Example like this stackoverflow post

In the context of web development, I will say dynamic typed language greatly speed up development as it evolve with changing implementation. Codes are also cleaner.

Also, for the benefits from static type check, for most dynamic typed language it is also possible to state explicit type checking when necessary.

Closure

Closure is another thing that greatly increase the reusability of code. As a major tool of functional programming, it improves the flexibility of the language and also it keep the global scope tidy. I wont cover much of this here since it is much more on experience and style. JAVA plans to have it only at Java 8, where that may not be a true one.

Dynamic language is better for web Development

Actually nowadays you can hardly find a website without Dynamic language. Most dynamic languages in this context serves as the loose defined scripting language

Client-side programming

This is the most important thing talking about dynamic language in websites.. It is not about to use dynamic language or not, but just where and how much.

Dynamic language is easier to model dynamic website behaviours.

AJAX technologies is mature which handle request and provide dynamic response according to different user behaviours. This asynchrous client side programming is hard to achieve by traditional programming rely on server side. It is possible to use Comet.

The example of JSP, requires different tags and library to do integration for back end and UI, which is overcomplicated and less flexible in my opinion.

Also, closure and help in guarding the scope of variables for the state while providing different features.

It is possible to render different script to client according to different situation. The script client requires can be greatly reduced.

Faster development cycle

This increases developer productivity by reducing scaffolding code when developing web, GUI, database or console applications

As mentioned in previous part, dynamic language is more flexible and cleaner to understand. For web developemnt that need to adapt to rapid changing requirement,  reducing development time will be crucial

This is particular important to handle the front end UI.

No redeployment

Firstly redeployment is not required to add new functionalities.

For traditional ways, like JAVA with tomcat will requires redeployment to compile new code. Although it is just about the startup, it is not suitable for prototyping website as mentioned above, also this will affect the site beahviour and impact the stability. It is required another server for flip-flop to keep the site running.

Same data structure from UI to back end

Although this can also be achieve with JAVA EE and JSP, that is not as seamless. As mentioned most UI used dynamic language like javascript for dynamic behaviour, lots of paring is required to do the end to end integration. For example, front end send back a JSON object, which will require to parse by the JAVA servlet to Java Objects with mapping. There exists lots of libraries like GSON to help, but still manual work has to be done on customizing different types of complex objects as it is not dynamic typed. Error due to Reflection lack of public constructors are not uncommon.

There is another step to convert those objects back the the back-end database. Hibernate is there but still that is one another configuration.

I will argue that with dynamic language in both front end and back end, it is possible to use the same data structure. There are Document-oriented database such as CouchDB, which use JSON for storage. This provides mechanism for end to end using JSON, simple but powerful. From experience lots of error can be reduced in parsing code.

Performance?

Regarding the production performance, many argue that dynamic language is less productive, as there are extra checking to be done in run-time. However for the discussion it is important to note  Hiphop for PHP and Google V8 Engine, examples of write in dynamic language, where traditional language allow them run faster.

Current Developement

I will talk about some languages, framework and their development.

1) Javascript and its development

Javascript can definitely speaks for the place of dynamic language in web development. Contradict to what most people thought, Javascript is not a toy language that only capable for showing consecutive alerts of “Hello”, and some small icons flying around your cursor, as what is popular 15 years ago. In recent years there has been a great development over javascript.

Again in the work by Tommi et al here is the recap of javascript:

JavaScript is a typical dynamic language in the sense that variables in JavaScript do not need to be introduced before their use and that the types of variables are resolved dynamically during execution. JavaScript allows function definitions and other code to be modified while the program is running. The execution model of JavaScript is based on interpretation of source code. In contrast with less dynamic languages such as the Java programming language, there is no public intermediate representation format such as class files or binary files. In summary, JavaScript manifests all the three key characteristics of dynamic languages listed in Section 2.1.

Java script is dynamic-typed and it is object -oriented,  to be exact, following a prototype pattern. A long but awesome discussion on this can be find here

I would say it is some concepts is more complicated than traditional language but using it is handy and flexible. 

It is true that there are many design flaws in the language itself. Some weired behaviour documented in the Javascript garden. However, Javascript still stay as a simple,powerful language with the its good parts. This single book is the bible of writing awesome javascript.

Recent development extend Javascript in server-side programming (Node.js) or other areas outside browser (CommonJS), better standard to modularize the code to become reusable like Asynchronous Module Definition, with example as (RequireJS).  Lots of libraries help to achieve more with less code and address browser compatibility issues like JQuery.

Yes, I agree that there should be a domain name .js for these sites.

Unlike mysterious error you will see in IE6, debugging and development is now much easier with Web Developer tools in Firefox or Chrome, which even support hot code replace.

2) GRails and Play! framework

Both inspird by Ruby on rails, the two frameworks are very good examples about integrating dynamic language with traditional language in web development.

With increasing complexity in web development which is now also more mature, I will suggest framework is of crucial  importance. For past web MVC frameworks, it has been shown that J2EE EJB has its design flaws, while spring web MVC might be too heavy-weighted.

These frameworks adapt a “convention over configuration” approach, which help developers to build a easy-to-maintain application in short time.

It has to be admitted that not only dynamic language, but many other components and ideas such as RESTful, Behaviour driven development, LESS CSS, together with community support including Heroku and github, consituted such great frameworks. However the language itself is crucial for linking everything together and make it usable for developers

Developers of those framework do a great job that Java developers can use their familar language while providing dynamic behaviour support.

Grails use groovy, which can be translate to bytecode running in JVM. This allows extending legacy Java system with dynamic behaviour in an easy manner.

Play! framework support automatic source loading and use Comet Socket and WebSockets to enhance dynamic behaviour compared to traditional Java. It also use Scala as the template engine, which integrate easily with Java objects in the view.

Conclusion

This has been a long post, but for sure there are even more discussion that can be going on as this is a really big topic. The simple conclusion is that it is not about using dynamic language or not, but how much it is going to replace in the traditional way of web development.

Advertisements

2 thoughts on “Aside: Dynamic programming language on web development

  1. To compensate my lack of contribution last week, I would like to add a few points and to clarify some facts. This piece is long and thought provoking.

    First off, to clarify, closure is not a dynamic programming feature. Closure is actually have nothing to do with dynamic or static language, but it is much related to imperative language or multi-paradigm language, most importantly supporting first-class functions. In another words, if you can use function and data interchangeably, you are already supporting closure. For example, C#[1]. Even “traditional” imperative programming language like C can mimic such behavior[2] via function pointer. Of course, the holy grail of programming language among the elites, Haskell, is the epitome of static typing language supporting closure.

    Secondly, Nowadays web development seems to be a noun that have a very broad meaning. Web development can include from the simplest static web pages to a meaningless group buying site to the most complex software ever built with Hadoop, NoSQL DBMS, ….(added your technology stack here). So the discussion should be more pinpoint on what scale of web application you are building, is it an experimental, proof of concept web application, or you are building the next Google / Baidu?

    Also, in the conclusion, traditional is also an ill-defined adjective. For me, I consider traditional web development as PHP + MYSQL + JS, so I can only argue that the tool set for dynamic language, instead of dynamic languages themselves, is rapidly advancing the state of the art for web development.

    So, I hope you will clarify this in the later draft. For the following arguments, I will suppose you are building a fairly large web application.

    One of the main issue of this article is that there are only one-sided arguments for the good side of dynamic languages but totally omit the dark side of them.

    Dynamic language, to start with, is hard to debug and refactor, it relies on search and replace tools to do simple renaming. And the worse part is that you have to run all your tests, unit or integrated, once more to ensure you have not accidentally rename something unrelated. In static typing, this is trivial via IDE of the related language. Changes of data structure is also easier as the compiler with pick up the errors in case you miss to define / declare some of the variable.

    Another major advantage of static typing is the languages itself also check the correctness of your program, which in dynamic language is abysmal. The “1” + 1 mess in Javascript and PHP is making bug tracing a nightmare for sufficiently large program built by average Gilbert. One can argue that this advantage is based on the completeness of type system in the language itself, but most of the time, have is better than have not. And I am rather pay the price of Integer.parseInt(x) than spend the time testing the unpredictable type juggling.

    Dynamic language do allows client-side development. Dynamic language is more abstracted and higher level. Hence developers can do more things in one line of code. But AJAX technology is the way to build large application as Javascript is not suitable for large application[3], despite some of the gurus suggesting patterns.[4]

    And from my experience, expressiveness is a powerful tool. So powerful that only the elites can make sense of it. For 5 years of my journey in software development, I yet to see a functional, expressive macro written by myself or my fellow classmates, though macro is the most abusive feature in dynamic language.

    All in all, I am not convinced to build large system based on dynamic language. There are practical reasons and philosophical reason.

    P.S For the “convention over configuration” part, after playing through Mass Effect, I am very skeptical in such practices, but maybe next article.

    References:
    [1] http://www.codethinked.com/c-closures-explained
    [2] http://porkrind.org/missives/closures-in-straight-c/
    [3] http://blogs.adobe.com/bparadie/2012/05/07/the-pew-pew-manifesto/
    [4] http://addyosmani.com/largescalejavascript/

    • nice shot. a quick reply-this article is baised as in the question of the assignment lol. ‘traditional’ is also a really loose term, which origin from the question mostly refer to java and c family. will surely love to add more pts later. articles you quoted also sound interesting

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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