So I've been writing a lot of JavaScript lately. Despite what many people say I think JavaScript can be a simple and beautiful language.
What I've pondered of late is how to structure the code so it is best readable to the human eye.
Sometimes there are cases like this:
mainFunction = function(){
var init = function(){
// do some initialization
doSomeThing();
doAnotherThing();
window.addEventListener("scroll", someCallback);
}
var someCallback = function() {}
var doSomeThing = function() {
// code
}
var doAnotherThing = function() {
// code
}
init();
}
You first have to define all functions before you can initialize some thing and finally do the stuff you wanted to do.
Now when I'm reading code, I always try to read it in a chronological order first, that's just how my brain works. But with JavaScript, the longer the code gets, the more complex I find to see where the start point is.
So I guess my questions are:
- Are most people reading code chronologically? Is chronological the best way for readability?
- How to achieve this in JavaScript? (What are some languages that do it better)
- What are some common JavaScript code writing philosophies out there?
You first have to define all functions before you can initialize some thing and finally do the stuff you wanted to do.- you don't have to, if you use the function declaration statement. i think it's cleaner, to see what gets called first, and the definition below. so yes, i think chronological order is often a good choice – Bruno Schäpper Jul 21 '15 at 10:42