Live data from Hacker News

Code snippets on Microsoft website shown in form of images.

msdn.microsoft.com

21–30 of 58 posts

Re: Code snippets on Microsoft website shown in form of images.

#22
post #9

More importantly, that is some of the nastiest looking JavaScript I've seen in a while. I think MS are doing everyone a favour.

I am learning javascript nowadays. Can you describe, what is wrong with this javascript? So that, I won't learn any bad practice.

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.

#23
post #20

Earlier quoted context omitted.

I am learning javascript nowadays. Can you describe, what is wrong with this javascript? So that, I won't learn any bad practice.

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 y…

> .. 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.

#24
post #9

More importantly, that is some of the nastiest looking JavaScript I've seen in a while. I think MS are doing everyone a favour.

I am learning javascript nowadays. Can you describe, what is wrong with this javascript? So that, I won't learn any bad practice.

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.

#25
Since the page is all about translation, could it be to avoid online translators (like Google Translate) to attempt to read that code and translate it since you expect many of the visitors to the page coming from a locale different than English?

Or, does Google Translate etc. handle this and not attempt to 'translate' code?

Re: Code snippets on Microsoft website shown in form of images.

#26
post #22

Earlier quoted context omitted.

I am learning javascript nowadays. Can you describe, what is wrong with this javascript? So that, I won't learn any bad practice.

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…

> 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.

#28

I would like to see how long it stays that way ... people make mistakes, but if you are on the front page of HN for a wrong reason, you leave everything aside and fix it.

I doubt the editors or developers of the MS Translator even know what HN is. I know... it's shocking.
Post reply on HN