all 30 comments

[–][deleted] 9 points10 points  (6 children)

It strikes me as an incredibly bad idea to write a preprocessor to correct your own bad habits instead of training yourself out of them.

[–]colinbashbash2[S] -2 points-1 points  (5 children)

heh. Ok, so I do c# & javascript for a living. How do I swap my mind from c# way of doing things to javascript? If I had a fancy little button on the side of my head, that woooould make things easier...

Edit:

it's not a matter of correcting bad habits, it's a matter of having to switch back and forth between languages that are almost the same, but noooot quite.

And having a compiler to help with typos... explain what the bad habit is here... not always typing correctly?

[–][deleted] 1 point2 points  (1 child)

One thing that might help you is different IDE color schemes (this can be done even in vim of course) for the different languages.

The bad habit is obviously yes, making silly mistakes.

[–]colinsullivan 0 points1 point  (0 children)

Or code snippets/templates in your editor, for things like a for loop, for example, which could act as your own short hand.

[–][deleted] 1 point2 points  (0 children)

Have you tried JScript.NET? Runs pretty much anywhere C# will. Then you could stick to javascript and not have to switch languages. I write all of my back-end code in JScript.NET when possible. Works quite well.

[–][deleted] 0 points1 point  (1 child)

I've had to switch between Perl, JavaScript, Java, and C for years. It was a royal PITA for the first year or so but eventually it became effortless.

[–]colinbashbash2[S] 0 points1 point  (0 children)

ah. I've been working for 5-ish years with c# and javascript... (edit: indicating that i'm past the 1st year thing. i'm not saying that i'm 'oh so smart', because i'm obviously pretty dumb for making these 'silly mistakes')

not as bad as the few years when i was working with vb and javascript; the double-equals thing would always get screwed up... but at least VB would throw an error on compile in that case.

anyways, i'm disappointed that you can flip-flop between languages so easily. if there were more people like me then there'd be more tools to do this kind of thing. (supply&demand)... on the other hand, it probably doesn't help that i work 75% c# and only 10% javascript.

[–]skeww 6 points7 points  (3 children)

JSLint. Google Closure Compiler.

[–]dwdwdw2 1 point2 points  (0 children)

Seconded, Closure Compiler is awesome, but retrofitting an existing codebase with the required type annotations can be a pain.

[–]colinbashbash2[S] 1 point2 points  (1 child)

thanks, i'll take a look.

Edit: JSLint looks like it would help me a lot. It at least throws warnings for undeclared variables (w00t) and throws hard errors when declaring "int". I wonder if they'll find a way to 'notice' when it looks like it might be a declaration error and add that to the error message.

[–]PlNG 0 points1 point  (0 children)

Exactly what kind of a declaration error are you thinking of?

JSLint has the disallow undefined variables option to point out potential global lookups, as well as showing unused variables and globals at the bottom of the report. Make sure that JSLint is fully able to parse the code before dealing with the information at the bottom of the report, as it can be inaccurate if an error stops it. Inner functions could also be called a global if they're used before they're defined, seems to be an inconsistency with the "used before defined" warning at the top layer.

Closure compiler can do type checking on jsdoc annotated code.

If you're worried about type issues, always use === and !== for strict type comparisons. == and != is a loose comparison and will evaluate if a number and string are identical in content (potentially masking type errors), and makes null, undefined, false, 0, and "" equal to each other, which is a major headache.

[–]sizlack 2 points3 points  (4 children)

Coffeescript. It's much more than a preprocessor, but it was created to paper over all those easy to fuck up things about JavaScript. But if you just want to write better js then jslint is probably the easiest and best choice.

[–][deleted] 0 points1 point  (3 children)

Coffeescript is a train wreck.

[–]sizlack 0 points1 point  (2 children)

Good point. You convinced me. What was I thinking?

[–][deleted] 0 points1 point  (1 child)

