Thread: C++ Style Question

  1. #1
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812

    C++ Style Question

    I'm pontificating over this. Which is better?

    This:

    Code:
      if ( r > 0.0 &&
        pGraphType != gtBarChart &&
        pGraphType != gtHistogram )
      {...
    or this:

    Code:
      if ( r > 0.0
        && pGraphType != gtBarChart
        && pGraphType != gtHistogram )
      {...
    Is there an accepted style for this? Or does it not matter?
    OS: Windows XP
    Compilers: MinGW (Code::Blocks), BCB 5

    BigAngryDog.com

  2. #2
    Greetings Davros,

    > I'm pontificating over this. Which is better?
    » It all depends on which helps you read better. For instance, lets break this down.

    Which of the following statements help better the understanding when reading:
    Code:
    if r is greater than 0.0 and
    pGraphType does not equal gtBarChart and
    pGraphType does not equal gtHistogram
    then...
    Or:
    Code:
    if r is greater than 0.0
    and pGraphType does not equal gtBarChart
    and pGraphType does not equal gtHistogram
    then...
    Note: Take a 2 to 3 second break after reading each line. This may help you find your style of how you like to read your code.

    > Is there an accepted style for this? Or does it not matter?
    » It does not matter. Either way we express it, it always means the same. Some programmers adapt to Example 1 or Example 2 when reading code. Which ever way you read code best should depict how you like seeing code.


    - Stack Overflow
    Last edited by Stack Overflow; 11-15-2004 at 05:20 PM. Reason: Added more info
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  3. #3
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    I don't think there's any accepted style. But I have to say, I like the second one better because:
    a) You can clearly see that the logical operator is an AND
    b) The &&'s are lined up
    c) The indent from the &&'s causes the 3 conditions to line up perfectly

    On the other hand, what do you do if you have:
    Code:
    if(r < 0.0 && (fooo || ((!foo || bar) && baar)) && foooo == baaar)
    {
       //...
    }
    Would you do this:
    Code:
    if(r < 0.0 && (
          fooo || (
             (!foo || bar) &&
             baar
          )
       ) && 
       foooo == baaar)
    {
       //...
    }
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  4. #4
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    I'm fond of the first myself, though I indent so that the conditions line up.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  5. #5
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    Thanks
    OS: Windows XP
    Compilers: MinGW (Code::Blocks), BCB 5

    BigAngryDog.com

  6. #6
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Most programmers will look to the end of the line for the operator.
    C Elements of Style

    This example brings up another question: Do you put operators at the end of the line, as in the previous example, or at the beginning of the next line?

    Actually, either method is acceptable as long as it is used consistently. That means that I don't get to dodge the issue if there is any reasonable basis for choosing one method over the other.

    There is a basis: majority rule. Most programmers prefer to put the operators at the end of the line. So let's go with the majority preference and make it the rule:

    When writing multi-line statements, put the arithmetic and logical operators at the end of each line.
    The online book is well worth a read.

  7. #7
    Arggggh DeepFyre's Avatar
    Join Date
    Sep 2004
    Posts
    227
    i like the second one beacuse in the first one you have a sentence and then you use 'AND' but then you start a new line so it is kind of confusing.
    Keyboard Not Found! Press any key to continue. . .

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    I'm a fan of the first version. Not least because I write in a variety of programming languages, and not all have statement terminators. In those languages, it can be vital to have an unsatisfied operator before the linebreak. (I think the language where this is so is called Mosel.)
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Design layer question
    By mdoland in forum C# Programming
    Replies: 0
    Last Post: 10-19-2007, 04:22 AM
  2. automatic type conversation style question
    By sept in forum C++ Programming
    Replies: 3
    Last Post: 10-14-2007, 04:28 AM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  4. Lame question on dialog controls appearance
    By Templario in forum Windows Programming
    Replies: 2
    Last Post: 03-18-2003, 08:22 PM
  5. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM