Hacker News new | ask | show | jobs
by dnndev 1810 days ago
“If you are writing code that some other person is going to read and/or you are interested in having it behave reliably in the long run, I think a static type system is a must.”

Can’t this be accomplished with JavaScript? Assuming your interested in clean code and best practices.

“Coupled with good unit tests”

Can’t you also write unit tests in JavaScript?

“if the type checker does not complain, you can be very confident the code you wrote is working and will work for a vast majority of the cases.”

This is a false sense of security. “Compiling” without errors just tells me I did not do something dumb like assign a string to an int. Is this really the main issue devs have? From what I have seen no… Devs usually need to chase down and understand the code regardless of type checking.

Thanks for your perspective!

4 comments

> This is a false sense of security. “Compiling” without errors just tells me I did not do something dumb like assign a string to an int. Is this really the main issue devs have? From what I have seen no… Devs usually need to chase down and understand the code regardless of type checking.

I disagree. Working towards a successful "compile" isn't much different than TDD. The number of runtime errors I run into is significantly lower with TS than it ever was without which gives a very real sense of security.

You do need to understand the code but without types it can be very difficult to track down all the places that need to be fixed when you need to change your data model. Types are more important for refactoring than writing IMO

> Can’t this be accomplished with JavaScript? Assuming your interested in clean code and best practices.

There's nothing that prevents you from swimming against a stream, but we know swimming with the stream is faster. Same thing here. It's not impossible in javascript, but typescript makes code maintenance much more easier.

> Can’t you also write unit tests in JavaScript?

"Being more than sum of its parts" and all that.

> This is a false sense of security. “Compiling” without errors just tells me I did not do something dumb like assign a string to an int. Is this really the main issue devs have? From what I have seen no… Devs usually need to chase down and understand the code regardless of type checking.

Typescript does not solve all the software engineering problems. It makes common problems and annoyances much more easy to handle.

I don't have that much experience migrating codebases from JS to TS, but I do have experience converting codebases from C to C++. The corollary is that C++ is typesafe while C isn't. So you can call a function taking a point to an int with a pointer to a double, and the compiler won't complain (too much).

Word on the street says that C programmers are usually expert engineers that know what they're doing. Yet, I had to deal with those conversion errors all the time. That C code isn't a random CRUD application on a simple website. That can be software embedded in controllers in sensitive equipment, or equipment that will be harder to update. So it's even more important to get it right. And yet...

In C if you pass a pointer to double to a function expecting a pointer to int, that is a constraint violation (see 6.5.16.1 in the standard), and the compiler is required to emit a diagnostic.
For a good talk that discusses the boundaries of unit tests and the type system and how they overlap (or don't) I can highly recommend this video:

https://www.destroyallsoftware.com/talks/ideology