DEV Community

loading...

Discussion on: Stop Using "data" as a Variable Name

Collapse
josefjelinek profile image
Josef Jelinek

accountIndex is hard to read because the reader really needs to read it... for i there is instant recognition in the brain.

  for (let accountIndex = 0; accountIndex < accounts.length; accountIndex++) {
    totalBalance += accounts[accountIndex].balance;
  }
Enter fullscreen mode Exit fullscreen mode

vs.

  for (let i = 0; i < accounts.length; i++) {
    totalBalance += accounts[i].balance;
  }
Enter fullscreen mode Exit fullscreen mode

fortunatelly, there is a superior alternative (without going into Array methods):

  for (const account of accounts) {
    totalBalance += account.balance;
  }
Enter fullscreen mode Exit fullscreen mode
Collapse
drumstix42 profile image
Mark

Yeah i think it's important to use words where it makes sense, but sometimes the syntax gets lost in long lines because of longer words. For variables, meaningful names are important, but I agree in things like loops, it's better to use alternative methods or function methods instead! +1 for "for...of"