*Naming Conventions*

bruno.desthuilliers at gmail.com bruno.desthuilliers at gmail.com
Mon Jun 11 22:32:06 CEST 2007


On Jun 8, 2:06 pm, Neil Cerutti <horp... at yahoo.com> wrote:
> On 2007-06-08, Bruno Desthuilliers <bruno.42.desthuilli... at wtf.websiteburo.oops.com> wrote:
> > Neil Cerutti a écrit :
(snip)
>
> >> Certainly i and j are just as generic, but they have the
> >> advantage over 'item' of being more terse.
>
> > I'm not sure this is really an "advantage" here.
>
> Why not?
>

Because
1/ too much terseness (like too much verbosity) goes against
readability,
2/ 'i' and 'j' are canonical names for C-style for loops indices - and
Python's for(each) loops are very different...

A few special cases aside, one-letter identifiers are bad when it
comes to readability. As far as I'm concern, I stopped using 'j' even
in C code a long time ago - as soon as I have too distinct loop
indices, I can never remember what are 'i' and 'j' supposed to
represent, so I prefer to name them more explicitly (ie : 'row' and
'col').

Just make the test:


for (i=0; i < mi; i++)
  for (j-0; j < mj; j++)
    f(a[i][j]);


vs

for (row=0; row < nb_rows; row++) {
  for (col=0; col < nb_cols; col++) {
    init_item_at(grid[row][col]);
  }
}





More information about the Python-list mailing list