Live data from Hacker News

Maintainable JavaScript (2014)

alexkras.com

11–20 of 33 posts

Re: Maintainable JavaScript (2014)

#11

He uses the following example for when to throw your own errors: 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…

Probably easier to just use an assertion library here. E.g.

  assert.ok(element)

Re: Maintainable JavaScript (2014)

#15
post #11

He uses the following example for when to throw your own errors: 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…

Probably easier to just use an assertion library here. E.g. assert.ok(element)

Even easier- typescript or flow

Re: Maintainable JavaScript (2014)

#17
post #11

He uses the following example for when to throw your own errors: 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…

Probably easier to just use an assertion library here. E.g. assert.ok(element)

I wrote a library [1] and a babel plugin [2] (bonus point: is Flow compatible) in order to deal with those cumbersome runtime type checks.

[1] https://github.com/gcanti/tcomb [2] https://github.com/gcanti/babel-plugin-tcomb

Re: Maintainable JavaScript (2014)

#20
The code examples use ==, which does lossy comparison. Using === is much safer. The tools section mentions JSLint which flags any use of == as an error.

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.

Post reply on HN