by Drakim · 12y ago · 0 comments
Ultimately, if naked JS is too difficult to manage due to the lack of types and interfaces, I much prefer to use another language that translates to JS, rather than attempting to force it onto the JS language, because it always becomes messy and half-baked.
http://discuss.joelonsoftware.com/?joel.3.219431.12
Although it's ranting about Java frameworks rather than JavaScript a lot of the complaints look rather familiar. Having said that, I don't think anything in the JS world has reached the baroque complexity once regarded as normal in the enterprise Java world.
[NB Note my comments on that discussion - I had not long finished rescuing a monster of a Java enterprise application where the simplest thing needed to go through about 30 layers of abstraction (I did actually count them) from a page being submitted to the underlying message being sent over MQ - it seemed to be a point of honor to combine every feature of J2EE with every design pattern to do the simplest things. Having multiple huge jar files in the classpath each 97% identical to the other just added to the sense of adventure when you clicked on something.]
http://ws.apache.org/xmlrpc/apidocs/org/apache/xmlrpc/server...
I know it's a bit unfair to pick on that particular document - but I'm worried as a long recovered abstraction addict to what degree I actually thought things like that were ever a good idea.
The stuff I hate most is the boilerplate that means you find crappy little singletons everywhere when all that was needed was a function. And for the love of god, a function that's named and not pointlessly assigned by a var because you're mono-linguistic and haven't realised yet what the actual point of functions are yet.
Huge chunks of the javascript in the web site I've taken over were done by someone who loved these patterns, this is an example of his favourite one, pointless and led to colossal code bloat as he used it over and over for basic handlers.
function instantSearch() {
var instantSearchObject = {
config: {
animSpeed: 250
},
init: function () {
this.findComponents();
this.bindEvents();
},
findComponents: function () {
this.searchBlockCollection = $('.search, .entry-search');
this.searchInputCollection = this.searchBlockCollection.find('input:text');
},
bindEvents: function () {
var self = this;
this.searchInputCollection.on({
'focus': function () {
var currentInput = $(this),
currentSearchSuggestionsBlock = currentInput.parent().siblings('.search-suggestions');
currentSearchSuggestionsBlock.slideDown(self.config.animSpeed);
},
'blur': function () {
var currentInput = $(this),
currentSearchSuggestionsBlock = currentInput.parent().siblings('.search-suggestions');
currentSearchSuggestionsBlock.slideUp(self.config.animSpeed);
}
});
}
}.init();
}
Nigh on 30 lines of code, so few lines that actually do something. I tend to refactor them when I can so I can see what they're actually doing, the following code should do exactly the same thing (without me testing it): function bindSearchBox() {
$(".search input, .entry-search input")
.on("focus", function () {
$(this).siblings('.search-suggestions').slideUp(250);
})
.on("blur", function () {
$(this).siblings('.search-suggestions').slideDown(250);
});
}
Hmmm, even just refactoring that tiny bit of code shows that there might even be a bug there, or at least pointless code, I'm not sure why he's sliding the search results up on focus, they don't stay open.Thus they went and tried to "fix" JavaScript by trying to make it more Java like and thus unleashed abominations such as Dojo or Prototype.js upon the world.
God have mercy on their souls.
Now we just have to define "var global = (function() {return this;}).apply(null);" and we have a reference pointing to the global object! Pure magic! Really? Are you serious? Sorry to hear so. (No, I was not suggesting to use some kind of more elaborate pattern for this. See, there is "self" ...)
(When ever you see "var self = this;", take your things and run.)
AFAIK in all the c-style languages `this` is the usual self reference. Python & Ruby are self. VB.Net is Me.
But javascript screwed `this` up and this is especially apparent when you use events where `this` ends up being the caller instead of the callee. Having a self reference in the closure allows you to fix the problem.
I think people ended up using self for a reason, it's strange enough in a C-style language that you're not expecting it to be anything, but familiar enough to be obvious. The other common ones over the years have been `_this` & `that`.
There are patterns in javascript and patterns. We need Crockford to write, "Javascript Patterns: The Good Ones".
Android activities/fragments = a framework.
WinForms = a framework.
ncurses = a framework.
Unity / Unreal3 = a framework.
Not sure if that makes it a framework, though.
Application development w/ Flex is a big exception to that.
Does that statement actually mean anything? I don't see how it's anything but nonsense.
Also note that AngularJS (which is what's being discussed) is a library.
"AngularJS fits the definition of a framework the best, even though it's much more lightweight than a typical framework and that's why many confuse it with a library."
https://docs.angularjs.org/misc/faq
[NB I was curious as I haven't used Angular and I thought it was a framework and I wanted to check to see if I'd got things completely wrong!]
AngularJS might exhibit some of the trappings of a framework (e.g. inversion of control), and it has some of the components a front-end framework would need: routing, templating, networking... but these components aren't tied together in a way that impresses an overall architecture. Calling Angular a framework leads developers to assume architectural decisions have been made when they haven't.
From their own project page: "AngularJS — Superheroic JavaScript MVW Framework" (empasis mine).
You have to code your logic inside its objects (controllers, directives, services, etc) -- that's not how a library works.
Even internally they're insecure about it. I think library is a more useful definition because it encourages developers to think about architecture before they start building application specific stuff.
AngularJS is much better than what we had before, but it is still not great, and a bit clunky .. hopefully the lessons will be learned and in the next cycle we'll get some nicer slimline things instead (though this cycle could take a few more years to run through).
The article comes off as whiny without really giving any argument for the viewpoint other than essentially "this pattern sucks because I say so"
The difference between a framework and a library, IMO, is control.
You forfeit control to the framework in exchange for less code. But then you depend on it for your every feature.
Also, when the next big framework comes along that promises you even less code writing. It usually is non-trivial to make them work together, if at all. you just might end up with a legacy framework on your hands or face a complete rewrite.
Disclaimer, I work with legacy code written on top of JSF 1.2 and Richfaces 3. It probably seemed more organized and modular at the time. It's not.