r/programming • u/sheldonpooper • May 09 '12
JavaScript: The good parts
http://blog.movereem.nl/images/javascript-the-good-parts-the-definitive-guide.jpg45
9
u/tragomaskhalos May 09 '12
IIRC "the definitive guide" goes into punishing detail about all sorts of noddy bits that you are going to know from any other language, which would account for its brobdingnagian footprint. Maybe it's targetted at HTML jockeys with zero prior programming experience assumed.
7
May 09 '12
brobdingnagian footprint
I thought you made that word up until the dictionary laughed at me and told me "read a book ya dipshit".
1
21
May 09 '12
Old, and wouldn't this be true of every language?
24
u/angrystuff May 09 '12
Some languages are better than others:
48
u/Fissionary May 09 '12 edited May 09 '12
Hello World is just
print "Hello, world!"Not in the latest version of Python, it isn't. It will work in Basic, though.
The simplicity of the Hello World program is not a good indicator of the power of a language.
29
u/Wolfy87 May 09 '12
No, now it uses parenthesis. How difficult.
25
u/Fissionary May 09 '12
Difficult? No, it's not difficult at all. In fact, it was a good decision, getting rid of the special syntax for
3
u/Wolfy87 May 09 '12
I was trying to imply a bit of sarcasm with the italics. Sorry about that.
I think it is the right move for the syntax but it will be annoying to convert.
3
u/jmmcd May 09 '12
"Will be"? -- no, everyone should be using print-as-a-function since what, 2.6 or so.
4
u/kqr May 09 '12
You can syntactically pretend it's a function, but it's still not a function. If you need a printing function pre-3.0 you'll still have to use os.write() or something like that.
5
u/jmmcd May 09 '12
I was surprised at this so:
Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49) [GCC 4.2.1 (Apple Inc. build 5646)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> x = print File "<stdin>", line 1 x = print ^ SyntaxError: invalid syntax >>> help(print) File "<stdin>", line 1 help(print) ^ SyntaxError: invalid syntax >>> print("hello") helloTIL thx
2
u/roerd May 09 '12
You still need to enter
from __future__ import print_functionin Python 2. After that, it is a function. (Just putting the string in parens should always have been valid code, that's just their usual grouping functionality.)0
0
1
u/Liorithiel May 09 '12
Well, actually I have already seen one person who used print() as function and trying to deal with its return value. Confusion arises, py2 would at least signal an error.
9
May 09 '12
it's not difficult, but it is the wrong direction for the language to move in. Ham-fisted correctness over readability? WTF python devs .... I'm sticking to the 2.7 branch as long as I can ...
5
u/kqr May 09 '12
Do you not want to be able to pass print to a higher-order function?
4
u/Traubert May 09 '12
That's possible even if print is kept as a keyword too. You can do it in any of the 2.x versions that provide print as a function.
6
u/kqr May 09 '12
How do you pass it? It just syntax errors me when I try.
2
u/Traubert May 10 '12
Hey, what do you know, you're right! I'm sorry I made that claim without checking it out. I assumed that as a rvalue it would be evaluated as a function reference, but it's a part of the syntax so it does indeed just error out. Thanks for teaching me something.
1
-2
u/sli May 09 '12
Why would you want to pass it and not simply use it normally?
2
u/cacahootie May 09 '12
You're joking, right? Metaprogramming is super powerful in Python.
→ More replies (0)1
u/HorrendousRex May 09 '12
It could be useful in some circumstances I suppose where functions are passed first-class in to the arguments of other functions, to print out the order the callbacks are being called in. Although at that point I can't help but feel that something like:
lambda x: print xmight be just fine. Or better yet, import py3k's
__future__.→ More replies (0)2
May 09 '12
if parenthesis and correctness mattered more than readability, we would all be using LISP, I use python for that "import antigravity" feeling of irrational exuberance illustrated in the xkcd comic. Anyway I tend to use the logging module for the cases where you need to pass something like print around.
0
u/Ran4 May 09 '12
How often do you really do that? In those few cases, there are tons of workarounds.
Having print be a function might be the "right" decision, but it's still incredible annoying and makes debugging slower. Oh, and 9 times out of 10, setting a trace and using the python debugger is not faster than adding a line or two of print, no matter how many times you try to say otherwise.
The gain is next to zero but the losses are massive. Having print a keyword was the right decision.
5
u/gthank May 09 '12
Seriously?
print()is "annoying" and "slower" than1
u/Ran4 May 09 '12
Stop playing stupid. It is annoying and slower (editing the code), the typical excuse though is "don't use print for debugging!" but that's a bad excuse because using print:s is most of the time an easier way of debugging code.
2
u/gthank May 09 '12
I'm not playing stupid. I can't imagine the difference between
print()ever being significant enough to notice. Hell, I've had tendonitis reduce me to 1-handed, hunt-and-peck typing and it still wasn't even a big deal. Of all the gripes about Python 3, this is the only one (off the top of my head, at least) that I just can't even begin to understand.1
u/velit May 09 '12
You can define aliases in ipython for example. Or you can just write the parentheses.
2
u/Wolfy87 May 09 '12
I think the most problems will come from conversions. Writing from scratch is fine. But I agree, 2.7 just feels better at the moment. And until Django moves I am not budging.
8
u/fernandotakai May 09 '12
I really want to start using py3 just because of the "strings are unicode by default".
I hate writing u"something".
15
1
u/HorrendousRex May 09 '12
Eh, be careful... strings aren't unicode by default, they are abstract by default and can be encoded in a unicode encoding (like utf8) very easily.
Yes, internally they are being stored as unicode strings, but the point of py3 strings is that they are not tied directly to an encoding.
2
u/gthank May 09 '12
Then you probably want to keep track of this: https://www.djangoproject.com/weblog/2012/mar/13/py3k/
1
u/cacahootie May 09 '12
Although I too am stuck at 2.6, I think the moves py3k made are good, there's at most an iota of difference in readability, but many of the idiosyncracies and cruft are gone. Python has been around for a long time without breaking compat, I think the time has come.
5
May 09 '12
The simplicity of the Hello World program is not a good indicator of the power of a language.
9
u/patternmaker May 09 '12
Indeed; http://xkcd.com/224/
0
May 09 '12
The irony of using that comic an an example is that it shows that even the good lord can't use LISP for something practical...
2
1
9
May 09 '12
Just as an aside and in case you don't know, if you now "import antigravity" on python, it'll open your browser on that comic page
-2
9
u/yogthos May 09 '12
There is the principle of least astonishment, and JavaScript is one of the worst languages in that regard.
6
u/Amadan May 09 '12
PHP is worse. I can use JS and be perfectly happy (although I do prefer Ruby). Sure some things are a bit wordy (hello, Mr.
hasOwnProperty), but it's really not too bad. PHP on the other hand is a blight of bugs and bad surprises.5
3
4
May 09 '12
In what way?
20
3
u/fnord123 May 09 '12 edited May 09 '12
[] === [] and friends. There's a video kicking about with this shit.
1
-10
May 09 '12
I have never done that, so it's a non-issue.
In chrome I also get false, so it's not even correct.
16
u/fnord123 May 09 '12
"I've never done that so its fine for me." Words to live by.
4
1
May 09 '12 edited May 09 '12
What i should have said is that I have never seen anyone do that in any language, in my code or anyone else's, over the last 10 years.
His example isn't even correct, and
doesn't work.it works as expected.1
u/julesjacobs May 09 '12
You've never compared two lists?
1
May 09 '12
Not with empty array literals, no.
1
u/julesjacobs May 09 '12
...
Have you ever had an empty array in a variable? Have you ever compared variables that contain arrays?
5
u/yogthos May 09 '12
I have never done that, so it's a non-issue.
Yes, but one day you'll have to look at somebody else's code, and they're going to do shit you don't do, and you're going to be wondering what the hell is going on there. Or worse, it'll look like it's doing one thing, but really be doing something else, because of some subtlety of syntax.
In chrome I also get false, so it's not even correct.
That's the other problem, that it doesn't behave the same way in every browser, which makes it that much more difficult to write Js that actually works everywhere as expected.
1
May 09 '12
That's the other problem, that it doesn't behave the same way in every browser
I tried your code in opera, firefox, safari, chrome, IE 9, IE 8, IE 7, IE 6 and even IE 5.5, and all yield the same result: false. Where does it work differently?
Outside my code, I've also never seen anyone else do '[] === []' in 10 years of programming, but even then, false is exactly what it should and does return.
1
u/doomslice May 09 '12
Once you understand the difference between value and reference types, this doesn't sound bad, and is similar to how many other languages work.
1
u/yogthos May 09 '12
This has nothing to do with value and reference types, Js has tons of quirky syntax and evaluation rules which are not at all intuitive.
1
u/doomslice May 09 '12
This has nothing to do with value and reference types
No? I was responding to the assertion that [] === [] -> false is unintuitive/incorrect, when actually it is correct since it performs reference evaluation. Do the same with a value type and you'll see it perform as expected.
1
u/fnord123 May 09 '12
Do the same with a value type and you'll see it perform as expected.
> [] == [] falseI expect [] to be equal to [] by value. Python does this:
>>> [] == [] TrueYou may be interested to know that numpy gets this wrong, imo:
>>> import numpy as np >>> np.array([]).__nonzero__() False >>> (np.array([]) == np.array([])).__nonzero__() False >>> np.array([]) == np.array([]) array([], dtype=bool)Back to Javascript. Here's some more weird examples:
> [] === [] false > [] + [] "" > []-[] 0 > Object() == Object() false > Object() === Object() false > [1,2,3] + 2 "1,2,32"→ More replies (0)2
u/munificent May 09 '12
ASI.
3
u/masklinn May 09 '12 edited May 09 '12
ASI is nothing compared to the way
thisbehaves (or doesn't, mostly) in JS .1
u/munificent May 09 '12
Yeah, but at least there's an underlying logic and some value behind
thissemantics. ASI is just busted.0
u/Cygal May 09 '12
global variables, function scope, this binding, new constructor, arrays indices as string,
for inordering, and so on. JavaScript: The Good Parts explains this quite well.6
u/pavel_lishin May 09 '12
Not really. "PHP: The Good Parts" has a similar size comparison, but unlike the Javascript book, does not actually contain any good parts.
0
4
u/sgoody May 09 '12
Agreed, I think most of us have probably seen this a good few times already.
Whilst it's a funny image, I don't think it's being fair to Javascript either.
0
u/cacahootie May 09 '12
I think it is totally fair. Javascript works, but man is it a dog of a language. I think the image conveys the appropriate balance between the language and what doesn't suck.
1
u/sgoody May 09 '12
I'm not saying it's the greatest of languages, but I don't think it deserves all the bad press it gets. I think it's a pretty competent language and I think most of people's complaints are cross-browser compatability.
There are some deficiencies... such as the lack of namespaces... but it does allow you to get things done pretty quickly and sometimes quite nicely within a browser.
edit: I'm also agreeing with the parent who points out that almost all languages have their nobbly parts.
1
u/cacahootie May 09 '12
Agreed - a big part of my hatred is developing targeting ie8 (internal web apps) and the terrible nature of ie...
Tthe crux of my complaint is that javascript tries so hard to fail silently that errors/tracebacks are meaningless. Add in the fact that using a framework adds another level of confusion, and you have a language which sucks to develop for.
Every language has its problems, but the problems w/ js are fundamental design choices, not idiosyncracies.
16
10
May 09 '12
0
May 09 '12
[deleted]
4
u/strife25 May 09 '12
Eh, the JS book doesn't really teach much - it's a reference more than anything.
Google is one of the best programming resources around, although I've found that books where the topics address system design, soft are architecture, design patterns, best practices etc. are worth it. Javascript: The Good Parts is one them (and a must read for anyone that cares about the language). Books that are mostly language references are becoming obsolete imo.
4
u/mq2thez May 09 '12
I'd argue that what you're missing isn't the stuff you'd find in the big book, it's the theory / technical knowledge that comes with a structured degree. But anyone can learn that stuff -- plenty of older programmers don't have CS degrees because there were no CS degrees when they were in school.
1
u/insertAlias May 09 '12
Not just older. I'm 27, no degree, working as a developer. I got lucky though. I went through a headhunting agency, took a test, and they helped place me at a company that didn't require a degree. I started out not making all that much (though far more than the menial jobs I had been working), but over the last three years I've gotten multiple raises and a promotion, all merit based.
So, it's possible, though I wouldn't recommend it. If I could time travel, I'd tell my dumbass 18 year old self to not fuck up at university.
0
May 09 '12
[deleted]
1
u/mq2thez May 09 '12
I mean, I think of algorithm analysis (speed / memory) and data structure design as important. You'll need those to understand why you use specific algorithms -- one sort over another, for example.
2
5
3
1
0
u/jaylink May 09 '12
Yes, it's a funny pic, but in reality it has more to do with Crockford's book not being that good rather than the language itself sucking. Aside from the terrible choice of using "+" as a concatenation operator, Brendan Eich did a fantastic job with Javascript. And he had, what? Two weeks?
Typically problems with JS are due to the DOM being slow and not due to JS itself. That, or people who don't know what they're doing.
3
u/yeskia May 09 '12
May I ask what is wrong with "+" being the concatenation operator?
6
2
u/Ran4 May 09 '12
"1" + "2"
Given how the web usually handles types (badly), it's easy to fuck up something and you really have to know about tons of special cases.
1
u/MrSurly May 09 '12
Results in the string "12", as it should. I know I'll get hate for this, but Perl does type coercion correctly. Instead of strongly typed variables, it has strongly typed operators. A '+' always does numeric addition, and a '.' always does string concatenation*. Most of the time, the resulting type isn't important. That can be argued as both a strength or a weakness.
* Unless you overload them, but that's an entirely different bucket of worms.
1
1
u/jaylink May 09 '12
Because it is also a mathematical operator, and so should "3" + "3" become "33" or 6? The problem shows up when using variables, as they are "loosely typed" in JS (i.e., the interpreter decides on the fly if it's numeric or alpha).
Other scripting languages solve this by using a period ( . ) for concatenation which makes it explicit. (Hmm, that looks like a boob -- not intentional).
Bonus tip: if you want to ensure that your variable is treated as a numeric value, you can just put a "+" in front of it, like so:
var x = 3; var y = 3; var z = +x + +y;
(You can also use parseInt() but the plus is simpler.)
1
u/doomslice May 09 '12
Number(y) also would work, and is more explicit and less error-prone than parseInt (having to remember to supply a radix).
1
May 09 '12
When it comes to javascript, I don't know what I'm doing, and my software still runs fast. I never understood the "javascript is so slow" idea.
1
u/bloodredsun May 09 '12
While there a lots of great bits in js the bad bits are horrendous and that is what pisses people off.
-1
u/sebzim4500 May 09 '12
This has been posted so many times... it's not even particularly funny and/or true.
3
u/cnowacek May 09 '12
How is it not true? It's a photograph of two books.
4
u/Amadan May 09 '12
It is misleading, since neither the purpose of the books nor the content nor the target audience is the same. These people explain better.
-4
-2
0
u/dogs_on_my_face May 09 '12
I suspect the good parts would have a few more pages if more people wrote JS using OO concepts.
4
u/grendel-khan May 09 '12
"JS: The Good Parts" is by Douglas Crockford, who included plenty of stuff on information hiding and inheritance. (The stuff in the book is a little longer, and includes what he considers to be a much cleaner (and more traditional) OO style bolted atop JavaScript's syntax.)
I think "the good parts" would have been a lot longer if he'd included a comprehensive DOM reference, which I'm pretty sure "the definitive guide" did.
1
u/dogs_on_my_face May 09 '12
Interesting.. do you recommend the book? I was not being literal about this book, but just posing a complaint that in general developers often do not use OO techniques in JS which to me makes JS often more difficult to read than other languages.
2
u/grendel-khan May 10 '12
I do recommend it--it's what I used when I had to learn JavaScript in a few weeks for work. (Well, learn enough JavaScript to start cleaning up a heap of nonstandard hacky code, add comments and unit tests, and so on.)
I do have to keep reminding myself that JavaScript was not the worst thing that could have happened to the internet. We could all be using VBScript, for example.
-2
0
May 09 '12
[deleted]
39
u/sheldonpooper May 09 '12
I know, I'm sorry. But there was no other subreddit where it would be appreciated.
Edit: Wow, what a username.
67
May 09 '12
/r/ProgrammerHumor would probably be the best place for it.
20
1
u/ZShock May 09 '12
I love you. Now I can be lazy at work with (almost) no hard feelings... 'CAUSE EVERYBODY WILL JOIN ME NOW, BABY.
-2
4
u/eliben May 09 '12
The real problem is, this pic has already appeared in this very subreddit, many, many times.
9
u/ZShock May 09 '12
Am I the only one who finds the idea of /r/programmingpics geekly attractive?
21
May 09 '12
49
5
u/ZShock May 09 '12
You're taking revenge, ain't you. Do you know how frustrating it is to click on a reddit link and find out that there's no subreddit? HUH?!
33
u/lobster_johnson May 09 '12
To be fair, I believe the big book contains all the browser stuff, including all the DOM APIs and differences/incompatibilities between browser implementations. That's a huge amount of material to cover that is not the fault of JavaScript-the-language.