Q: Why do people use variable names like "i" and "j" for loop counters? I thought that you were supposed to use longer, desciptive names!
A1: Fortran (
FORmula
TRANslator) was one of the prevalent programming languages in the 1960s and 1970s, at a time when many programming techniques, algorithms and methodologies were being developed, and in Fortran, variable names starting with I J K L M and N were implicitly integer, and all others (A to H and O to Z) implied float. So it was natural to START your loop variables - which tended to be integers as they counted up through discrete array members - with one of these default letters.
A2: Loop variables are set up and used within a very restricted (small) area of your program, but are intensively referred to within that area. Giving them a long name which you have to keep repeating leads to code that's needlessly stretched and can actually be more confusing that shorter code. Think about it ... in the wider real, we refer to our queen as "Elizabeth" but I'll bet that in the small local environment of home at
Windsor Sandringham Balmoral Buck House, her Phil calls her Betty or even Bee.
(written 2009-09-22)
Associated topics are indexed under
J608 - Java - Naming Conventions and Code Management [2604] Tips for writing a test program (Ruby / Python / Java) - (2010-01-29)
Q101 - Object Orientation and General technical topics - Programming Principles [3551] Some terms used in programming (Biased towards Python) - (2011-12-12)
[3548] Dark mornings, dog update, and Python and Lua courses before Christmas - (2011-12-10)
[3542] What order are operations performed in, in a Perl expression? - (2011-12-07)
[3456] Stepping stones - early coding, and writing re-usable code quickly - (2011-09-24)
[3026] Coding efficiency - do not repeat yourself! - (2010-11-02)
[2964] An introduction to file handling in programs - buffering, standard in and out, and file handles - (2010-09-21)
[2915] Looking up a value by key - associative arrays / Hashes / Dictionaries - (2010-08-11)
[2878] Program for reliability and efficiency - do not duplicate, but rather share and re-use - (2010-07-19)
[2769] Easy - but for whom? - (2010-05-18)
[2737] Improving your function calls (APIs) - General and PHP - (2010-04-24)
[2586] And and Or illustrated by locks - (2010-01-17)
[2550] Do not copy and paste code - there are much better ways - (2009-12-26)
[2510] The music of the stock market - (2009-11-22)
[2327] Planning! - (2009-08-08)
[2310] Learning to write high quality code in Lua - (2009-07-30)
[2228] Where do I start when writing a program? - (2009-06-11)
[2022] Pre and post increment - the ++ operator - (2009-02-03)
[2001] I have not programmed before, and need to learn - (2009-01-19)
Some other Articles
Where is my Java class?Viv.java uses unchecked or unsafe operations - explanation and cureJava Programming FundamentalsAutomating access to a page obscured behind a holding pageVariable names like i and j - why?Hello World - a good traditional start to a Java courseSanity checking the price, and selling up to increase incomeAutumn Fruits and Bristol Old StationFurther North - long summer days and lovely countrysideRemoval of technical resources from this site