How Safe is Your Programming Language

First, let me define what I mean by safe: the earlier a programming language catches a programming error for you, the safer it is. Haskell is extremely safe, whereas php is extremely unsafe. Some examples:

How do you measure the safety of a programming language? One can take specfic classes of errors and see how and when a language catches them. I will use the notation >> to mean safer than. A score is given for each language for each type of error corresponding to how safe they are.

Null Pointers

    So in this case:

Haskell, OCaml (2) >> Java, Ruby, Python, C#, Smalltalk, Javascript, LISP (1) >> C, C++ (0)



Array Index Out Of Bounds

    So:

Haskell, LISP, OCaml (2) >> Java, Python, C#, Smalltalk (1) >> Ruby, Javascript, C, C++ (0)



Type Errors

   

Haskell, OCaml (3) >> Java, C# (2) >> Ruby, Python, Javascript, Smalltalk, LISP (1) >> C, C++ (0)



Mispelled Named Parameters

   

OCaml (2) >> Python, Smalltalk (1) >> Ruby (0)



Wrong Number of Parameters

   

Haskell, OCaml, Java, C#, C, C++ (3) >> Python, Smalltalk, LISP (2) >> Ruby (1) >> Javascript (0)



So a language can be safer or less safe under different contexts. There obviously are other types of errors that I have not listed, but this will do for a simple comparison. I've already given scores for each language for each error type. Since some languages do not have the named parameters feature, I will average the score for each language over the error types that are relevant to it. So the Tally is:

  1. Haskell - scores: [2, 2, 3, 3], average: 2.5
  2. OCaml - scores: [2,2,3,2,3], average: 2.4
  3. Java, C# - scores: [1,1,2,3], average: 1.75
  4. LISP - scores: [1,2,1,2], average: 1.5
  5. Python, Smalltalk - scores: [1,1,1,1,2], average: 1.2
  6. C, C++ - scores: [0,0,0,3], average: 0.75
  7. Ruby - scores: [1,0,1,0,1], average: 0.6
  8. Javascript - [1,0,1,0], average: 0.5

It's not really fair that OCaml is scoring lower than Haskell, they should be the same, the difference is because Haskell doesn't have named parameters so the average is skewed - my scoring system is not perfect. Haskell is safer than OCaml in one aspect though. Which is that in Haskell, purely functional code is guaranteed to have no side effects and will never throw exceptions.

blog comments powered by Disqus