web developer & system programmer

coder . cl

ramblings and thoughts on programming...

typing is not a problem

published: 10-06-2012 / updated: 12-06-2012
posted in: development, haskell, java, programming, python, rants, tips
by Daniel Molina Wegener

Typing is not a problem. We have many programming languages which are using dynamic typing. Dynamic typing is not so good as you can think. It allows you to do much stuff, where you can evaluate some expressions like (int)"1" + 1 = 2, without type errors. The problem with that kind of flexibility is the fact that there is no standard on boolean evaluations, leading to logic errors, and obviously to program failures. Even in some type-safe programming languages, they allow some stuff like that, there is C++98 and further versions allowing a wide variety of type casts, where any mistake on the casted values can generate logic errors and failures.

I prefer to see static typing and strong-static typing as a good feature, mainly strong-static typing. It avoids many programming errors. Please imagine a common example. We have a legacy system made on top of PHP, and we are migrating that system to Python, or even Node, you can find logic errors during the migration, so it is not so easy as you are thinking. The application which is subject of the migration is consuming a typed service, where one element received from the service is a string, but PHP has an automatic conversion to numeric types, but not Python, and even Node. Please run the following examples and see the result.

<?php // PHP code...
// comes a from the service as string
$a = "1.0";
// another element comes as integer
$b = 1;
// the application logic is adding both elements
$c = $a + $b;
// finally the result is 2
echo "{$c}\n";

Then we migrate that logic using literals to Python and we get an exception with a dynamic type that is not so dynamic as you think. Just think what happens on logic evaluation level and the amount of logic errors that you can reach with such differences.

# Python code...
# comes a from the service as string
a = "1.0"
# another element comes as integer
b = 1
# TypeError: cannot concatenate 'str' and 'int' objects
c = a + b
# no result due to the exception

In Node is not so different. Rather than getting the addition of 1.0 and 1, we get a string concatenation and not more numeric operations on that kind of strings. The amount of differences between dynamic type systems is huge, just try to make a boolean evaluation of empty lists or arrays in different dynamic type systems, some languages are evaluating empty lists as true and others are evaluating empty lists as false. Just take a look on this messy jungle of boolean evaluations in PHP. At least Python has a formal approach to its boolean evaluation and expression evaluation on its Language Reference on the Chapter 5. Expressions.

// Node.js / JavaScript code..
// comes a from the service as string
var a = "1.0";
// another element comes as integer
var b = 1;
// string concatenation rather than addition
var c = a + b;
// finally the result is "1.01"

So, the messy world of expression evaluation in dynamic languages is not so fun as you think, there are huge differences between languages, there is no standard way on evaluating expressions, leading to type safety errors and logic errors that can generate various programming mistakes. I still prefer strong-static typing to build applications.

Also, you will probably find some interesting facts. If you are a category theorist, you will probably find that with the JavaScript approach on types you cannot apply some abstractions like Monads, so JQuery is not a Monad because it is bound directly to a type, and JavaScript cannot bind types. And even you cannot apply some typed lambda calculus combinators in JavaScript due to its dynamic type system, some times disallowing some β-reductions requiring explicit type conversion despite it has a dynamically typed system because you cannot get the desired type conversion, forcing the usage of K combinators and similar ones on its dynamic type system.

But even with static typing you can get logic errors. Just track the amount of errors that your favorite financial company has on its web system built on top of their cool Java technologies or NET technologies. That is why I prefer languages like Haskell, their strong-static typing system are really type-safe, bringing a better final result. Programming Haskell is slower than programming JavaScript, but it is real type-safety is meet.

-- Haskell code...
module Main (main) where

main :: IO ()
main = let a = "1.0"
           b = 1
           c = fromIntegral b + (read a :: Float)
           -- the result is the desired 2.0 as Float
           in print c

No coments yet.

post a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>