The first bad pattern what I have seen in this project:
name.space.classA = (function(){
function init () {...}
function createWidget() {...}
...
return {
createWidget : createWidget
}
})();
createWidget() was a kind of page processing function (which was called in init()). The big problem was there that createWidget() had code which was necessary for page loading, say it contained validation logic. Then it got exported to the public without thinking... and there is the big lie. Nobody will stop you to do that, neither in strongly typed (OOP) languages, nor in JavaScript. It's called semantical problem. Because if you export any type of function to the public, you must think about what that function does and how, and especially you have to name it right.If I call a function like createWidget I somehow think about that is a function which creates a widget on the page... and, that's all! It shouldn't contain any type of business logic, especially not of a type those contain logic about page processing! Because what happened is that function called from a context which already calculated the validation logic, besides that it fired an event that somehow called the caller function again, and there is our infinite loop...
The problem was not that I had to fix an infinite loop, but why. Because of the semantical problem. Why you make a function public which contains processing logic (so that's function is almost not a function just a reducated step of a bunch of processing lines). But if I go in deeply: why you have any kind of business logic (like validation and firing uncorrelated events), when you name a function like createWidget? It shouldn't have any side effects with that name..
The point is about what makes a programmer. If you can learn a programming language, that's good for starters, but if you see the bigger picture, you can see that it's just the top of the mountain. Programming is not about loops, conditions, classes, namespaces or statements, moreover it's about roles, responsibilites, separation and building separate -as dumb as they can- components. If you can't see what is the role of a function, or a class, or which are the burden of a component in your code then you can write spagetthi code anyways.
No comments :
Post a Comment