Thread: Parentheses preference

  1. #16
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Style two. Things getting complicated?

    bool maybe = condition;
    if (maybe) ...

    Though it's not always easy to pare down to bool, which is fine by me.

  2. #17
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Style two but often see style one. I normally continue with the style that has already been used in the code to keep it consistent regardless of my personal pref.

  3. #18
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Quote Originally Posted by whiteflags View Post
    Style two. Things getting complicated?

    bool maybe = condition;
    if (maybe) ...

    Though it's not always easy to pare down to bool, which is fine by me.
    Some folks I work with would argue that the 'maybe' is superfluous and adds nothing to the code; I know this b/c I am always doing that (boiling down large, complex (IMHO needlessly so) if() conditionals. A twist on the above is like this (warning, off-the-top of my head code coming to illustrate a point):
    Code:
    // old conditional
    if( (someVal > 6) && (someOtherVal <= 2) || (someState == STATE_FOO))
    {
         // do something
    }
    
    // often becomes:
    bool shouldActionBeTaken(int someVal, int somOtherVal, machineState someState);
    
    if( shouldActionBeTaken(6, 2, someState))
    {
        // take action
    }
    In any event I find in complex logic, boiling your conditionals down to a single state ( in your case, 'maybe') helps convey the intent of the code better than almost anything else.

    One thing in this thread that cracked me up is someone going on about the virtues of using little whitespace and single-char variables to save disk space...if your drive space is so limited that you have little room for clean-reading source you have larger issues than just drive space...

    By that estimation, you should never backup source, keep a VCS going, etc. I am (literally as I write this) in the middle of a 6 TB data backup...well "middle" might be a little generous but you get the idea...anyhow the bottom line in that while I have a LOT of source code on my various dev machines, all of it combined (as well as multiple copies that the VCS keeps) is less than any single data file I am backing up. Having single-character variables, minimal whitespace, etc would save me ~ 0.00000000001% of my drive
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  4. #19
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Quote Originally Posted by jeffcobb View Post
    Code:
    bool shouldActionBeTaken(int someVal, int somOtherVal, machineState someState);
    
    if( shouldActionBeTaken(6, 2, someState))
    {
        // take action
    }
    I do have a problem with naming conventions that reflect the processing logic instead of the business logic. I could probably never name anything shouldActionBeTaken. If I find myself in a spot where naming something after the processing logic is easier than business logic, that's IMO a clear sign that I overcomplicated my design. I'll probably go back and check what am I doing wrong.

    Naturally, if I'm being particular sloppy or it just doesn't matter for that particular piece of code, I don't bother. But generally speaking, shouldActionBeTaken may reveal a design flaw, if the naming was achieved because of clarity.

    One thing in this thread that cracked me up is someone going on about the virtues of using little whitespace and single-char variables to save disk space...if your drive space is so limited that you have little room for clean-reading source you have larger issues than just drive space...
    Sly was obviously making a joke. I thought it was evident.
    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.

  5. #20
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    I'd use Style 2 as well.

    Although at work I code in MUMPS, and in that language, there ARE no whitespace choices -- you either do it the way the language expects or you do it wrong.

    (OK, there is the occasional whitespace choice. If you have a block statement for an IF or FOR construct, you can optionally use exactly one space to indent.)
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  6. #21
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Mario F.
    I do have a problem with naming conventions that reflect the processing logic instead of the business logic. I could probably never name anything shouldActionBeTaken. If I find myself in a spot where naming something after the processing logic is easier than business logic, that's IMO a clear sign that I overcomplicated my design. I'll probably go back and check what am I doing wrong.
    I think you are concentrating on a name chosen just because a name is needed for an example when you should be concentrating on the idea. If you will, change the name to something like isMarioFied, or perhaps isOld or hasWhiteHair
    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

  7. #22
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Yeah, I revisited this thread sometime later and I agree I missed the point. isMarioIdiot evaluates to true sometimes.
    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.

  8. #23
    Registered User
    Join Date
    Jul 2010
    Posts
    20
    Style 2 all the way! Easier to read imo...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Value initialization using an empty pair of parentheses
    By zephon in forum C++ Programming
    Replies: 43
    Last Post: 09-07-2009, 11:58 AM
  2. Parentheses checker with stacks
    By cannsyl in forum C++ Programming
    Replies: 11
    Last Post: 12-05-2008, 02:17 PM
  3. Parentheses in IF statements.
    By thetinman in forum C++ Programming
    Replies: 11
    Last Post: 11-30-2005, 04:08 PM
  4. Using a Calculator - Formula Input Preference
    By Davros in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 07-21-2004, 04:38 PM
  5. Nesting Parentheses
    By XZSNPP in forum C++ Programming
    Replies: 6
    Last Post: 01-18-2003, 10:01 PM