views:

86

answers:

4

Are there any tools out there to help programmers that REALLY want type safety and the more stringent rules of a language like c# or java with javascript?

For example, something that say analyzed source files and found things like undeclared variables or a js file of defined type objects like int or string?

+2  A: 

You want a lint program. Try jslint -- http://www.jslint.com/

no
+4  A: 

In addition to JSLint, Google Closure Compiler can be used for static analysis of JS source as well, and it checks different things than JSLint too.

Neither of these tools does provide exactly what you want though.. Closure Compiler attempts type-checking if I remember correctly, but it doesn't always work (at all).

Although such features may seem desirable from the point of view of someone who's used to strictly typed languages, I often see people attempting to use JS like C#/Java/whatever go wrong on more than one level, and usually ending up with less readable and worse code in general. Of course this may not be your case, but this is just a general observation/warning to keep in mind.

My suggestion would be to embrace the "looseness" of the language, with perhaps test-driven development as a quality assistance tool (JsTestDriver).

Jani Hartikainen
+1 for embracing the "looseness"
galambalazs
I started using JSLint. It is the goods - very good stuff. Closure, I find less useful (though that may be ignorance).
Dr.HappyPants
It sort of drives me mad though - the looseness - I mean. For example, I have a structure in my program for states that reads: ga.gameEvents.Idle etc. And at some point, I guess I missed a "." when running a macro and in my code was gagameEvents.Idle. So I spent 30 min sifting through chrome trying to find why a given value passed into a function was undefined. Grrr.Drives me a little mad knowing that a compiler would have caught this. I think JSLint will still miss things of this nature. :( Perhaps the right answer is GWT. Was I not doing html5 canvas stuff, I would use it.
Dr.HappyPants
You could try using an IDE such as Aptana or WebStorm. They can catch that sort of things, since they check if the variable you try to use exist or not. JSLint might actually catch the use of undefined variables too.
Jani Hartikainen
Awesome, I will check out those IDEs. Thanks Jani!
Dr.HappyPants
Here is a neat tool as well: http://www.codeproject.com/KB/macros/JSLintVS.aspx it apparently embeds JSLint in VS if you are using it.
Dr.HappyPants
A: 

Like @no said, jslint is hawt. I think you're looking for the Closure compiler, though.

lawnsea
A: 

if you don't link javascript language features, don't use javascript.

hvgotcodes
Thats great feed back. Let me just fire up some cross browser c# script. /sarcasm
Dr.HappyPants
depending on what you are doing, there are sometimes java libraries available that can be processed to output js, like gwt and the ext-gwt library. Other than that there is only so much you can do, like lint your code. As others have said, one of the "features" of dynamic, loosely typed language is that things change at run time and are casted into the type that makes the most semantic sense. There is not much you can do about it.
hvgotcodes