Maintainable JavaScript (2014)
alexkras.com
alexkras.com
Of course in practice ...
This is def a lot better than what I currently use in production: Handlebars hacked to the point of almost being JavaScript but not really. Then you both have views with too much logic and they're annoying to implement.
At least with React you get the full expressiveness of JS right in your views.
It's no different than $(<p>) in jQuery, so unless you're referring to some pre 2007 attitude, what are you talking about?
var Controller = {
addClass: function(element, className) {
if (!element) {
throw new Error("addClass: 1st argument missing.");
}
element.className += " " + className;
}
};
I don't think this is a very good as a native error will have all this information already in a stacktrace, and if you're running Chrome dev tools with 'Pause on exceptions' then you'll be shown the exact place this fails. Additionally, you need to now keep the function name in sync with the string (your IDE / build tool will not tell you if they get out of sync).Better cases for custom errors are situations where a native error will not be thrown, such as:
- in his example method: if className is undefined
- valid objects in a state you don't expect
- switch statements that don't match any expected case
- etc.
assert.ok(element)Second, how is that different from writing in a statically-typed language like Haskell/Rust/what have you and then compiling down to assembly which is for all intents and purposes dynamically typed? We do it hoping to gain safety from the typing, but do we lose the type safety in the machine code? (We don't, the type-safe language rules out compiling to certain classes of erroneous code.)
[1] https://github.com/gcanti/tcomb [2] https://github.com/gcanti/babel-plugin-tcomb
Not so much "javascript" specific advices here.
No words on ES6 classes, that help keeping a meaningfull syntax, rest parameters, template strings.
No words on nodejs callback as last arg best practice.
No words on generator & promises (nor async await) that totally change callback behavior. No word on synchronious throw / catch vs callback(err) pattern.
No words on code modularisation.
This video is 4 years old, believe we, javascript change, a lot (and the "maintainable" best practice)
No link to mozilla MDN, when it's now a de-factor standard for web & js documentation.
The 4 most important things for me to keep under control are:
1) Length
2) Cyclomatic complexity
3) Shared mutable state
4) Coupling
If you keep those under control, the rest of the maintainability will come by itself. e.g: testability, reusability, thread safety, security and other high level goals are easier when these low level requirements are met.
And, of course, consistency. Make sure that things do what they say they do. e.g: make sure the purpose of a variable or function can be explained only through its signature without requiring to look at the code.
JavaScript is such a quirky language that I always want the code to pass JSLint and Closure Compiler with 100% type annotation. I simply do not feel confident about the code otherwise.
I'm surprised about the lack of space after 'if'. 'if' is not a function so 'if(' looks weird.
I'm way too young to be a grumpy old man, but I kind of wish people had just decided to learn how to use Make instead of re-implementing it over and over.