Javascript when to split vs when to nest
I was wondering what the community of javascript developers say about when
to nest vs when to split code
Essentially lets say you are running some code within an IIFE
Why is it that a lot of times I see things like
(function(context) {
context.bang = {
someTing: (function() {
//a lot of code
}),
someTingElse: (function() {
//a lot of code
})
}
}(this));
vs
(function(context) {
function doSomething() {
// a lot of code
}
function doSomethingElse() {
// a lot of code
}
context.bang = {
someTing : doSomething,
someTingElse : doSomethingElse
}
}(this));
I find the second piece of code much more readable, and if you want it
compressed send it through something like google's closure compiler which
will take the second version of the code and condense it down to
essentially the first + tricks you didn't think of. Code is to be
maintained by people, optimized by compilers, and run by applications.
No comments:
Post a Comment