Thread: Two style doubts ( do while and pointers )

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    2

    Two style doubts ( do while and pointers )

    Each one is from this style guide I'm reading.

    1.

    57. do-while loops can be avoided.
    do-while loops are less readable than ordinary while loops and for loops since the conditional is at the bottom of the loop. The reader must scan the entire loop in order to understand the scope of the loop.
    In addition, do-while loops are not needed. Any do-while loop can easily be rewritten into a while loop or a for loop. Reducing the number of constructs used enhance readbility.
    So a practical example:

    Code:
         do {
            printf( "Write a number between 1 and 10: " );
            scanf( "%d%*c", &number );
        } while( ( number < 1 ) || ( number > 10 ) );
    I should instead write:
    Code:
        printf( "Write a number between 1 and 10: " );
        scanf( "%d%*c", &number );
        while( ( number < 1 ) || ( number > 10 ) ) {
               printf( "Write a number between 1 and 10: " );
               scanf( "%d%*c", &number );
        }
    It isn't too much redundant ( code repeated )?, how would you do it?

    2.

    51. C++ pointers and references should have their reference symbol next to the type rather than to the name.
    float* x; // NOT: float *x;
    int& y; // NOT: int &y;
    The pointer-ness or reference-ness of a variable is a property of the type rather than the name. C-programmers often use the alternative approach, while in C++ it has become more common to follow this recommendation.
    So I should write also things like FILE* fp instead of FILE *fp?

  2. #2
    Registered User
    Join Date
    Apr 2010
    Posts
    32
    Maybe you can be the star in the middle like this for better style.


    Code:
    FILE * fp

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by VelvetMirror
    It isn't too much redundant ( code repeated )?, how would you do it?
    I would use a do while loop. Notice that the argument of that style guide makes sense when the body of the loop has many lines. If you want to more faithfully follow it, you could do things like move the body of the loop into a function, then call the function twice. You could also make use of a flag variable that controls the while loop, but the flag would likely be set at the end of the while loop, thus negating the whole point of avoiding the use of a do while loop.

    That said, if we consider a more developed form of your example, we might observe that we may wish to print a message if the user enters a number outside of the range, or possibly enters non-numeric input. Therefore, the loop might be changed to a controlled infinite loop to cater to this.

    Quote Originally Posted by VelvetMirror
    So I should write also things like FILE* fp instead of FILE *fp?
    It depends. If you are working with an existing code base that already follows a convention, or if your company/project has an established convention, then stick to that convention. Otherwise, the choice is yours, including the choice to follow this style guide. See Stroustrup's answer to the FAQ Is ``int* p;'' right or is ``int *p;'' right?

    Quote Originally Posted by compnub1
    Maybe you can be the star in the middle like this for better style.
    That is not better style, unless it is the convention used for existing code.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Bear in mind that style guides are completely subjective: get two style guide writers into a room, and you'll get to watch a never-ending debate.

    Quote Originally Posted by VelvetMirror View Post
    So a practical example:

    Code:
         do {
            printf( "Write a number between 1 and 10: " );
            scanf( "%d%*c", &number );
        } while( ( number < 1 ) || ( number > 10 ) );
    I should instead write:
    Code:
        printf( "Write a number between 1 and 10: " );
        scanf( "%d%*c", &number );
        while( ( number < 1 ) || ( number > 10 ) ) {
               printf( "Write a number between 1 and 10: " );
               scanf( "%d%*c", &number );
        }
    It isn't too much redundant ( code repeated )?, how would you do it?
    Personally, I'd use a do-while loop. That's my style.

    However, if I was required to comply with this style guideline, I'd use;
    Code:
    number = 0;   /*   Start with some number out of the required range */
    
    while( ( number < 1 ) || ( number > 10 ) )
    {
               printf( "Write a number between 1 and 10: " );
               scanf( "%d%*c", &number );
    }
    Note that I've put the curly braces in a different position too - again that's my stylistic preference

    Personally, I disagree with the second guideline, whether I'm using C or C++. My reason is that it (sort of, arguably) implies that this;
    Code:
    float* x, y;
    declares two pointers. However, y is not a pointer. So, in this case, I'd attach the asterix to the variable name;
    Code:
    float *x, y;
    Of course, if another part of the style guideline requires only one variable be defined per statement, this sort of thing can be mitigated. But, with that sort of constraint, I consider the positioning of the asterix becomes completely irrelevant anyway.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Regarding second convention, I prefer typeName* variableName, under the logic that I'm declaring a variable named variableName of data type pointer-to-typeName. Both typename * variableName and typeName *variableName strike me as illogical.

    There are downsides, of course. See grumpy's post above.

    This, of course, is the stuff flame-wars are made of, and I'd like like to point out that, unless governed by external restrictions, every programmer should use whichever style she feels is theologically justified, and, most importantly, remain consistent in her style. There are bad styles, but inconsistency is worse.

  6. #6
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    do-while
    If I was going for style, I'd use the do-while there. But in practice I stay away from them even when I shouldn't (I do the same with switch statements), just because I'm so used to seeing only while (or a string of if-else if) and it's easier for me to work through mentally. I guess that's something I ought to work on, so my code is more readable.

    float* x; // NOT: float *x;
    int& y; // NOT: int &y;
    I prefer to have the asterisk next to the variable, for the reason that grumpy pointed out and because I think of the declaration like this:
    Code:
    int *x; //*x is an int
    This is how I remembered how the pointer is referenced when I started C. I declared the variable the same way I would reference it later - if I need the value stored in x, I type *x, so int *x makes more sense to me.

    For the same reason, I actually prefer the pass-by-reference style as
    Code:
    int& y; //y is an int
    because you type 'y' when referring to the value stored.

    EDIT: And all this talk about where things go makes me think of const. Oh, sweet 'demon of style' const.
    Last edited by bernt; 05-16-2010 at 12:56 PM.
    Consider this post signed

  7. #7
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    do-while loops are less readable than ordinary while loops and for loops since the conditional is at the bottom of the loop. The reader must scan the entire loop in order to understand the scope of the loop.
    In addition, do-while loops are not needed. Any do-while loop can easily be rewritten into a while loop or a for loop. Reducing the number of constructs used enhance readbility.
    Any decent editor will let you jump from the opening brace of a block to the closing brace. It takes roughly no time to see the condition of a do/while loop. I absolutely agree with you that it's redundant. If your code lends itself to a do/while loop, use one.

    Honestly, there's really not all that much to C's syntax. If a do/while loop is unreadable, that means you don't know C. Heck, you can get rid of while loops, too; just use a for loop! While we're at it, get rid of loops entirely and use goto.

Popular pages Recent additions subscribe to a feed

Tags for this Thread