The World’s Largest Online Community for Developers

'; functional programming - What is the name of this coding style? - LavOzs.Com

In the past years I have oftener stumpled upon Javascript code that reminded me of the old "Perl" programming days and used boolean terms over if/else clauses. This usually resulted in shorter code with higher complexity per line (and sometimes unnecessary default value creations or empty loops).

I do NOT want to open a discussion on whether this is good or bad practice :-)

But I am curious if there is a name for this compact, "boole-ish" style?

Long / rather "verbose" / lower complexity per line:

function sum(a) {

  if (!Array.isArray(a)) {
    // No array
    return 0;

  if (a.length === 0) {
    // Empty
    return 0;

  var sum = 0;

  for (let i = 0; i < a. length; i++) {
    sum += a[i];

  return a;

Similar result, with ternary evaluation and ES5 "functional" programming:

// Would we call this "functional" programming? Not sure.

const sum = (arr) => (
  Array.isArray(arr) ? arr.reduce((before, value) => before + value, 0) : 0

Even shorter, with boolean evaluation:

// How to call this (professionally)?

const sum = (arr) => (
  (arr || []).reduce((before, value) => before + value, 0)

Note in the last example that the code is the shortest for the human reader, but that it may also create an "unnecessary" array and even evaluate empty loops, i.e. probably more memory consumption and more processing effort.

Just a quick hack for demonstration purposes, no real-world example!