JavaScript type safety is hard, and "I'm not smart enough to handle them"


(Nicholas Perry) #1

Continuing the discussion from Is it better for Discourse to use JavaScript or CoffeeScript?:

Speaking of jshint, I’d like to ask the community what they think can help make JavaScript code easier to write.

JavaScript type safety is hard, and “I’m not smart enough to handle them”.

I feel like JavaScript is just too big to keep everything ‘pure’ and it all ends up resting on my ability to be anal about what i’m shoving into variables. I’d like a system that helps to double check that I’m not dong something asininely stupid by mistake.

I’m excited about things like dart and other more type-safe languages used natively in browsers, but in the mean time I’d like to make JavaScript not suck so much.

In Perl, I knew conventions like ‘use strict’ and ‘use warnings’ which helped to keep code clean and maintainable and not fall into the little dynamic language gotcha’s. Even php had things like print_r() that could be used to see if the code was doing something unexpected.

The closet thing I have now is chrome’s debugger and metric engines, but every time I pull that up, I feel that its far too late in the process. I want the computer to be helping me code better WHILE I am coding, not forcing me to dive into all sorts of metrics after the fact.

also related: javscript and ruby ‘wat’

What other tricks can I use to avoid the living hell of “62dog” + 10 = 72 ?


(Jeff Atwood) #2

Be careful? Write unit tests? Have another developer do a peer review on your code? You know, the usual stuff?


(Nicholas Perry) #3

I’m a big into defensive coding because much of my experience is working with microcontrollers.

I like to write code that prevents me from accidentally assigning a ‘mile’ into a variable that contains a ‘kilometer’ without explicitly using a conversion.

In type safe languages, the compiler can catch these potential mistakes that even being careful can overlook. Its the whole “making wrong code look wrong” idea, but taking it up a notch and saying “make really wrong code not compile”.

I guess I’m more looking for ways to have the computer to do more help.


(Jeff Atwood) #4

Sounds like you may want TypeScript


(Nicholas Perry) #5

Been using facebook’s “flow” GitHub - facebook/flow: Adds static typing to JavaScript to improve developer productivity and code quality. that seemed to do the trick, but since moved on to mostly using scala.js when I need strong type safety. Been meaning to check out typescript, I am hearing good things :slight_smile: