Two style doubts ( do while and pointers )

This is a discussion on Two style doubts ( do while and pointers ) within the C Programming forums, part of the General Programming Boards category; Each one is from this style guide I'm reading. 1. 57. do-while loops can be avoided. do-while loops are less ...

  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
    21,650
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,261
    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%.

  5. #5
    msh
    msh is offline
    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
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    982
    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


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21