loading...
Cover image for Why you might need to type check your code

Why you might need to type check your code

faraazahmad profile image Syed Faraaz Ahmad ・2 min read

How many times have you had your code say, 'this is undefined' or 'that is not a function at so and so'? How long does it take you to even find the source of these errors? let alone fix them.

One of the worst things is finding out that you have a runtime error even though you haven't written any incorrect code. Sometimes, It can take a lot of sleepless nights to realise that you've made a type error, that you assigned an object of class Dog to a variable that should be an object of class Car.

such errors can usually be avoided by using type checking. What is type checking you ask? Well, (Very) simply put, you use data types like int, char, MediaStream, etc. while defining variables, functions, and other things and these data types are then used to check whether the variables with the right data types are being used. So if you were to assign an object of class Car to an object of class Dog, you should get an error, provided, the two types are incompatible.

Look, I get it. Strong typing increases the verbosity of your code. Nobody likes writing class names like SessionUtilDataTokenizer over and over again. Even coming up with proper names can be daunting at times (that is a topic for another time).

naming variables joke

By using strong typing, you can avoid most (IMO) type errors at compile time and get rid of them one by one. I think that is definitely more convenient than pulling out your hair trying to figure out what exactly the error is.

Oh! and did I tell you about IntelliSense?

IntelliSense is a general term for a variety of code editing features including code completion, parameter info, quick info, and member lists.
Simply put, It helps you remember which function or property is available on what variable, its type and what it does. This way, you don't need to have everything in the back of your head.

intellisense gif

At the end of the day, you are responsible for any bugs that creep into your code, but type checking can help you reduce them.

Posted on by:

faraazahmad profile

Syed Faraaz Ahmad

@faraazahmad

A passionate full-stack web developer.

Discussion

markdown guide