Search

Monday, 22 October 2012

Lipstick on the Pig?

At the beginning of the month Microsoft announced what is effectively a new programming language, Typescript.

Typescript is a superset of JavaScript, so in a sense it's not really new. What it does is to extend JavaScript to add a feature of other languages that many developers, myself included, consider to be extremely desirable - static typing.

Now I'm sure I've just written some mumbo jumbo for any non-programmers reading this so I'll try to explain. When we write code we need to store various values that the code will work upon to produce whatever result is required. These values are called variables and are given a name so that each unique variable can be identified. Variables can also be thought of as being like an address pointing to a location in memory where its value is stored.

A variable can be one of many different data types such as text, a date or a number. If a variable is given a particular value in one section of code, to avoid any errors it's important that the code that uses it in another section knows whether it is text, a date or a number. If the code tries to add a text variable to a number, for instance, depending upon the language it could fail and produce an error, coders refer to it as a type mismatch.

It's a very common sort of error in dynamically typed languages, for instance many years ago for historic reasons I had to work on VB6 code that was dynamically typed and type mismatch errors were a common problem - especially when you were working on code someone else had written. It caused a lot of frustration and certainly cemented my preference for statically typed languages such as C#, that declare the type of a variable and then ensure that the values assigned to it and operations performed on it are always of appropriate for that type. By doing that a language eliminates a whole class of possible errors.

But JavaScript is a dynamically typed language, meaning that a number can be assigned to a variable and I need to remember that it's a number in that variable in order to avoid a type mismatch. That makes it unnecessarily complex for larger applications. JavaScript wasn't originally designed for building big applications with, but increasingly complex applications and frameworks are being written in it so great care and a lot of testing has to be done to ensure these applications don't have type mismatch bugs.

Typescript tries to alleviate this by providing a syntax with which the developer can declare the type of a variable and uses intellisense and a compiler to highlight the error when the wrong type is used in an operation with that variable. Hence in theory it should help to eliminate those types of errors. But it goes further than that and also provides other tools to help develop and debug larger JavaScript projects.

Unfortunately, it's only a veneer, very clever for sure but it's only cosmetic because Typescript is compiled into JavaScript so that the end product can run in your browser and have a chance of being 'multi-platform'. So the potential for those types of bug is still there and we have to trust that the clever guys working on Typescript cover all the bases.

Comments
To leave a comment please login
Register