Are there problems with nesting many loops?

I am working on some homework and want to know if there are such things as too many nested loops. Are there any disadvantages to nesting multiple loops? If so, how will I reorganize the code fragment that I have below?

Below is the code to read the file one line at a time, analyze the fields marked with some specific separators, and remove the front whiteness before printing to the console.

// Read the file one line at a time while (fgets(lineStr, MAXLINELENGTH, fp) != NULL) { charPtr = strtok(lineStr, DELIMITERS); // Loop until line is parsed while (charPtr != NULL) { // Skip past leading whitespace while (isspace(*charPtr)) charPtr++; puts(charPtr); charPtr = strtok(NULL, DELIMITERS); } } 
+6
source share
3 answers

This is really a pretty subjective topic. In my opinion, there is nothing fundamentally wrong in the three nested loops, but you reach the limit of acceptability. If you added one or two levels of nesting, you would, in my opinion, cross the border of what is reasonable to expect from the reader. The human brain can only cope with such complexity at one point in time.

Some people will argue with my opinion that a function should have no more than one level of nesting, and that functions should not contain more than 10 lines of code. The argument of the counter is that such a policy can lead to more fragmented, disjoint code. My rule of thumb is that if you cannot come up with a good function name for a piece of code, then perhaps this piece of code is not really designed to work on its own.

Looking at the ways in which you could break this function down, there are some obvious options.

  • Extract the body of the external while into a separate function. This extracted function would process one line. It would be easy to call and readable.
  • Extract a while that skips the space into a separate function. This would again be easy to name and make code easier to read. You would remove the whitespace comment because the name of the extracted function would make it unnecessary. It is probably worth it.

If you applied these ideas, your code might look something like this:

 char* skipWhitespace(char* str) { while (isspace(*str)) str++; return str; } void parseLine(char *lineStr) { charPtr = strtok(lineStr, DELIMITERS); while (charPtr != NULL) { charPtr = skipWhitespace(charPtr); puts(charPtr); charPtr = strtok(NULL, DELIMITERS); } } ...... while (fgets(lineStr, MAXLINELENGTH, fp) != NULL) parseLine(lineStr); 

Note that refactoring and naming the extracted methods makes comments a little redundant, and I deleted them. Another good rule of thumb: if you need to comment too much on the code, it may not be well known.

Ultimately, there are really no hard and fast rules, and it comes down to judgment and personal preferences. In my opinion, the code in the question is very clear and readable, but, in my opinion, the refactoring version is a little understandable.

Disclaimer: I do not comment on the correctness or code. I just ignored this aspect.

+6
source

The only real flaw is readability, for which there are no hard and fast rules ... although more than three nests usually annoy anyone you work with. As another poster said, sometimes it’s better to break the nest by moving the loop to another function, but the fact that you are here is perfectly readable for me - and this is the only real metric; purely subjective opinion :)

+3
source

As mentioned earlier, this is relatively subjective. However, the way you loop around can have a direct effect on your code. Consider caching-enabled programming. That is, you want to organize your code so that the processor can pre-extract (i.e. predict) the next block of data in the cache before it is needed. This will increase the number of cache accesses and speed up memory access time.

Note that this is not particularly important for your example, however, if you do a lot of memory access, this can be a significant increase or decrease in performance. If you move a multidimensional array according to the column method over a large-row architecture, you can have many misses in the cache (note that misses in the cache are very expensive in real time).

Thus, nesting loops are not necessarily bad, but it can definitely have noticeable effects on performance, especially after a few integers n .

0
source

Source: https://habr.com/ru/post/914172/


All Articles