Thread: should statements having no effect be allowed?

  1. #1
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708

    should statements having no effect be allowed?

    is there a good argument for allowing them? if not, should they generate a syntax or parse error? (note: some function calls might have no effect but since it may be impossible to determine whether they do or not, they should probably be excluded from this category.)

    examples of what I'm talking about are:

    Code:
    j;
    ;;;;;
    for(;;);{}
    the last example also illustrates how a typo can cause bugs that are difficult to find.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by Sebastiani
    is there a good argument for allowing them?
    The only one I can come up with right now is this.
    Code:
    void foo(int value)
    {
       switch ( value )
       {
       case 1:
          puts("Selection 1");
          break;
       default:
          ;
       }
       printf("value = %d\n", value);
    }
    Here the null statement is just that on purpose. It produces a syntax error if it is omitted.

    Regarding this, a good compiler/linter ought to mention it:
    Code:
    j;
    And this is has a useful purpose as well.
    Code:
    for(;;);{}
    It could be waiting for an interrupt and then need some variables with block scope. Not that things couldn't be written differently.

    Good indenting and commenting can help avoid issues with any of these.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    Blank statements are sometimes necessary.

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    thanks for the input, guys. I'm working on a compiler in my spare time (a superset of C++) and I was considering omitting no-ops from the language. do you think it's warranted - or should I just leave well enough alone? any input would be appreciated.

    >> The only one I can come up with right now is this.

    true, but couldn't the default statement be left out altogether?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    5
    Not 100% sure about this one but i could have sworn GCC will stop compiling the code after it hits a infinite loop.

    ex:
    Code:
    #include <stdio.h>
    
    int main()
    {
            for(;;);
            printf("hello\n");
            return 0;
    }
    The above when disassembled with gdb would be all the assembly until the for() everything afterwards does not exist? Am i misunderstanding the situation?

  6. #6
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    Well I wouldn't ever see a need for

    Code:
    for(;;);
    but maybe for something like this

    Code:
    for(int i = 0; i<MAX; cout<<array[i++]<<endl)
         ;

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> but maybe for something like this

    or this:

    Code:
    for(int i = 0; i<MAX; cout<<array[i++]<<endl)
         continue;
    it just seems to me that they serve no useful purpose, and that we'd be better off if they hadn't been incorporated into the language in the first place. I think I'll allow them, though - if only for the sake of portability. thanks again for the input, everyone.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  8. #8
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    If ANSI/C99 says it's allowed, you'd better make it allowed. Remember what went with Miracle C.

  9. #9
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    You could just throw a warning. It is a good middle ground at least you let the programmer know that something may be up.
    Woop?

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Apart from examples above where empty statements are useful (even though their need may be debatable), empty statements occur a lot in automatically generated code. They have no effect, and it is difficult in some programs (eg code generators that parse something else to generate code) to eliminate them.

    I'd suggest not eliminating them altogether. Having a warning about their presence (preferably a warning that can be turned off, if desired) would be useful for picking up coding errors in hand-written code.

  11. #11
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    excellent suggestion. I think I'll go that route. thank you.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  12. #12
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    Quote Originally Posted by Sebastiani
    >> but maybe for something like this

    or this:

    Code:
    for(int i = 0; i<MAX; cout<<array[i++]<<endl)
         continue;
    That would do nothing because the action would never be executed.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    That would do nothing because the action would never be executed.
    As far as I can tell it should work. continue skips over the rest of the loop body, but not over the post-iteration expression.
    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

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Do you propose support for a pre-processor as well?

    Because things like this could easily result in empty statements with varying conditional compilation flags.
    Code:
    if ( somecond )
    {
    #ifdef TEST
      printf( "something happened\n" );
    #endif
      LOGMSG("a message");
    }
    LOGMSG may or may not be a macro which may or may not expand to nothing.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  15. #15
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    More importantly: Tell us about this language, Seb!
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  2. C++ If Statements Help
    By moporho in forum C++ Programming
    Replies: 19
    Last Post: 01-18-2008, 08:40 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Efficiency of case statements
    By Yasir_Malik in forum C Programming
    Replies: 26
    Last Post: 05-23-2006, 11:36 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM