5 ms·
More importantly, that is some of the nastiest looking JavaScript I've seen in a while. I think MS are doing everyone a favour.
by EpaL 15y ago
More importantly, that is some of the nastiest looking JavaScript I've seen in a while.
I think MS are doing everyone a favour.
- riffraff 15y agothere _is_ something to be said for not allowing copy&paste, but I'd be more worried of poor read&type-rs :)
- loozapalooza 15y agoI am learning javascript nowadays. Can you describe, what is wrong with this javascript? So that, I won't learn any bad practice.
- cbs 15y agoWhen the code was adjusted to fit on the website they lost a lot of the formatting, and severely hurt the readability of the code. It appears that the lines of code were rather long, but were word-wrapped to fit within the size of that image, creating linebreaks at unexpected places. The first and last lines of the block open and close curly braces, but the rest of the code is not indented. Readability is something you need to be aware of, but don't worry about learning bad practice. You'll become opinionated pretty quickly on your own.
- troels 15y ago> .. but don't worry about learning bad practice. You'll become opinionated pretty quickly on your own. So true.
- troels 15y agoJust a few quick observations: The formatting is non-existant. That makes me angry. Because I have OCD. Most programmers do. There's a lot of direct use of DOM traversing, which makes the code rather brittle. (Use a css selector for binding to the DOM - jQuery is the de-facto standard tool here). Variables are not encoded in the URL. And when it is, it happens on a separate line from where it's used. That's just bad style. Oh, and what's the point of those declared-but-unused variables (`spanish, german, english`). And why aren't they in a hashmap?
- frou_dh 15y ago> That makes me angry. Because I have OCD. Most programmers do. I assume you mean fake, self-diagnosed OCD, then.
- SeamusBrady 15y agoYeah. Real OCD is no joke. It is a terrible illness. And I don't get angry over bad formatting either :)
- paul-woolcock 15y agoyes, the small, annoying personal neuroses that everyone has, but for some reason we programmers like to wear as a badge of honor.
- WesleyJohnson 15y agoI'm not the most savvy javascript developer, but I'll take a stab at it. Variable declarations and assignments happen on multiple lines in the 2nd snippet instead of all being defined at once. var currentLanguage = "en"; var spanish = "es"; var german = "de"; var english = "en"; vs var currentLanguage = "en", spanish = "es", german = "de", english = "en"; Additionally, "en" is assigned twice. If you're going to take the time to assign the "en" abbreviation into the "english" variable, then you should use it when assigning to "currentLanguage", in my opinion, if only to avoid typos and redundancy. var spanish = "es", german = "de", english = "en", currentLanguage = english; The "disclaimer" element is accessed using the documentGetElementById DOM call twice. javascript is getting faster, but forcing multiple DOM calls when it's not necessary is bad practice. Ideally, you'd access it once and save it into a reference. document.getElementById("disclaimer").firstChild.nodeValue = response; var text = encodeURIComponent(document.getElementById("disclaimer").firstChild.nodeValue); vs var disclaimer = document.getElementById("disclaimer"), text = disclaimer.firstChild.nodeValue; disclaimer.firstChild.nodeValue = response; The code formatting is poor and inconsistent, notice an incorrect space after "encodeURIComponent (", but no spaces after "getElementById(" and "getElementsByTagName(". Also notice no indentation anywhere, or line-breaks after the function signatures and opening brackets "{"; The code is polluting the global namespace by not being enclosed in its own namespace or perhaps an immediately executed anonymous function. I'm sure more seasoned JS devs can list some more things wrong with the code, but those are the ones that stood out to me.
- ward 15y agoI don't see a bunch of variable definitions on one line as an improvement, just reads less easily and only feels justified in the case of some low numbers like var x = 0, y = 1, z = 0; // Some looping or similar here I do agree with your using of `english` variable to assign `currentLanguage`.
- WesleyJohnson 15y agoPersonally, I like doing all the declarations on their own line as well. But anytime I've used JSLint in the past, I seem to recall it complaining about that. Perhaps I'm mistaken?