question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Suggestion: Prefer iterators dependent on the variable's name

See original GitHub issue

I figured out that for most complex code structures where I need nested loops due to performance, I can more easily read them once I chose to use iterators based on the variable’s (object’s or array’s) name. I typically use the leading character as the iterator (e.g. dataset => d) and a trailing l to imply the length (e.g. dataset => dl).

While I know that functional programming is easier to read (e.g. using Object.values(dataset).forEach((chunk, c) => {}) it’s hardly avoidable in timing-relevant methods and complex sorting algorithms that need performant behaviours (e.g. an update or render loop in a game engine).

Anyways, here’s an example. Let me know whatcha think.

BAD example

const dataset = [{ foo: [1,2,3,4], bar: [1,2,3,4] }];

for (let i in dataset) {
    let chunk = dataset[i];
    for (let j = 0, l = chunk.length; j < l; j++) {
        console.log(chunk[j]); // you get the point
    }
}

GOOD example

const dataset = [{ foo: [1,2,3,4], bar: [1,2,3,4] }];

for (let d in dataset) {
    let chunk = dataset[d];
    for (let c = 0, cl = chunk.length; c < cl; c++) {
        console.log(chunk[c]); // you get the point
    }
}

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:1
  • Comments:12 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
vsemozhetbytcommented, Jan 6, 2017

It may be not absolutely matched with Avoid Mental Mapping (and Don’t over-optimize). Maybe something like datasetPart and chunkElement?

And I don’t know if this is in the book (I have not read it, unfortunately). Do the repository creators accept tips not connected with the book directly?

0reactions
ProLosercommented, Jan 19, 2017

What about dataItem instead of d? I’m still not sure what part of your example is the lazy caching.

I do agree about too many variables with bad names, but I feel like that’s a symptom of overall a bigger problem with your code organization and changing from i to d isn’t really going to be where the real impact could be had.

Read more comments on GitHub >

github_iconTop Results From Across the Web

What is an ideal variable naming convention for loop variables?
I always use a meaningful name unless it's a single-level loop and the variable has no meaning other than "the number of times...
Read more >
5734 - inline rename - hit ENTER accepts suggestion not what ...
I found an annoying behavior of the inline rename. If I attempt to rename a variable, e.g. "iter" in code below, it automatically...
Read more >
Solved: Inline model variable substitution - Esri Community
I would like to be able to select a number of input soil shapefiles, clip each according to the same clip features, then...
Read more >
Iterators: an ADT for Positions
In this lesson we introduce one of the fundamental building blocks of the C++ standard library: the iterator. Concept is simple.
Read more >
Naming Conventions for these STL Reference Pages
These are generic variable names used for the default type of iterator (or reverse_iterator) for a given container. For example, when used in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found