Search in the blog:

2024-02-21

Loop counter name: "t" vs "i"

It is common in programming to use the variable name "i" as a loop counter. This is an old practice with roots in mathematics (using "i" as index notation, index of summation, etc.). The practice was played up in early versions of Fortran, where the default implicit typing rule is that if the first letter of the name is I, J, K, L, M, or N, then the data type is integer, otherwise it is real.

Of course, when I started programming, I also used the name "i". But if you look at some of my old code, you will see a deviation from the rule.

For example, Tonigy (C language, 2001-2002):




w95k_tvh (C language, 2002):




malykh-geo-ep (Java language, 2009-2010):




Yes, the name "t" is used instead of "i". And there was a reason. Back in the 1990s, my keyboard broke: I had to press the "i" key hard to close the switch. It was very annoying, but I had no money to repair or replace the keyboard right away.

So I tried to minimize the use of the "i" key. And I chose "t" for the variable name (I assumed it meant "temporary").

Although I replaced the keyboard after a while, the habit persisted for a long time.

Today, I don't use the name "t" as a loop counter. Modern programming languages allow direct enumeration of entities without an index. Therefore, the name of the loop variable should be related to the entity type. And only for a simple index (in a clear case of use) I write the name "i".


(some Scala code of SZ Viewer)

0 comments:

Post a Comment

Blog Archive