• 2012-07-29

    Make dynamic programming languages more dynamic

    Views: 4553 | 2 Comments

    The definition of “Dynamic Programming Language” must be redefined!

    Strong typing if not dynamic

    According to the current definition of “Dynamic Programming Language“, a language is called dynamic regardless if it is weak typing or not. But I think strong typing languages such as Python are not dynamic languages.

    Consider what is the result of

    1 + "2" = ?
    

    Let’s see how mainstream programming languages treat this expression:

    Language Result
    Java compile error
    Python runtime exception
    JavaScript “12”(string concat)
    PHP 3(integer addition)

    Most Python learners counldn’t understand why Python throw exception on such expression, why a “Dynamic Language” do so? PHP uses the dot mark “.” to concat strings, perfectly avoid the ambiguity of “+” operator in other language, and automicly convert string literal integer into int as people expected.

    Be tolerant to null

    Most programming languages terminate the execution when encounter statements as follows:

    Object a = null;
    Object v = a.b.c; // ?
    
    Language Result
    Java compile error
    Python runtime error
    JavaScript runtime error
    PHP v = null

    PHP works fine as human expected(with ~E_NOTICE). Why should it terminates at getting null object’s properties? To make the code after keep running, programmers have to do lots of null checks, while all these checks can be eliminated.

    This is how JavaScript does:

    a = null;
    if(a != undefined && a.b != undefined && a.b.c != undefined){
    	v = a.b.c;
    }
    

    Programmers should write codes to take action only if it is in the expected condition, and ignore all unexpected conditions. A dynamic language runtime should not do force programmer to do uneccessary works.

    Codes like

    if(a.b.c == 1){
    	// take action
    }
    

    is always prefered than

    if(a != undefined && a.b != undefined && a.b.c != undefined){
    	if(a.b.c == 1){
    		// take action
    	}
    }
    
    Posted by ideawu at 2012-07-29 15:05:27
|<<<1>>>| 1/1 Pages, 1 Results.