DEV Community

loading...

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

Collapse
darkwiiplayer profile image
DarkWiiPlayer

I don't like the get prefix at all. Returning a value is the default use of a function, so it shouldn't be part of the functions name.

The word "total" could mean many things. Regarding balance, we can guess that it's the sum, but it's still better to make it clear.

The accountIndex variable should be named i. Generally, one-letter variables are bad, but i, j and a few more are so ubiquitous that they can and should be used. Every competent programmer will know what they mean.

Instead, that loop shouldn't be written like that at all. It looks like C code. Javascript and comparable languages have much nicer ways to iterate over an array and those should be used instead.

A good solution would be either

const sumBalance =
   (accounts) => accounts.reduce((acc, account) => acc+account.balance, 0)
Enter fullscreen mode Exit fullscreen mode

or

const sumBalance =
   (accounts) => accounts
      .map( (acc) => acc.balance )
      .inject( (a, b) => a+b )
Enter fullscreen mode Exit fullscreen mode
Collapse
dcwither profile image
Devin Witherspoon Author

Thanks for sharing! As far as defaults go, I try to always make the implicit into the explicit. For me that means saying get when it only returns a value. Same goes for the i value, I want my reader to do as little work as possible to understand my code. Using i can also result in referencing the wrong value when looping over multidimensional arrays. Someone who is a bit distracted may also have trouble keeping track of i and j simultaneously.

Regarding your point about the loop, I agree. Personally I don’t write loops using indices either, but many people do, and it’s the most universally recognizable for loop format. Changing the format of the loop would have obfuscated the intent of the exercise - showing how having rules or conventions can help us find better names for things.

Collapse
darkwiiplayer profile image
DarkWiiPlayer

when looping over multidimensional arrays

For two dimensions, i and j are still easy enough to follow. Starting at 3, it's very rare to not have better names resulting from context, like x, y and z for spacial coordinates, etc.

but many people do

They shouldn't. Iterating over data structures with C-style numeric for loops is a much worse habit than calling a variable data. But fair point on the intent of the exercise, sometimes we have to write "bad" code to avoid having to think up a convoluted example just to illustrate a very basic principle.

Thread Thread
cubiclesocial profile image
cubiclesocial

I've never understood why people use i and j for loop variable names. I prefer x, y, and z because they are generally used for looping spatially over an array (columns = x, rows = y, depth = z). Using x, y, and z also mirrors the Cartesian-like planes in mathematics fairly well. That is, anyone with a strong math background will understand x, y, z, and n intuitively while i and j are largely meaningless with i being used for imaginary numbers. i and j and l (lowercase L) are also the thinnest characters in many fonts, making them harder to read.

Thread Thread
drumstix42 profile image
Mark

Check out this Answer on StackOverflow:
stackoverflow.com/a/4137890/4035952

The answer is mostly because of Math, and what "i" and "j" stood for and it's pretty easy to understand how it made it's way into code. Luckily these days, simple "for" loops can be often be replaced with functional versions, or use "for...of"

Collapse
aedanobrien profile image
Aedan

While working on smaller scripts or just scribbling for yourself, I always use i, however once you work on a massive project with 1000s of lines of codes where there are multiple loops and multidimensional arrays going on, and they use i, x, z etc. it gets so confusing. It honestly never hurts to write out a word like accountIndex. It's simply good practice that has no real downside. The argument that it takes longer to write accountIndex than just i is true, however with IDEs having autocomplete this isn't a real issue. Even when writing it manually, it takes maybe 1-2 seconds to write accountIndex. While it might cost you 1-2 seconds now, you'll easily make up for it down the road when you go over that code weeks later and you immediately know what accountIndex refers to, rather than seeing the i for the 50th time. Even just scrolling through the code while working on it makes it so much more visible, saving you seconds here and there.