Poor Javascript Coding Quality
Wednesday, 18. June 2008, 16:31:09
What is it with Javascript programmers these days? Doesn't anybody know how to program?
I was investigating why tinyMCE 2 (.1.3) is failing in Opera in some circumstances, and what did I find?
Exhibit 1:
Exhibit 2:
It doesn't matter how other browsers somehow manage to avoid these obvious bugs - this is just plain crappy coding.
The worst thing is, this is typical of the sort of code that makes the web go. It's a wonder it works at all.
I was investigating why tinyMCE 2 (.1.3) is failing in Opera in some circumstances, and what did I find?
Exhibit 1:
if (tos.getEditorTemplate) editorTemplate = tos.getEditorTemplate(this.settings, this.editorId); deltaWidth = editorTemplate.delta_width ? editorTemplate.delta_width: 0;The if statement is there for a reason - to initialize editorTemplate to something useful - but what happens if the statement is false? editorTemplate does not get initialized. Look at the following statement - the potentially uninitialized editorTemplate is used! That would generate an error in any browser.
Exhibit 2:
getRng: function() {
var s = this.getSel();
if (s == null) return null;
if (tinyMCE.isRealIE) return s.createRange();
if (tinyMCE.isSafari && !s.getRangeAt) return '' + window.getSelection();
if (s.rangeCount > 0) return s.getRangeAt(0);
return null
},
isCollapsed: function() {
var r = this.getRng();
if (r.item) return false;
return r.boundingWidth == 0 || this.getSel().isCollapsed
},getRng is obviously capable of returning null. The very next function, isCollapsed, calls it and immediately uses the returned value without checking for null. That will generate an error in any browser.It doesn't matter how other browsers somehow manage to avoid these obvious bugs - this is just plain crappy coding.
The worst thing is, this is typical of the sort of code that makes the web go. It's a wonder it works at all.