I just found ... AGAIN ... a real time wastage bug as follows
for (int i = 0; i < length; i++)
{ //...Lots of code
for (int j = 0; i < length; j++)
{
//...Lots of code
}
}
Did you notice straight ahead the inner i which SHOULD BE j ? Neither did I. So from now on I am going to use:
for (int i = 0; i < length; i++)
{
for (int i1 = 0; i1 < length; i1++)
{
}
}
What are your tips for inner and outer while and for loops ?
Edit: Thanks for the valuable responses. Herewith short summary of the proposed tips:
- use meaningful variables names for index variables ( instead i use SomeObjCollectionLength )
- place the contents of the inner loop into a separate method and call that method from the outer loop
- not manageable amount of lines of code between the outer and inner loop is a strong signal for code smell
- avoid copy pasting and rushing , write the index vars with care
You might want to check the summary by LBushkin for the following
- use foreach and iterators whenever possible
- initialize the variables just before entering the loops
- Make each loop perform only one function. Avoid mixing responsibilities in a single loop
- When possible, make your loops short enough to view all at once