Skip to content
loading...

What the hell is type-safe language?

twitter logo github logo Updated on ・1 min read  

Consider you are a compiler. Now suppose you are a person who doesn't care about your fellow programmers who are writing code and using you to get them compiled. Like for example:

Someone is assigning an integer(4 bytes) into a char(1 byte) variable.

If you are not careful, that assignment will take away the adjacent 3 bytes away. Poof!.

This is type-unsafe. That is the compiler or YOU are not taking care of such validations before compiling them.

Again this can be at two levels:

  1. Static typing - one which is done at compilation time.
  2. Dynamic typing - one which is done at run-time. This is when you get all those run-time exceptions.

Be type-Safe!

twitter logo DISCUSS (8)
markdown guide
 

Why should I care about making a compiler happy in the first place? Static typing requires a huge boilerplate. No, thanks.

 

Well, you could have the compiler verify the types for you or write unit tests instead..

How else do you make sure the function expecting a User doesn't get a Monkey instead?

 

you could have the compiler verify the types for you or write unit tests instead

This is the biggest deception raised in the last ten years and I have no idea where is comes from. Strong static typing does not obsolete unit tests.

Please note, I even added strong to your original static which gives you even more freedom to argue.

A dozillion time strong and static as hell type would not help to catch "foo" instead of "bar". 99% of bugs exist not because somebody sent a monkey to the represent the user. Developers are not that stupid.

While pattern matching would. And restriction to create own types would. And even despite that we in erlang/elixir do indeed write unit tests. Because the vast majority of things that require an explicit check are like β€œthe currency rate cannot be less than one if the currency pair is normalized.” Welcome to protect this with your vaunted types.

I never said you would write 0 unit tests. You would write no tests for types.

Kotlin has nullability built into the language, so I don't have to write a test what happens when the variable I am passing is null.

I also don't have to do defensive programming and check if the variable is null.

I never check the variable I pass is null, mostly because I am sober enough to not pass nulls where they don’t belong. In CS variables do not become null all of a sudden.

 

What does dynamic typing have to do with "Divide by zero"? Zero is a value, not a type. Statically typed languages won't prevent division by zero.

 
 

True ... that was a mistake. Thank you for pointing it out.

Classic DEV Post from Jan 7 '19

Use the full power of Your Brain to be a Better Developer πŸ§ β€οΈπŸ‘©πŸ»β€πŸ’»

3 simple steps to use the full potential of your brain without any pills πŸ˜‰

Visakh Vijayan profile image
Willing to learn anything and everything exciting. Always awake. Skips food for fun.
Join dev.to

Be a better developer. Free forever.