Why static typing matters

This is a short post about an experience I had where the loss of type safety caused a nasty bug.

 

Look at the following code:

val tenYearsFromNow:Long = System.currentTimeMillis + 10 * 365 * 24 * 60
* 60 * 1000

 


It has a bug. Can you spot it?

 

The bug is that 10 * 365 * 24 * 60 * 60 * 1000 is computed as an integer and oveflows. tenYearsFromNow then holds a value that is twenty days from now.

 

Where I work this caused the server to stop functioning twenty days after being restarted...

 

The bug was not detected by the compiler because of the Java/C legacy where the compiler automatically promotes integers to long. So the loss of type safety is what caused it to be undetected during compile time.

 

Furthermore, this bug is not detected by unit tests (you don't wait 20 days...), so this is an example of how unit tests cannot cover the loss of type safety.

 

Conclusion? Type safety matters.


 

Thank you for your interest!

We will contact you as soon as possible.

Send us a message

Oops, something went wrong
Please try again or contact us by email at info@tikalk.com