Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7838097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T14:58:39+00:00 2026-06-02T14:58:39+00:00

I am working on some homework and wanted to know if there is such

  • 0

I am working on some homework and wanted to know if there is such as thing as too many nested while loops. Is there downsides to nesting several while loops? If so how would one refactor the code snippet I have below?

Below is code to read a file one line at a time, parse the fields delimited by some defined delimiters, and remove leading white space before printing to 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);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T14:58:40+00:00Added an answer on June 2, 2026 at 2:58 pm

    This is really a rather subjective topic. In my view, there’s nothing fundamentally wrong with three nested while loops, but you are reaching the limit of acceptability. Were you to add one or two more levels of nesting then you would, in my view, cross the boundary of what is reasonable to expect a reader to comprehend. The human brain can only handle so much complexity at any one point in time.

    Some people would argue, counter to my opinion, that there should be no more than one level of nesting in a function, and that functions should not contain more than around 10 lines of code. The counter argument is that such a policy can result in more fragmented, disjoint code. My rule of thumb is that if you cannot think of a good function name for a chunk of code, then perhaps that chunk of code is not really meant to stand alone as a function.

    Looking at ways in which you could break this function up, there are a couple of obvious options.

    1. Extract the body of the outermost while into a separate function. That extracted function would process a single line. It would be easy to name and clear to read.
    2. Extract the while loop that skips whitespace into a separate function. That again would be easy to name and would make your code easier to read. You would remove the whitespace comment because the name of the extracted function would render it needless. That’s probably worth doing.

    If you applied these ideas then your code might look a little 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 the refactoring and naming of extracted methods makes the comments a little superfluous and I removed them. Another good rule of thumb is that if you need to comment code too much, then perhaps it is not yet well-factored.

    Ultimately, there really are not hard and fast rules, and it comes down to judgement and personal preference. In my view, the code in the question is very clear and readable, but the refactored version is just a little clearer, in my view.

    Disclaimer: I make no comment regarding the correctness or otherwise of the code. I simply ignored that aspect.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on homework and wanted to know what this is actually defined
I am working on some homework here, but I have completely run out of
I'm working on some homework for an intro to C class, in which we
I'm working on some homework for an intro to C class, in which we
I was working on my advanced calculus homework today and we're doing some iteration
I am working on some homework. I cannot get the footer to fit properly
So I am working on some homework and I have to complete a size
I'm working on some homework for my compiler class and I have the following
Having some issues with one small function I'm working on for a homework assignment.
I am working on some homework for class, and decided to add some extra

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.