8 ms·
Javascript is an absolutely horrible language to use for such purposes. There are far too many gotchas. See the following for numerous examples: http://stackov
by absconditus 15y ago
Javascript is an absolutely horrible language to use for such purposes. There are far too many gotchas. See the following for numerous examples:
http://stackoverflow.com/questions/1995113/strangest-language-feature?tab=votes&page=1 http://stackoverflow.com/questions/1995113/strangest-languag...
- maximusprime 15y agoDisagree... for example from the link: In JavaScript: '5' + 3 gives '53' Whereas '5' - 3 gives 2 That's just logical and obvious, since "+" is both numerical addition, and string concatenation, but "-" is only numerical subtraction, and the '5' starts out as a string. Every language has 'gotchas'. Doesn't really matter which you pick to learn first at all. The more important thing is that you don't give up. possibly there are languages that just make people want to give up, but I'd say perhaps they're not motivated enough to learn if that's the case. I started out on BASIC, and after a while I decided it was a piece of shit language and learnt assembly. But it taught me programming which is what I wanted to learn. I'm really glad I learnt BASIC first... essentially I learnt to swim really fast through syrup, and then switched to swimming in water. The good thing about javascript as a first language is that people can be programming in it immediately, in their browser. They have a built in REPL to help them, as well as a debugger, profiler, etc. They have numerous docs to look at, and if they go to any website they can check the source to see how it works. That's a big win.
- kevingadd 15y agoCalling your example logical and obvious is only logical and obvious if you're completely trapped within the JS mindset. It's the same as how people defend the absurd semantics of Visual Basic and PHP. It's fine if you like it, but to claim that it's objectively okay is just not supported by fact. It's also worth considering that even if every language has 'gotchas', some of them have far worse gotchas than others. It is worthwhile to choose a starting language that teaches the fewest bad habits and the fewest bizarre rules so that people can easily learn new languages.
- maximusprime 15y agoSee my comment below. It's not about being trapped in any mindset. It's about thinking logically. Either '5' - 3 results in an error of some kind, or it evaluates to 2. There is no other logical outcome. Javascript chooses to do the latter. I also disagree that starting language matters. It's like saying you need to have a steinway grand to learn the piano properly. Learning is about learning what not to do just as much, if not more, than what to do. Also if you learn to swim through syrup, imagine how fast you'll be when you try swimming through water...
- kevingadd 15y agoThe time wasted learning the quirky semantics and special rules covering each operator and data type in JavaScript could be better spent learning to reason about algorithms and data structures in a less confusing language. Not all learning is equal, and not all challenges are exactly the same. The claim that by hindering a newbie programmer's attempts to express themselves will make them more effective in a better language is ridiculous. Even if it's true, it's missing the point - your goal when teaching newbie programmers should be to teach them good habits and generally applicable skills, and most importantly, you want them to love programming. Having to memorize arcane minutiae and spend tons of time debugging problems caused by stupid design decisions is not going to make people love programming. JavaScript is tremendously accessible by virtue of its ubiquity, but that does NOT implicitly make it a good language for learning to program. Its numerous flaws and divergent implementations will drive away beginning programmers that might otherwise learn to love programming if presented with a better environment.
- absconditus 15y agoPlease do not downvote maximusprime's comments just because you disagree with them.
- mquander 15y agoYour ideas about what is "logical" are completely arbitrary. Look, I can make up rules too: * '5' - 3 should return '5', since it's the string '5' minus all the instances of the character '3' in it. There is no other logical outcome! * '5' - 3 should return an empty string, since it's the string '5' with the last three characters removed. There is no other logical outcome! * '5' - 3 should return '2' -- since we started with a string, the result should turn back into a string. There is no other logical outcome! * '5' - 3 should return 50, since the only logical way to do math on a character is to take the UTF-8/ASCII value of it and then do the math. There is no other logical outcome! * '5' - 3 should return undefined, since subtracting from a string typically doesn't produce a reasonable result. There is no other logical outcome! You have provided absolutely no rational basis for discriminating between the merits of these choices, so your claim that Javascript's choice is one of exactly two "logical" ones is bizarre. If you think Javascript's choice is better, give a reason why it's better, don't just say that it's better.
- barrkel 15y agoIt's logical and obvious if you already know the language; otherwise, it's perverse. I had to read your comment (the initial edit of it) twice to be sure you weren't being sarcastic.
- maximusprime 15y agoNo it's not. Logically, the only other thing that could happen is for an "error" or "exception" be thrown when you do '5' - 3 There's really only 2 choices. Either convert the '5' to a number and subtract, or throw a hissy fit because it's a string. That's not perverse. It's very logical.
- rprospero 15y agoYou're looking at the wrong side of it. The subtraction does behave in a perfectly logical manner. The problem is that, given the the behavior of the subtraction operator, the addition operator's actions are illogical. Specifically, I'd argue it's perverse because it breaks commutativity: '5' + 3 - 3 != '5' - 3 + 3 The logical approach would be to only assume that '+' is a string concatenation if both operands are strings and otherwise type coerce into numbers. Then: '5' + 3 = 8 '5' - 3 = 2 To someone who doesn't code Javascript for a living, the above seems like a far more consistent and useful behavior.
- maximusprime 15y agoThat would be ridiculous because then what would 'hello' + 1 equal? Everyone would expect it to equal 'hello1'.
- rprospero 15y agoExcept, not "everyone" would expect that. I'd expect "hello" - 1 to return NaN, since you can't perform numerical subtraction on something that isn't a number. That's exactly what Javascript does. In the same way, I'd expect "hello" + 1 to return Nan, since you can't perform string concatenation on something that isn't a string, and you can't perform numerical addition on something that isn't a number. Edited for clarity
- hello_moto 15y agoThat's definitely not obvious. Some people think in patterns or generalizations. String [binary_operator] Number = String String [binary_operator] Number = Number That's just begging for further explanation. Explaining JavaScript is probably more challenging than say Java. Java has many keywords and usages, but when it comes to explaining the concept of those keywords/semantics to students, it may be a little bit less confusing.
- maximusprime 15y agoOK here's plain english simple explain... Left hand side is a string, and "+" is string concatenation as well as addition. So '5' + 3 = '53' (String concatenation). Just as "Hello" + 1 would equal "Hello1". The right hand side is converted to a string. Left hand side is a string, but "-" is subtraction for numbers. JS converts the '5' to a number, then subtracts. So '5' - 3 = 2 (Numerical subtraction). That's the explanation, and it's not terribly hard to get past.
- barrkel 15y agoThe issue isn't explaining it. The issue is it needing an explanation at all.
- absconditus 15y agoYou picked one of a dozen or more examples from one list of problems with JavaScript. Of course this particular example can be understood by those of us who already have programming experience. JavaScript's type coercion is likely to be a big stumbling block for many beginners and Resig specifically points this out.
- __david__ 15y agoI hate behavior myself, though it's probably because I learned Perl first, where there's a separate string concatenation operator ("." in perl5, "_" in perl6). I do wish javascript had that same separation so that "+" was always addition and not sometimes string concatenation.
- bradleyland 15y agoOne might say the same about the English language, yet it is taught in classrooms all across the United States, despite the fact that the U.S. has no codified national language. English is the de facto national language, but nothing more. So why Javascript as a first language? Probably the best argument is that it has the widest reach of any programming language in use today because Javascript will run in any web browser on any platform. It is the closest thing to a universal programming language you'll find today.
- kevingadd 15y agoIf widest reach is your criteria, Java and C would also be equally good choices. Unless the web browser is special in some particular way that makes it 'more worthy' than other execution environments?
- maximusprime 15y agoEveryone already has a browser, which includes a profiler, debugger, REPL etc. Not everyone has a c compiler or JDK, or wants the hassle of having to set them up just to start learning.
- kevingadd 15y agoThe claim that everyone has a profiler, debugger and REPL is blatantly false. Only a subset of commonly available desktop browsers include those features, and out of those, only recent versions do. Mobile web browsers don't include any development or debugging tools. The cost of installing Visual Studio or Eclipse is pretty small considering that you only have to do it once. Compare that with the cost a newbie programmer pays every time they use the comparison operator or the arithmetic operator and they now have to consult their reference text to figure out what the operator is going to do and whether they're using the right operator. If your goal is to lead someone into a lifetime of programming and help them enjoy it, choosing a language in order to avoid a pesky 15 minute setup cost for a compiler is tremendously short-sighted.
- 15y ago
- bdfh42 15y agoI disagree as well - it is a very capable language with some very friendly features. Also it is very applicable for many new programmers - an all round good choice.
- asktell 15y agoEvery language has its gotchas. Even assignment operators are kind of a gotcha. If you're coming from a basic math background, and have no programming experience, then the expression "a = 15; a = a + 1;" would be confusing in any mutable language, because assigning a value to a variable is not equivalent to stating the equality (identity) of both sides.
- brlewis 15y agoYou're right in that even Scheme, which evolved with teaching in mind, has gotchas. However, the Racket folks made "student languages" that specifically exclude gotchas, so DrRacket is as close as a student can get to a gotcha-free environment.
- maratd 15y agoI have to agree. BASIC is a far better option. It will obviously never be used for anything, but it will teach the student basic concepts like output, input, variables, etc. These are not simple concepts for somebody starting from zero!
- jordan0day 15y agoBut, but, but, Dijkstra! In all honestly, I started out learning BASIC on Apple IIe's, and have managed to become an okay programmer. I would support it as a learning language. That said, I don't know that BASIC's syntax is that much easier for beginners than javascript. I mean, there's no braces or semicolons, but a lot of the control structures are basically the same.
- munificent 15y agoDijkstra could be an asshat sometimes. BASIC is great because it teaches you that a computer can be made to do neat things by following a series of tiny instructions. It gives you that without you having to understand lexical scope, which is, I think, pretty hard to grasp for new users. Sure, every user will outgrow it, but that's fine. Tricycles are a shitty way to drive to work, but that doesn't mean you should just put your toddler behind the wheel of your car and let him go.
- kls 15y agoThe problem is deeply technical people will put for the effort to learn deeper languages so that leaves a majority of the candidates as being people who want to learn programming because they want to scratch an itch. That being said, BASIC is usually not the language or platform they want to scratch that itch on, more and more the web and mobile are where they want to deliver to. That being said, JavaScript becomes the better investment of time, due to the fact that it directly parallels their goals in learning programming in the first place. More and more I am starting to think Objective-C and IOS may be a contender, while, Objective-C is technically more difficultly to get started in, the technology stack is a lot simpler, to be effective on the web, one needs to know HTML, CSS and JavaScript and then really and truly they need to know a back end language or Node.js to deliver a back end. Whereas Objective-C and IOS is a visual editor and code. The feedback loop is faster for IOS development than JavaScript development and I believe the feedback loop is very important to a certain population of would be developers. I have never tried to teach a non-programmer Objective-C and IOS dev so the above statement is loaded with assumptions. Please read it as such, but I am leaning towards trying it next time someone approaches me and wants to learn.
- DrHankPym 15y agoWhatever, Javascript is the easiest language to get started on. No installing a complicated IDE. No configuring a server. Nothing is required except for a browser! If anything, it's the best language for such purposes: getting started.
- showell30 15y agoI don't think Javascript's availability in the browser should be a major driving concern. Browser-based REPLs and editors are getting better and better. Two examples: http://repl.it/ http://repl.it/ and http://cloud9ide.com/ http://cloud9ide.com/.
- phzbOx 15y agoAlso, as stated by John, JSlint will be used. That by itself avoid the big majority of JS' gotchas. And IMO, C++ has way more gotchas than Javascript and that doesn't stop universities to teach it as first language. In fact, I think the first language is not just about giving a strong foundation but also give students the chance to find a good internship. So, with that logic, even though I enjoy hacking with Scheme, I'd feel bad to teach that to students who will try to find an internship and writing on their resumes "Scheme" while other universities students would have "C++/Java". And, please, before your argue that a language can be learned or something else, have in mind how companies recruit and parse resumes.. they often just search for a keyword. Now, one might say that you don't want to work for a company who does that, but then, you have to start somewhere in your first internship. Thus, I like JavaScript as a first language; not because it's the easiest to learn but because I believe it gives a strong foundation but also gives students the ability to find a good internship.
- Stormbringer 15y agoWell, I wanted to know what IDE JSLint was an expansion for, so I did some poking around... it's an online tool. Meh #1. Next up I thought I would take a snippet from the linked stackoverflow discussion and see what JSLint made of it: 127.0.0.1.toInt(); And... it complained So I did this: var = function() { 127.0.0.1.toInt(); } And it complained (yes, there's a typo) var foo = function() { 127.0.0.1.toInt(); } And... it 'compiles' fine, no complaints. So I thought I'd push the syntax tree button to see what was going on and it tells me: Tree: "" Which is less than spectacularly useful. Oh, I went back and hit the JSLint button again and now it givess me lots of errors for the above code. Including, but not limited to complaining that 127 occurs in column 1 instead of column 9. But then I try to enter a tab at the start of the line to achieve proper indentation... and of course it doesn't work because it is in the browser and so tab just goes and selects the next control. Sensational. If I had to enter all my indentation as spaces, by hand, I'd conclude that the Professor teaching this was a colossal idiot.
- malandrew 15y agoWhitespace can be a huge gotcha for novice programmers. Hard spaces? Soft spaces? New lines?
- wwweston 15y agoI suspect Mr. Resig is familiar with the potential gotchas, and maybe even has his reasons for still believing it's still worthwhile. For my own part, as someone who regularly taught high school seniors (and other non-programmers) various programming languages (Pascal, Perl, Prolog, JavaScript, and Java) over a good chunk of the 1990s, I have to say that JavaScript worked out pretty well, possibly even the best of the bunch depending on what your goals were. Prolog had some real strengths. The combination of accessibility for some simple applications with the power and conceptual depth of the logic paradigm tended to put more or less smart people with no previous experience on the same footing with those with some experience. But JavaScript seemed to be the best of the bunch in terms of balancing accessibility, speed from which students could get to doing something with everyday practical use, and available depth. And despite the fact that those of us who were teaching were just learning and coming to grips with some of the "bad parts" ourselves (because how many people really knew JavaScript in the 1990s, right?), everybody got stuff done anyway.
- showell30 15y agoI can see the advantages of JavaScript over Pascal, Perl, Prolog, and Java. Did you ever try Python? What are your thoughts on Logo, Karel, etc.?
- wwweston 15y agoI'm a little down on things like Logo and Karel, partly because I prefer things that are more general, and partly because even in the niche I might use them, my judgment is that Scratch is better. :) I didn't try Python myself until late 2001, and by then, I was already more or less out of education, so I didn't get to try it out on anybody else. But if I were guessing, I think the language itself would probably work pretty well for most students, possibly somewhat better than JS. Not sure how big the advantage would be, though (like I said, we really didn't struggle with JS's warts much), and I think the broad target for JavaScript's use give it something of an advantage too. This would vary by domain, though. If I were trying to teach a number-crunching focused class, for example, Python would probably move way up my list.
- zackzackzack 15y agoWhenever I see a post about what to use as a first language I always think about how I learned, and am learning, how to program. Warning: long-ish story/rant ahead. My first class in high school was the basic Java 101. The teacher did not know anything more than how to draw UML diagrams and tell us to use for loops. He had been teaching the class for years and had no industry experience. I imagine 10 years ago he figured out all the questions he was going to ask and learned just enough to explain the really difficult ones. He was not a zen master LISP programmer who had glided down from the plane of Forms to enlighten us. But I started to learn programming because of him. He knew what he knew and he had his presentation down. I didn't know how to program so anything was better than what I was going with. He explained terms and ideas in the most stereotypical of ways. But they made sense. In a way, he passed off his framework of knowledge to those who cared to take it. It was incredibly mind expanding at the time. And so I had this little ball of specific domain knowledge. I wanted to be able to do cool stuff though on my website though. Java wasn't quite there yet, couldn't do all the things I wanted it to do. So I picked up Javascript from W3 schools online. It was terrible, awful Javascript that I have purposefully forgotten. But I could make my pages interactive, I could impress my friends. My little ball of know how was growing and expanding. I kept at it and started seeing how Java "sucked" and why Javascript was the One True Language. New ideas like functions as variables, easy to run scripts, interactive webpages, they all got added on and replaced old less useful ideas from Java. Pretty soon after that I started actively learning Mathematica in college. The whole IDE is just lisp with some mathematical lipstick, but I didn't know that at the time. I started thinking of how to solve problems with lists, how to use IDE's, and reading and writing to files so I wouldn't have to redo everything each time. I had programs that wouldn't run in less than a second which made me really aware of what I was writing and how to optimize things. And the story just keeps going and going on like that. Common lisp with Emacs last winter because of PG. More Mathematica and jQuery during last spring and summer because of an Internship. Node, Haskell, Clojure, Python during this past fall for enlightenment. And now R and, maybe, Ruby/Rails during the winter because I have ideas I want to make happen. I'm by no means a professional, but I know enough to shoot the shit with the CS majors and randomly drop into tech meetups (What up Cinci.rb!) All that sprung from a high school class taught by a standard teacher on a shitty language that I don't ever want to touch again. And so when people talk about first languages and why X will be an absolutely horrible language to use to learn, I zone out. Every language is going to be a horrible first language. All of them. I don't care what arguments you make about js gotchas or Python's batteries included or why scheme is more beautifuller than common. If you don't know how to program, learning how to program is going to suck. Period. Maybe learning C++ first isn't the hottest idea, but at some point you have to grit your teeth and go "Programming is hard." So I applaud Resig's and Khan Academy's use of javascript. Not because I love javascript, but because I know they are going to doing an amazing job presenting the tiny balls of programming knowledge to kids across the world. They could have picked most any mainstream language they want to present these ideas with and I would still be excited by their ideas. All that matters is how they plant the seeds. If they can stir up a kid's desire enough to get over the "ARRAYS SUCK. FOR LOOPS SUCK. WHY WON'T MY PROGRAM COMPILEEEE." and learn the next language or build the next project, then they have done an amazing job and good for the world. tl;dr Programming is hard, every language has it's own use, waffling about the choice of languages isn't going to teach you what you need to know any faster.