Front End Development Guidelines
taitems.tumblr.com
taitems.tumblr.com
The Google JavaScript Style guide recommends single quotes over double quotes for this reason:
http://google-styleguide.googlecode.com/svn/trunk/javascript...
I'm yet to see a compelling argument for either. My original paragraph for string creation forgave C style strings, as they do seem very clean cut and easy to identify. I think this is an area that needs some debate.
But the same goes for something like "you can't do that"
I guess it just depends what you don't want to escape. For Google, the more probable scenario was probably the former.
In C: single quote = character, double-quote = easy initialization of array of characters terminated with \0.
(the above contains many simplifications, of course)
Or you could, you know, just make code that's understandable.
I'm such an Internet jerk.
Often this means that if I don't leave myself a reasonable comment about what I'm doing in that spot and why, I'll relearn the importance of good comments later.
Maintainability is quite often the most important thing when it comes to coding not terseness (which many do not consider to be elegant at all, quite the opposite, they see it as unnecessarily complicated and obtuse).
I mean, you should comment your code, but Javascript doesn't make me think of "regular" commenting.
EDIT: The other response is more accurate.
THIS is why I hang out here. :) Kudos..
- At the end of the "Always Use === Comparison" section, the example will throw a ReferenceError if (as is suggested) the variable `bar` has not been declared. If you're testing a variable that may not have been declared, you either need to use a `typeof` check or declare bar using `var bar;`, which will be essentially a no-op if bar exists and declares it with a value of `undefined` otherwise.
- The comments example is demonstrating unnecessary comments. The check and the call in
if (zeroAsAString === 0) { doHeapsOfStuff(param1, param2) }
... are self-explanatory to even the an inexperienced developer and do not require comment. What may require comment is why the code performs this check and this call.
- Your array in the "Loop Performance - Use ‘break;’ & ‘continue;’" section has one element. I know it's just an example, but it would be better as an example if it worked. You could instead use
var bigArray = new Array(1000);
- Chaining has downsides, the main one being it makes debugging much harder. You might want to mention this.
Personally I disagree. In my code-bases it's:
functions: doSomeThing()
constructor: new Foobar()
object/array/primitive: some_object
"constants": SOME_VALUE
I find that much more readable, especially when looking at code completion in the IDE, I can always tell whether a property is a method or not.
For example, you might have an object foo that has a property called isBar. I like being able to tell that that is a function that returns a true or false value, as opposed to is_bar which just holds the boolean value.
This example snippet actually generates a Javascript error (at least, in IE9):
var foo = null;
// foo is null, but bar is undefined as it has not been declared
if (foo == null && bar == null) {
// still got in here
}
It will work if bar is declared but has value undefined, but not if bar is completely undeclared as shown. typeof bar === 'undefined'1. This is the web. Users will resize the window, copy and paste, click a button five times, turn off JavaScript, and arrive from every possible combination of browser, operating system, and language. You will maintain an appropriate fear of any trick so complicated that you don't know what it will do under all of those circumstances.
2. The user is always right. Even when they run unpatched IE7 with JavaScript and CSS on but images hidden.
Then a few pointers I would like to add:
- Think twice before binding to every instance of an event. Are you looking for a keydown, a mouse move, or a scroll? Throttle your function to make sure it runs only ten times a second, or whatever is appropriate. Ironically, the faster browsers and JS environments become, the slower your pages will be if you calculate something on all 1000 scroll events per second.
- For heaven's sake try to avoid absolutely positioning your whole interface. But if you do, you better try it in every browser and resize it real fast to see if it works.
I was keen to write a foreword explaining that I am self-taught, so there are going to be shortcomings in my theory knowledge - as I never did a CS degree. It was to mitigate the shread-tearing I expected to receive in the comments section of HN ;)
if (isSelectable === true) { ... }
versus:
if (isSelectable) { ... }
It's a shorthand method, similar to why you would use int++; instead of int = int + 1;
Good article, bookmarked it for reference/a checklist.