Вопрос

I was running our code through JSHint, and decided to switch checks against cyclomatic complexity, and then went on long refactoring sprint. One place though baffled me, here is a code snippet:

var raf = null //raf stands for requestAnimationFrame

if (window.requestAnimationFrame) {
  raf = window.requestAnimationFrame;
} else if (window.webkitRequestAnimationFrame) {
  raf = window.webkitRequestAnimationFrame;
} else if (window.mozRequestAnimationFrame) {
  raf = window.mozRequestAnimationFrame;
} else if (window.msRequestAnimationFrame) {
  raf = window.msRequestAnimationFrame;
} else {
  raf = polyfillRequestAnimationFrame;
}

Not surprisingly, in this implementation CM is 5, first attempt was to use solution from MDN:

var requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame ||
                          window.webkitRequestAnimationFrame || window.msRequestAnimationFrame;
window.requestAnimationFrame = requestAnimationFrame;

Which simply looks like a hack to me (yes I know, majority of full time javascript programmers won't agree with me, however this is a prevalent opinion within our team). Doodling around my code, I found another hacks I could employ to full code-linter, among ones I was proud for about 5 second was usage of array comprehensions:

var rafs = [
             window.requestAnimationFrame,
             window.webkitRequestAnimationFrame,
             window.mozRequestAnimationFrame,
             window.msRequestAnimationFrame,
             polyfillRequestAnimationFrame
           ].filter(function (rafClosure) {
             return rafClosure !== null && rafClosure !== undefined;
           });
return rafs[0];

However I'm curios whether there is more or less standard practice of refactoring long branching code (that's not trivial to reduce)?

Нет правильного решения

Лицензировано под: CC-BY-SA с атрибуция
scroll top