Skip to content

Latest commit

 

History

History
52 lines (39 loc) · 935 Bytes

no-loops.md

File metadata and controls

52 lines (39 loc) · 935 Bytes

Forbid the use of loops

Loops, such as for or while loops, work well when using a procedural paradigm. In functional programming, recursion or implementation agnostic operations like map, filter and reduce are preferred.

Fail

const result = [];
const elements = [1, 2, 3];

for (let i = 0; i < elements.length; i++) {
  if (elements[i] > 2) {
    result.push(elements[i]);
  }
}

for (element in elements) {
  result.push(element * 10);
}

for (element of elements) {
  result.push(element * 10);
}

while (n < 100) {
  result.push(n);
  n *= 2;
}

do {
  result.push(n);
  n *= 2;
} while (n < 100);

Pass

const elements = [1, 2, 3];

const result = elements.filter(element => element > 2);

const result = elements.map(element => element * 10);

function doubleThemAll(n) {
  if (n >= 100) {
    return [];
  }
  return [n].concat(doubleThemAll(n * 2));
}
const result = doubleThemAll(n);