News for hackers. Stuff that matters.
Re:Code snippets on Microsoft website shown in form of images. by EpaL on Wednesday December 07, 2011 @08:05AM (#3323572)
More importantly, that is some of the nastiest looking JavaScript I've seen in a while.

I think MS are doing everyone a favour.

Re:Code snippets on Microsoft website shown in form of images. by loozapalooza on Wednesday December 07, 2011 @08:32AM (#3323642)
I am learning javascript nowadays. Can you describe, what is wrong with this javascript? So that, I won't learn any bad practice.
Re:Code snippets on Microsoft website shown in form of images. by cbs on Wednesday December 07, 2011 @09:31AM (#3323890)
When 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.

Re:Code snippets on Microsoft website shown in form of images. by troels on Wednesday December 07, 2011 @09:46AM (#3323943)
> .. but don't worry about learning bad practice. You'll become opinionated pretty quickly on your own.

So true.

Re:Code snippets on Microsoft website shown in form of images. by WesleyJohnson on Wednesday December 07, 2011 @09:48AM (#3323951)
I'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.

Re:Code snippets on Microsoft website shown in form of images. by ward on Wednesday December 07, 2011 @11:17AM (#3324391)
I 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`.
Re:Code snippets on Microsoft website shown in form of images. by WesleyJohnson on Wednesday December 07, 2011 @01:09PM (#3324986)
Personally, 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?
Re:Code snippets on Microsoft website shown in form of images. by masklinn on Wednesday December 07, 2011 @01:14PM (#3325020)
I expect JSLint was whining about using multiple `var` statements.

Using a single var and putting each declaration on its line is not incompatible:

    var foo = 4,
        bar = 5,
        baz = 6,
        qux = 'quux;
Re:Code snippets on Microsoft website shown in form of images. by ward on Wednesday December 07, 2011 @01:36PM (#3325130)
That feels like a situation that could easily end up (copy pasting something, someone adding another var, ...) in an unexpected global variable declaration.

    var foo = 4,
        bar = 5,
        baz = 6,
        sprinkles = 'wee';
        qux = 'quux';
Making `qux` global.
Re:Code snippets on Microsoft website shown in form of images. by mattmanser on Wednesday December 07, 2011 @11:38AM (#3324494)
A lot of developers don't like declaring more than one variable on one line, that's a style choice.

There's nothing wrong with it, especially in JS where accidentally deleting a variable declaration can suddenly turn your variable global.

The problem with the code is more that the code stinks, even after you remove the formatting problems, it looks more like .Net code and it doesn't make any sense.

Re:Code snippets on Microsoft website shown in form of images. by troels on Wednesday December 07, 2011 @09:42AM (#3323927)
Just 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?

Re:Code snippets on Microsoft website shown in form of images. by frou_dh on Wednesday December 07, 2011 @09:59AM (#3323989)
> That makes me angry. Because I have OCD. Most programmers do.

I assume you mean fake, self-diagnosed OCD, then.

Re:Code snippets on Microsoft website shown in form of images. by paul-woolcock on Wednesday December 07, 2011 @01:36PM (#3325131)
yes, the small, annoying personal neuroses that everyone has, but for some reason we programmers like to wear as a badge of honor.
Re:Code snippets on Microsoft website shown in form of images. by riffraff on Wednesday December 07, 2011 @08:16AM (#3323600)
there _is_ something to be said for not allowing copy&paste, but I'd be more worried of poor read&type-rs :)