3 ms·
> Types guarantee that your code executes as you expect, and prevent you from writing incorrect code in the first place. If you write a Rational class and impl
by klibertp 13y ago
> Types guarantee that your code executes as you expect, and prevent you from writing incorrect code in the first place.
If you write a Rational class and implement it's + method as
def +(other: Rational):Rational = new Rational(0)
then no amount of static typing will save you. At least without dependent types.
Static typing gives you a knowledge about what kind of object you're dealing with without the need to run the code. It's almost completely useless if the "kinds" supported in type system are as broad as 'int', but it does help quite a lot if the type system let's you differentiate between cm and inches.
It still won't help you at all if you have a logic error - and this is where unit tests are valuable.
Personally I find a mixture of Typed Racket and Racket with contracts and unit tests to be the right thing when it comes to producing as error-free programs as possible. I was also pleasantly surprised with Smalltalk, which has no static typing, but because it runs all the time and every piece of code becomes "live" the moment it's written you can use introspection in place of static typing with very similar benefits.
Anyway, static typing is a valuable tool and one way of making software less wrong but it's important to know there are other tools and to use them all when it makes sense.
- mercurial 13y ago> then no amount of static typing will save you. At least without dependent types. Sure. Static typing also doesn't help with misunderstanding requirements, or a faulty test in a recursive function. But strong typing, especially in languages where there is no such thing as a NullPointerException, does eliminate entire class of problems. On the other hand, with dynamic languages, you are often a typo away from a runtime error.