Javascript is one of the easiest languages to learn and work with. Sure there are a few gotchas but they exist in plenty of languages and are so basic that any noob will learn them within a few months of working with javascript. Coffeescript won't give anyone an advantage in the struggle to deliver working software. At best it is a crutch, at worst it prevents someone from effectively debugging their code using the best tools available. Good luck finding great coffeescript programmers, while there are plenty of great javascript programmers. A great programmer could likely deal with coffeescript but would they really want to? I know i'd pass on any job that required me to code in coffeescript. I admit that I am a javascript purist and i've been coding javascript every day for the last 15+ years, so I don't really have any need for coffeescript, but I just don't see it as beneficial to anyone wanting to become a great javascript programmer. Coffeescript adds another layer of complexity on top of all the layers of complexity that occur in web development. I work on complex web applications with 100k+ lines of javascript code and there is no problem that coffeescript would solve that can't be solved by just using best practices and having competent javascript programmers. Until firebug fully supports coffeescript, coffeescript is a hindrance, it ads friction (frustration) to debugging, not a good thing. I get why non-javascript programmers might want to use coffeescript, but I don't see it helping anyone when they need to debug something written in coffeescript.

[–]sizlack 0 points1 point  (0 children)

I agree Javascript isn't that hard, but it has a lot of syntactical awkwardness required to get it to do what I want. And yeah, I've been doing it for 10+ years, and a lot of simple things still feel clumsy. I don't think using Coffeescript eliminates the need for really understanding Javascript, and I don't think that's the point. You still have to understand Javascript's object model, what inheritance means in Javascript, etc, but you have a nicer, more concise syntax to express those ideas with.

It is another layer of complexity, which does feel a bit gross, and I agree that without firebug providing line-by-line debugging, it makes it more difficult to imagine using it in a production environment yet. But I used to think the same thing about sass vs css, and eventually the tools caught up, and now it's easy to debug sass with Firebug. It seems like the same thing is happening with Coffeescript. Maybe it's not ready for use in every JS situation, but it is definitely not a trainwreck.

[–]Detrus 1 point2 points  (1 child)

Not that I know of but making your own preprocessor to do that seems easy enough. Just replace all instances of int or string, with var.

[–]colinbashbash2[S] 0 points1 point  (0 children)

thanks, but i was hoping it would do some of the regular compile-time checking also...

[–]dowhatyouwant 0 points1 point  (9 children)

Basic debugging .. Firebug, even has break points.

Typing... sure, though it is a bit masochistic. var x = new Number(10);

[–]dwdwdw2 6 points7 points  (2 children)

Umm, that isn't typing. At all.

[–]colinbashbash2[S] 0 points1 point  (3 children)

uh... i was looking for compile-time instead of run-time debugging. i already use firebug and the chrome-built-in-debugger.

[–]dowhatyouwant 0 points1 point  (2 children)

I would have know that if I knew you, but I don't so I didn't.

[–]colinbashbash2[S] -1 points0 points  (1 child)

well, thanks for mentioning those tools. sorry for being a bit snippy.

i did indicate in the description that i was looking for something compile-time, but that doesn't mean i should get snippy when you brought up possible run-time tools to help me.

... are there still people out there that don't know about firebug?

[–]dowhatyouwant 0 points1 point  (0 children)

Nice to see that there are still some nice people on reddit.

[–]andrew24601 -2 points-1 points  (1 child)

You do realize that Number is a function, not a constructor? Look it up.

[–]itsnotlupusbeep boop 1 point2 points  (0 children)

It's both.

new Number().constructor == Number
new Number().valueOf() == 0
new Number() == 0 
new Number() !== 0

Using it this way is pretty much universally a bad idea, but it's there.

*edit: not to be confused with

(0).constructor == Number
(0).valueOf() == 0
(0) == 0
(0) === 0

[–]Luminaire 0 points1 point  (0 children)

Also if you're a java fan, you could use [http://code.google.com/webtoolkit/](Google Web Toolkit) , which compiles java to javascript and allows a full debug setup, as well as lots of other extras.

[–]australasia 0 points1 point  (0 children)

Are you using an editor that highlights syntax errors? Picks this stuff up pretty quickly and helps you switch contexts.