News for hackers. Stuff that matters.
Re:John Resig: JavaScript as a First Language by jashkenas on Wednesday December 21, 2011 @03:14PM (#3378948)
For the interested (and with the caveat that I definitely would not suggest it for the Khan Academy's purposes), CoffeeScript does try to address all of the issues that John raises in his post.

Type Coercion: There is no `==` in CoffeeScript. You usually write `if x is y` in order to be particularly clear, but if you're in the mode of most other scripting languages, and you write `if x == y`, it will compile to `if (x === y) {` in JavaScript.

John's note about `x == null` being the only useful application of double equals is quite true, and something that CoffeeScript provides in the existential operator: `if x?`

Falsy Values: The existential operator helps you ask the question "Does this value exist?" (Is this value not either null of undefined?) ... which covers many of the use cases for having saner falsy values in JavaScript. For example, instead of JS' `if (string) {` ... where the string may be the empty string, you have `if string?`

Function Declarations: JavaScript having function declarations, function expressions, and named function expressions as three functionally different things is indeed a wart on the language. Especially so because JavaScript having a single type of function is one of the beautiful aspects that shines in comparison to languages like Ruby, where you have methods, blocks, procs, lamdas, and unbound methods -- all of which behave in slightly different ways. CoffeeScript only provides JS's function expressions.

Block Scope: This is a tricky one, because unfortunately it can't be emulated in a performant way in JavaScript. So all that CoffeeScript can provide is the "do" keyword, which immediately invokes the following function, forwarding arguments. So, if a regular "for" loop looks like this:

    for item, index in list
      ...
A faux-block-scoped "for" loop would look like this:

    for item, index in list
      do (item, index) ->
        ...
Re:John Resig: JavaScript as a First Language by __david__ on Wednesday December 21, 2011 @06:10PM (#3379537)
I don't understand the (anti) hype about type coercion and the '==' operator. I get a faintly cargo-culty vibe from people when they talk about '===' vs '==', since the examples are usually very strange things that I never see in production. I'm not an expert in JavaScript but I've written a fair amount and a couple largish programs and I've never been bitten by a type-coercion bug involving '=='.

The weird subtle bugs I see are usually associated with me using "for (i in a)" for arrays instead of the more ugly C style for loop, which results in "i" being a string (which usually doesn't matter except when using "+" which is sometimes addition and sometime string concatenation).

So, is '==' really that bad? Has anyone seen (otherwise) well-written production code fail because of '==' vs '==='?

Re:John Resig: JavaScript as a First Language by hassing on Wednesday December 21, 2011 @07:37PM (#3379788)
I spent a lot of time trying to track down bug in a system once (in php) that was caused by this. I don't remember the exact problem but basically it did something like "if(x == 5)" which is true for both 5 and "5" - but later in the code it was important that the value was actually 5 and not "5".

But I think the main thing is that for people beginning programming it's better to have less flexibility. In most production system it might not matter that 5=="5" but it's important for beginners to understand how different types work. And it helps give a better insight into how if statements, and boolean logic in general, work.

Re:John Resig: JavaScript as a First Language by z92 on Wednesday December 21, 2011 @08:06PM (#3379863)
> but later in the code it was important that the value was actually 5 and not "5".

The bug was in that part and not in previous if(5=="5") part. Issues like these appear when Java programmers start to code PHP as if it's Java.

Re:John Resig: JavaScript as a First Language by hassing on Wednesday December 21, 2011 @09:58PM (#3380157)
I agree - in fact the problem was solved by removing some unnecessary type-checking code.

The point I was trying to make is that it's easier to learn the basics of types and logic in a strict system. Implicit type conversion and polymorphism can easily result in misunderstandings when you've just started learning to program.

Re:John Resig: JavaScript as a First Language by BerislavLopac on Thursday December 22, 2011 @03:16AM (#3380852)
"Easier" is (usually) the enemy of "better".
Re:John Resig: JavaScript as a First Language by hassing on Thursday December 22, 2011 @04:13AM (#3380949)
Again. These are classes for _beginners_. I'm not suggesting that you should never learn these things. They should also learn functional programming, estimating how algorithms perform, concurrent programming, binary trees, and a few hundred other things. But Programming is not something you master overnight.

You need to understand that 2+2=4 before you can understand that 2+i^2=1.

Re:John Resig: JavaScript as a First Language by BerislavLopac on Thursday December 22, 2011 @07:54AM (#3381367)
You are correct, but in that case it's not the question of what is easier, but what is necessary. "Easier" implies that there is a choice between comparable options, not that one is the prerequisite for the other.
Re:John Resig: JavaScript as a First Language by dspillett on Thursday December 22, 2011 @10:17AM (#3381744)
Yes, but using === would have identify such bugs by causing failure of the test rather than running the code for "is true" and getting odd results. The conditional code simply not running will often make the bug (one of the values not being of the expected type) easier to trace than a calculation resulting in 51 instead of 6 ("5"+1 rather than 5+1) at some point later.

You can fix such a bug in two ways: "manually" coerce the values when used, or make sure they get converted to the right type as early as possible. I find tha ltter, along with using === except where you really must have a more flexible comparison, to be the better approach. And if I mistype and skip a = I end up with == instead of = (which would cause a different class of bug!)

Re:John Resig: JavaScript as a First Language by 6ren on Wednesday December 21, 2011 @08:34PM (#3379935)
CoffeeScript is a subset of JavaScript with bindings to an alternate syntax (CoffeeScript). John's suggestion is an enforced subset of JavaScript, with the advantage that (some) JavaScript in the wild is understandable; error messages are in terms of the source language (very helpful, more so for beginners); and yields a skill with greater marketability (so far).

Why not use the same subset as CoffeeScript? In some cases, this simply forbids some constructs; in other cases it allows a construct, but only in certain contexts.

A mathematically precise but implementation-unfriendly definition is: only allow the subset of JavaScript that is the compilation of some CoffeeScript program.

Re:John Resig: JavaScript as a First Language by Cushman on Wednesday December 21, 2011 @09:48PM (#3380130)
You totally could; that would be "good" JavaScript. No problem with that.

Some of us like a cleaner, poetic syntax with significant whitespace and a few useful macros; we like CoffeeScript.

Re:John Resig: JavaScript as a First Language by latchkey on Wednesday December 21, 2011 @04:22PM (#3379215)
These are all reasons why I've adopted CoffeeScript over JS. Sure, you can accomplish the same exact things in JS, but they are far more natural with CS and with far less typing. Less typing is less chance for mistakes and bugs and makes an easier learning experience.

One other line item is the encapsulation of classes in CS. Doing that in CS is natural and easy to explain to a first year programmer. Accomplishing the same thing in JS is far more boilerplate and much harder to explain why you have to fight the language to accomplish something as simple as a class.

Sure, you probably shouldn't learn CS without at least a general understanding of JS, but I definitely think that CS is the next logical step for the second semester.

Re:John Resig: JavaScript as a First Language by gliese1337 on Wednesday December 21, 2011 @08:43PM (#3379960)
Seeing as how one of the arguments for teaching JavaScript first is that it lets you teach about prototypical inheritance before the students get used to class-based inheritance, the complexity of simulating a class in JavaScript is probably not the best objection to using it instead of CoffeeScript. If you teach them CoffeeScript, then you will teach them class-based inheritance just like everywhere else, and miss out on the prototype paradigm.
Re:John Resig: JavaScript as a First Language by latchkey on Wednesday December 21, 2011 @08:55PM (#3379994)
I'm ok with missing out on the prototype paradigm. To me, it is a wart for JavaScript. There has been a ton of discussion on why libraries like DateJS, which pollute the Date prototype, are bad.

http://stackoverflow.com/questions/4823682/javascript-namesp...

Re:John Resig: JavaScript as a First Language by wwweston on Thursday December 22, 2011 @03:23AM (#3380867)
The discussion you're linking to doesn't appear to discuss either general problems with the prototype paradigm or specific problems with adding properties/methods to the Date prototype.

The closest it seems to come is a warning about not trying to modify the prototype of an object through its __proto__ property (or, I suppose, .getPrototypeOf), which isn't the same thing.

Re:John Resig: JavaScript as a First Language by Cushman on Wednesday December 21, 2011 @09:17PM (#3380040)
Personally, I treat the `class` keyword as a useful (and powerful) macro for constructing prototypes.

I imagine if someone thought they were building a 'real' class-based program for too long, they'd be in for a few surprises later on.

Re:John Resig: JavaScript as a First Language by jashkenas on Thursday December 22, 2011 @10:33AM (#3381804)
This is a common misconception. CoffeeScript's "class" keyword is just JavaScript prototypal inheritance. They work and are used in precisely the same way. There's nothing here to "miss".
Re:John Resig: JavaScript as a First Language by danmaz74 on Wednesday December 21, 2011 @03:53PM (#3379085)
As matter of fact, while I was reading the post the first that came to my mind was how much the recommendations reminded me of the CoffeeScript introductory page...
Re:John Resig: JavaScript as a First Language by swannodette on Wednesday December 21, 2011 @11:15PM (#3380349)
Block Scope: This is a tricky one, because unfortunately it can't be emulated in a performant way in JavaScript.

This is not true. ClojureScript does this and it's plenty efficient. What you lose is mutability of the loop local from inside a closed over function. Given how many people get burned by the JavaScript behavior clearly block scope is expected. I doubt anyone would miss the dubious ability to mutate a loop local via callback. Certainly not as much as I miss control over scope & names in CoffeeScript.

Re:John Resig: JavaScript as a First Language by limeblack on Thursday December 22, 2011 @12:19AM (#3380502)
Re:John Resig: JavaScript as a First Language by gnaritas on Wednesday December 21, 2011 @06:17PM (#3379557)
And made the mistake of making whitespace significant thereby driving off people in droves.
Re:John Resig: JavaScript as a First Language by zephyrfalcon on Wednesday December 21, 2011 @08:07PM (#3379866)
Yeah, because it totally didn't work for Python... :3
Re:John Resig: JavaScript as a First Language by knieveltech on Wednesday December 21, 2011 @10:25PM (#3380221)
Touting what is arguably the most hated single feature of a particular programming language as a clear success? Uh...ok.
Re:John Resig: JavaScript as a First Language by zephyrfalcon on Wednesday December 21, 2011 @10:57PM (#3380303)
It is probably the most controversial feature. Most people either hate it or love it. I think it's hated mostly by those who don't use Python, and well-loved by those who do.

It was not the first programming language to use significant whitespace, but it is probably the most mainstream one that does, and it did pave the way for other languages to do the same. So it makes sense (IMHO) that sometimes other languages copy this feature, because it makes code shorter and more readable.

I haven't really heard any good arguments against significant whitespace, just the usual "but when I copy & paste a block the indentation might be off" (so use the indent/dedent function in your editor) and "I cannot mix tabs and spaces" (which you shouldn't do to begin with). The "hate" is mostly because people are not used to it. Not unlike Lisp's parentheses...

Re:John Resig: JavaScript as a First Language by quadhome on Thursday December 22, 2011 @02:28AM (#3380759)
In the (vain) hope of arresting the whitespace flamewars:

http://c2.com/cgi/wiki?SyntacticallySignificantWhitespaceCon...

Re:John Resig: JavaScript as a First Language by dextorious on Wednesday December 21, 2011 @06:32PM (#3379595)
It's 2011. We love significant whitespace now, get on with the times...
Re:John Resig: JavaScript as a First Language by mindcrime on Wednesday December 21, 2011 @10:21PM (#3380210)
Speak for yourself. I can tolerate Python's whitespace standards, but I - for one - damn sure don't "love" anything about whitespace being significant.
Re:John Resig: JavaScript as a First Language by malandrew on Wednesday December 21, 2011 @07:30PM (#3379766)
Assume you've never programmed before. Would you want to debug a program that has a significant whitespace bug? It's much easier for the average person to spot an unbalanced parenthesis that an incorrectly sized whitespace.

Getting stuck and being frustrated hurts motivation a lot.

Now curly brackets and syntax may be seen by a person as an irritation or inconvenience compared to whitespace, but I'd say it's better to use a language with an inconvenience than one which is more likely to leave you stuck.

You, I and others used to programming with insignificant whitespace have developed and eye for detail and can rapidly spot a whitespace but. The general population with no programming experience can't consistently space most of the documents in their life, but they can typically place periods correctly.

Re:John Resig: JavaScript as a First Language by zephyrfalcon on Wednesday December 21, 2011 @08:09PM (#3379872)
""" It's much easier for the average person to spot an unbalanced parenthesis that an incorrectly sized whitespace."""

Is it? Assuming you use a fixed-width font, any incorrect/inconsistent indentation should stand out like a sore thumb. More so than counting { }s in nested blocks. (Of course, decent editors help with both cases, so I think it's essentially a non-issue.)

Re:John Resig: JavaScript as a First Language by malandrew on Thursday December 22, 2011 @03:34PM (#3383138)
You've never run into problems with hard spaces vs soft spaces vs tabs?

Visually they look the same, but finding those problems is a pain.

Re:John Resig: JavaScript as a First Language by dextorious on Wednesday December 21, 2011 @07:48PM (#3379817)
"""Assume you've never programmed before. Would you want to debug a program that has a significant whitespace bug? It's much easier for the average person to spot an unbalanced parenthesis that an incorrectly sized whitespace."""

I'd argue that for a beginner it's equally difficult.

And how about this equally difficult to spot classic bug:

if (f==x)

  do_something()

  and_also_do_this() //oops!
a language with significant whitespace does not permit it. It gets people using the correct indentation levels from the start, whay they should have used anyway in any language, even one where whitespace doesn't matter.

I propose we don't talk about languages with "significant whitespace", but instead about "languages with proper indentation enforced".

Re:John Resig: JavaScript as a First Language by melling on Thursday December 22, 2011 @12:45AM (#3380564)
Why is this hard to catch? Format your code and the incorrect indentation goes away. A beginner should be taught to run the formatter to keep the code neat.
Re:John Resig: JavaScript as a First Language by chimeracoder on Wednesday December 21, 2011 @11:45PM (#3380429)
> I propose we don't talk about languages with "significant whitespace", but instead about "languages with proper indentation enforced".

Yes, 'significant whitespace' is too sweeping a term to be useful. I've heard people complain about Python's 'significant whitespace' until they realize that it uses basically the exact same whitespace rules as Java does, except with sensible indentation rules instead of braces.

http://www.secnetix.de/olli/Python/block_indentation.hawk

Re:John Resig: JavaScript as a First Language by malandrew on Thursday December 22, 2011 @03:37PM (#3383151)
What language is that snippet supposed to be in?
Re:John Resig: JavaScript as a First Language by gnaritas on Wednesday December 21, 2011 @07:12PM (#3379720)
No, we don't. Kids who don't know better might think it's cool; it isn't.
Re:John Resig: JavaScript as a First Language by dextorious on Wednesday December 21, 2011 @08:01PM (#3379852)
Yeah. Because only kids use and love Python and like the significant whitespace, right? And Haskell with it's significant whitespace --yeah, that's another language for "kids who don't know better"...

Anyway --you're a Smalltalk guy. How do you get to talk about weird syntax people don't like? Do many people like interleaving-argument selectors?

Re:John Resig: JavaScript as a First Language by gnaritas on Wednesday December 21, 2011 @10:17PM (#3380200)
You're mistaken if you don't think a shit ton of people dislike Python exactly because of significant white space as well as it being the source of a great many bugs.

As for Smalltalk, yes, they do like them, since faking them by passing literal hashes is a common pattern.

Re:John Resig: JavaScript as a First Language by dextorious on Wednesday December 21, 2011 @10:46PM (#3380280)
"""You're mistaken if you don't think a shit ton of people dislike Python exactly because of significant white space"""

Those people don't use Python anyway. There are a shit ton of people disliking anything you can think of. I don't know of many people that tried actually USING Python and still dislike the use of whitespace.

I also don't think the "whitespace haters" are intelligent about it or have any basis. For one, you should follow EXACTLY the same whitespace conventions in any bracketed language, even C. That is: you should properly indent blocks, and you should not mix spaces and tabs. If someone does not do that, I don't really think many people would like to work with his code. And if he already does that, then Python's whitespace is not any different, it just removes the brackets.

"""as well as it being the source of a great many bugs."""

I cannot think of ANY bug that can be caused by Python's use of significant whitespace. On the contrary, it prevents a lot of common bugs in bracketed languages (if/else nesting, etc).

The only think Python's use of whitespace can cause is compiler error messages (when you have messed the indentation) -- which is totally different from "bugs".

So, what bugs are you talking about? Any specific example?

"""As for Smalltalk, yes, they do like them, since faking them by passing literal hashes is a common pattern."""

Literal hashed are more like named arguments than Smalltalk selectors. For one, elements inside them are optional...

Re:John Resig: JavaScript as a First Language by gnaritas on Thursday December 22, 2011 @01:22AM (#3380633)
The issues around significant white space are well known and well blogged about, I'm not going to re-argue them here; google it you must, it's an old argument. If it doesn't bother you, so be it, but you're not going to sell me on it because it's crap; I can't stand Python.

If you don't think it has ever caused bugs, then you aren't thinking very hard or you've not looked into the opposing point of view at all. In any case, I'm done discussing it with you.

Re:John Resig: JavaScript as a First Language by dextorious on Thursday December 22, 2011 @09:15AM (#3381550)
"""If it doesn't bother you, so be it, but you're not going to sell me on it because it's crap; I can't stand Python."""

Yeah, I thought that was your conception on the issue: i.e just blind prejudice.

"""If you don't think it has ever caused bugs, then you aren't thinking very hard or you've not looked into the opposing point of view at all. In any case, I'm done discussing it with you."""

You invoked some mysterious bugs due to Python's whitespace rules, I asked for a specific example, but conveniently "you are done discussing it".

Nice.

Re:John Resig: JavaScript as a First Language by gnaritas on Thursday December 22, 2011 @10:17AM (#3381746)
I told you to google it because it's common, don't be an ass. Obviously I'm talking about the difficulty of sharing code and cut and paste issues between developers who have different ideas about tabs vs spaces.

It's not blind prejudice, signifigant white space is a valid reason to dislike a language; that's anything but blind. That is but one of several reasons I don't like Python, and none of them are blind. Ruby is my scripting language of choice when Bash isn't sufficient.

Re:John Resig: JavaScript as a First Language by leptons on Wednesday December 21, 2011 @07:18PM (#3379741)
What, no disclaimer that you are actually the creator of coffeescript?? Your comment seems a bit biased to me. But I'd expect nothing less from a coffeescripter.

So you think it's better for someone who has no experience coding to have to understand why they would write some code one way, then have to "transpile" to something else to get it to run? Then if something goes wrong they have to figure out how to fix it in two languages? This doesn't sound like something I would recommend for a beginner, or someone experienced for that matter.

Re:John Resig: JavaScript as a First Language by joblessjunkie on Wednesday December 21, 2011 @08:06PM (#3379864)
No, he specifically said at the start that he doesn't recommend it to Khan Academy.

To me, it reads as if he is simply supporting John Resig's decisions about what parts of JavaScript to teach -- by virtue of having made those same decisions in designing CoffeeScript.

Re:John Resig: JavaScript as a First Language by munaf on Wednesday December 21, 2011 @08:02PM (#3379855)
Probably the reason he said:

"...I definitely would not suggest it for the Khan Academy's purposes"