I have two friends that have completely different schools of thought on how to lay out their code.
The first says that code should be well-indented and use lots of spaces and to name variables judiciously in order to organize things in a way so that you can immediately understand the hierarchy and semantics of the code.
Here's a sample of what his code looks like:
var Stack = function () {
var pair = function ( first ) {
return function ( second ) {
return function ( dispatcher ) {
return dispatcher ( first ) ( second );
};
};
};
var getFirst = function ( pair ) {
return pair ( function ( x ) {
return function ( y ) {
return x;
};
} );
};
var getSecond = function ( pair ) {
return pair ( function ( x ) {
return function ( y ) {
return y;
};
} );
};
var emptyStack = function () {
return function () {
return null;
}
};
var stack = emptyStack ();
var self = {
push: function ( item ) {
stack = pair ( item ) ( stack );
return self;
},
pop: function () {
var top = getFirst ( stack );
stack = getSecond ( stack ) || emptyStack ();
return top;
},
top: function () {
return getFirst ( stack );
}
};
return self;
};
The second says that it's better to have your code written in a more compact way so that you can see a bigger part of the picture at once and be able to recognize more immediately how one part of the program will affect another.
Here's the same program written in his style:
var Stack=function(){
var pair=function(x){return function(y){return function(f){return f(x)(y)}}};
var fst=function(p){return p(function(x){return function(y){return x}})};
var snd=function(p){return p(function(x){return function(y){return y}})};
var empt=function(){return function(){return null}};var stk=empt();
var slf={push:function(val){stk=pair(val)(stk);return slf},
pop:function(){var top=fst(stk);stk=snd(stk)||empt();return top},
top:function(){return fst(stk)}};return slf}
I personally prefer writing code the first way, but I haven't really tried the second way much, so maybe I'm just not used to it.
I have noticed that when I look at the Javascript code of most professional websites, they usually use the second style, so there might be something to it.
Other than just subjective stylistics preferences, is there a reason to prefer one over the other?