Skip navigation

I keep coming across code like

for( int deviceIndex=0; deviceIndex < devices.size(); deviceIndex++ )
{
    devices[deviceIndex].something();
    devices[deviceIndex].param += value;
}

what’s wrong with this code? wouldn’t it be a lot better like this?

for( int i=0; i < devices.size(); i++ )
{
    devices[i].something();
    devices[i].param += value;
}

i is a dummy variable. It’s name doesn’t matter – it just needs to be short. devices[deviceIndex] is redundant – we already know what we are accessing by the array’s name, devices[deviceIndex] is just longer and noisier than devices[i] – without adding additional clarity.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: