3 ms·
> The only possible reason for not doing so is that you're using an editor that makes you type it every single time. Here's some C: for (int player_index
by snprbob86 15y ago
> The only possible reason for not doing so is that you're using an editor that makes you type it every single time.
Here's some C:
for (int player_index = 0; player_index < players.length; player_index++) {
int rank = player_index + 1;
render_score(rank, players[player_index].name);
}
Blah. So much for the brain to parse!
for (int p = 0; p < players.length; p++) {
int rank = p + 1;
render_score(rank, players[p].name);
}
I find that much, much easier to parse in my head. The definition of p is available without any backtracking. The word "player" appears five fewer times, reducing confusion with the players array. This is a small case.
If this code were to start to grow, and the definition of p and its usages start to grow appart, it's trivial to replace.... but it's probably better for you to refactor.
- lemming 15y agoThat's kind of a pathological case you have there - I don't think many people would argue that loop indices should have long variable names. None of the examples in your post above were loop variables, and what I see a lot of in lisp code is using ps for players, for example. In your example you've used pretty sensible names for players, rank and render_score - why not ps, r and rs respectively?
- snprbob86 15y ago> why not ps, r and rs respectively? Um... Because that would be hard to read? I'm advocating using full words in general, avoiding long variables (multi-word phrases, adjectives, contextualization, etc), and not being afraid of short variables names if they help, rather than hurt readability. There are plenty of cases where short names are totally fine. If you've got a module full of string utilities and every single function takes a first argument of type String, why not just call it "s"? Everyone will figure it out in seconds and it will save the mental overhead of differentiating "String" and "string". when that does come up. Things like that; I'm saying use your judgement.