Programing Elegance vs Simplicity

By Xah Lee. Date: . Last updated: .

here's 3 styles of coding. which is the best?

console.log( typeof undefined === "undefined" );
console.log( typeof "abc" === "string" );
console.log( typeof true === "boolean" );
console.log( typeof false === "boolean" );
console.log( typeof 3 === "number" );
console.log( typeof NaN === "number" );
console.log( typeof Infinity === "number" );
console.log( typeof {} === "object" );
console.log( typeof [3,4] === "object" );
console.log( typeof (new Date()) === "object" );
console.log( typeof /x/ === "object" );
console.log( typeof JSON === "object" );
console.log( typeof Math === "object" );

// all true
console.log(
 typeof undefined === "undefined" &&
 typeof "abc" === "string" &&
 typeof true === "boolean" &&
 typeof false === "boolean" &&
 typeof 3 === "number" &&
 typeof NaN === "number" &&
 typeof Infinity === "number" &&
 typeof {} === "object" &&
 typeof [3,4] === "object" &&
 typeof (new Date()) === "object" &&
 typeof /x/ === "object" &&
 typeof JSON === "object" &&
 typeof Math === "object"
);

// all true
console.log (
[
 [undefined , "undefined" ],
 ["abc" , "string" ],
 [true , "boolean" ],
 [false , "boolean" ],
 [3 , "number" ],
 [NaN , "number" ],
 [Infinity , "number" ],
 [{} , "object" ],
 [[3,4] , "object" ],
 [(new Date()) , "object" ],
 [/x/ , "object" ],
 [JSON , "object" ],
 [Math , "object"]
] . every ( ((x) => typeof x[0] === x[1] ) )
);
// true

back in 90s, i'd absolutely strive for last. Today, actually, i think the first is best. It's is the most simple, clear, easy to understand.

Programing Idioms and Style

  1. Programing Elegance vs Simplicity
  2. Why Idioms Are Bad
  3. The Nature of Idioms in Programing Languages
  4. Hashtable as Switch Statement
  5. Logic Operator as Control Flow

If you have a question, put $5 at patreon and message me.