3

CS50 Style Guide

According to the cs50 style guide for C, more than 3 variables for iteration is not a good practice. I have done Pset 1 (Mario more comfortable) with 5 for loops, so it has 5 iterations variables. Is this not a standard programming procedure? If so what is the next option to replace nested for loops or to limit the iteration variables?

(The image shown here is from the style guide for C, please note the sentence below the code snippet) Click here to view the full document of style guide

2 Answers 2

3

Hmmm.... Good question for new programmers!

The style guide is right, more than 2 or 3 nested for loops should raise red flags, especially if they are based on the same controlling variable as in the example from the image (from the style guide).

The important thing here is to realize that there's a difference of asking whether it should be done vs. it should never be done.

If you have a really good, valid reason for nesting for loops 3 or more levels deep, then go ahead and do it. Deep nesting usually isn't necessary and is often a bright red flag that the code can be simplified, but sometimes it really is appropriate. So, if you've written deep nested loops like that, then know to look it over carefully to see if it can be simplified. Have someone else look at it too. Maybe they see something you don't.

For example, Mario uses nested loops. In fact, the more efficient versions use 3 for loops, but not 3 levels deep. Later psets may or may not need more depth, like.... oops! SPOILERS!!! ;-)

Now, about those control vars. Yes, it's common to use a single letter variable name. But, if the code is getting complex, then more descriptive names become appropriate so that their use can be understood. The more complex, the more important this becomes. Ultimately, it's up to you to determine how much clarity there is in the code and what needs improvement before you release it!

Finally, there's a variation on this. If there's ONE for loop with multiple control variables, that's another red flag that needs to be checked. Again, it doesn't mean that it should never be done, but as the number of vars increases, complexity goes up geometrically (IMHO). Perhaps some of the vars can be taken out of the for loop setup and handled independently. I've seen a trend of code being written with 2, 3 or more vars in a single for loop setup. The vast majority of them have not only been too complex, but have generated logic errors. I definitely feel that 2 control vars in a for loop is a red flag and 3 or more is a very loud RED ALERT alarm!

So yes, deep nesting is a red flag that needs to be examined and a decision needs to be made whether it's justified. But it's not automatically wrong to do.

If this answers your question, well, you know what to do. ;-)

(people keep quoting me. It's embarassing!)

1

There's no problem with having 5 for loops.

However, there are several problems with having 5 nested for loops:

  • It's hard to keep track when reading your code
  • That makes it for other people to understand your code
  • And it makes it hard for yourself to maintain your code later on

In fact, those problems start as soon as you nest more than 2 levels. That's why it is very important to keep your indentations in order.

Sometimes you can use 3 levels, for instance if you are traversing some 3-dimensional structure. But as soon as you start getting confused, you might take some time to check whether that is really the way to go.

However, if you start by designing something which needs 4 levels of nesting, it won't help you at all if you try to translate those loops into something else.


For this problem, you need only one loop (to reach the correct height).

Bus it's also quite normal to approach it with 2 nested loops, as we observe the object as a 2-dimensional structure ...


And to quote Cliff B:

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .