Simple syntax habit, good or bad...

This is a discussion on Simple syntax habit, good or bad... within the C++ Programming forums, part of the General Programming Boards category; Nice little low level opinion question for all the experienced coders who have worked with a lot of other programmers ...

  1. #1
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069

    Simple syntax habit, good or bad...

    Nice little low level opinion question for all the experienced coders who have worked with a lot of other programmers in their careers...

    I've been writing a certain syntax with conditionals and loops for quite a while and I've found now that not many people do it. Let me show you what it looks like and tell me if you personally have a problem with it as a programmer. It's perfectly legal, however it may seem slightly obscure.
    Code:
    if(/*condition*/)
       // Do something
    else while(/*NOT condition*/) {
       // Do some other stuff
    }
    I recently had a teacher tell me that "else while" isn't an actual control structure and I explained I understood, I just write it like that out of habit. Anyone think it's wrong and I should change it? It really is the simplest of code and I don't personally see anything wrong with it. In fact, it seems more logical to me to group it that way.
    Sent from my iPad®

  2. #2
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    I don't like it. It makes it seem as though this:
    Code:
    else while(...)...
    is different from this:
    Code:
    else
    {
         while (...)
         { ....
         }
    }
    Also, it makes your code somewhat less readable.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    Quote Originally Posted by manutd
    I don't like it. It makes it seem as though this:
    Code:
    else while(...)...
    is different from this:
    Code:
    else
    {
         while (...)
         { ....
         }
    }
    Also, it makes your code somewhat less readable.
    I think your first remark was the way my teacher felt, as well. I assumed, though, that any experienced programmer would know there is no difference between the two and would simply consider it a formatting style as it is. However, I do respect you opinion on readability. I do encourage you, though, to read a similar plain English statement and reread the code. For example:
    If the cake batter is a yellow color, pour evenly though out the cake dish. Otherwise, continue to beat the batter until it reaches a yellow tone.
    Now consider this code:
    Code:
    // Assume cakeBatter as an instance of a food class with the appropriate methods and variables
    // and assume cbYELLOW and pEVENLY are members of their respective enums
    if(cakeBatter.color == cbYELLOW)
       pourInDish(cakeBatter, pEVENLY);
    else while (cakeBatter.color != cbYELLOW) {
       cakeBatter.beat();
    }
    You see? Doesn't the "else while" seem appropriate there?
    Last edited by SlyMaelstrom; 11-09-2006 at 05:57 PM.
    Sent from my iPad®

  4. #4
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    In English, it does. However, most English speakers would be considered "advanced English programmers". As not ever beginning programmer might know that this is not a new type of loop, they might get freaked out and try to find an example of how to use it on the internet. Thus for consistency, I like the second way. Also, the braces/indentation show more clearly the program flow. But that's just my opinion. You may have your entirely different (but still valid) style.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Anyone think it's wrong and I should change it?
    I don't think it's wrong, but I do think that you should consider its usage carefully. When people read code, they expect certain styles and conventions. If you deviate from those styles and conventions, you tend to make your code harder to follow.
    My best code is written with the delete key.

  6. #6
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,458
    It doesn't shock me. I'm used to do things like:

    Code:
    if (this) do_this;
    else for (...) {
            do_that_;
            and_do_that_too;
        }
    Also,

    Code:
    if (this) do_this; else do_that;
    Confessedly, I don't try to be smart around much bigger pieces of code at which time I do "proper" indentation. But if I can avoid curly braces around small snippets, I do.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  7. #7
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    With me, if it's a one line thing:
    Code:
    if (/*condition*/) dosomething();
    I use it, but when combined with loops, etc, I use curly braces as they make execution more clear to me.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    if(/*condition*/)
       // Do something
    else while(/*NOT condition*/) {
       // Do some other stuff
    }
    Are the conditions above the same, with one simply the NOT of the other? If yes, then it seems like a logical grouping you've constructed, and I don't have a problem with it. If no, then I'm not as enthused about the grouping.

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    One of the guidelines I subscribe to is full bracing, always*. So your preference goes against my habit.
    [voice="Forest Gump"]And that's about all I've got to say about thaaat.[/voice.]

    *Except in some initializations, but always in program structure.
    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.*

  10. #10
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    There is no good reason to use 'else while'. There is a good reason for 'else if': for chaining them along when a switch would not work. You cannot chain while loops. I can't think of a plausible way that this could improve readability.
    There are 10 types of people in this world, those who cringed when reading the beginning of this sentence and those who salivated to how superior they are for understanding something as simple as binary.

  11. #11
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,458
    Other than perhaps for the one who reads and maintains his own code, and because of some wicked gene does find it readable?
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  12. #12
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    You know, the construct itself really isn't that problematic. At first seeing it is a bit odd, but that's really just a matter of rarity.

    The main problem with this is that it's hard to imagine a situation where that kind of code idiom would logically be used. Taking, for example, the cake example.
    Code:
    // Assume cakeBatter as an instance of a food class with the appropriate methods and variables
    // and assume cbYELLOW and pEVENLY are members of their respective enums
    while (cakeBatter.color != cbYELLOW) {
       cakeBatter.beat()
    }
    pourInDish(cakeBatter, pEVENLY);
    Callou collei we'll code the way
    Of prime numbers and pings!

  13. #13
    Registered User
    Join Date
    Apr 2006
    Posts
    2,032
    I'd probably do this:
    Code:
    if(/*condition*/)
       // Do something
    else 
       while(/*NOT condition*/) {
          // Do some other stuff
       }
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  14. #14
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    After thinking about it, the most important argument to me against this construct is my expectation when not reading carefully. A condition on the same line as an "else" belongs to an "else if", that's just a given for most programmers, which might cause them to miss the little "while" there. In other words, they read the start of the line ("else") and the end of the line (a condition) and their brain fills in what's between ("if").
    And then comes the confusion.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  15. #15
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >One of the guidelines I subscribe to is full bracing, always*.
    I did that at first, but now I omit braces if the body of a block is only a single line. So I would do this:
    Code:
    if ( condition )
      foo();
    Instead of this:
    Code:
    if ( condition ) {
      foo();
    }
    But I make sure to do this:
    Code:
    if ( condition ) {
      while ( condition )
        foo();
    }
    Rather than this:
    Code:
    if ( condition )
      while ( condition )
        foo();
    Because the latter isn't translated correctly with most automatic indentation software. Then again, a lot of my personal style consists of "do what I say and not what I do".
    My best code is written with the delete key.

Page 1 of 4 1234 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 02:49 PM
  2. Crazy errors caused by class, never seen before..
    By Shamino in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 11:54 AM
  3. Good news, and bad news... (meaningless post, do not wast your time reading it)
    By compjinx in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 04-05-2002, 03:27 PM
  4. Goto statements, good or bad?
    By Esparno in forum C++ Programming
    Replies: 20
    Last Post: 03-20-2002, 08:16 PM
  5. good news and bad news
    By Garfield in forum A Brief History of Cprogramming.com
    Replies: 25
    Last Post: 10-27-2001, 07:31 AM

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