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.
Edit:
though not apparent in this example lets just keep with the trend of putting children functions within children functions, Also I don't care about whats more scoped then something else within my iife as long as nothing creeps back out into context
but along the same trend the end result of most actual final code I see is like
(function(context) {
context.bang = {
someTing: function() {
return {
another: function() {
return {
yetAnotherFunction: function() {
return true;
}
}
}
};
}
}
}(this));
where each function level deep is more then just the one or two line functions im making here rather then
(function(context) {
function yetAnotherFunction() {
return true;
}
function another() {
return yetAnotherFunction;
}
function someTing() {
/*blahblahblah*/
return another;
}
context.bang = {
someTing: someTing
}
}(this));