Suggestion: Prefer iterators dependent on the variable's name
See original GitHub issueI 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:
- Created 7 years ago
- Reactions:1
- Comments:12 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
It may be not absolutely matched with Avoid Mental Mapping (and Don’t over-optimize). Maybe something like
datasetPart
andchunkElement
?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?
What about
dataItem
instead ofd
? 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
tod
isn’t really going to be where the real impact could be had.