Thread: Question (about missing curly braces)

  1. #1
    Registered User
    Join Date
    Feb 2005
    Location
    oslo
    Posts
    26

    Question (about missing curly braces)

    Code:
    if (var == something) { cout << "something else"; }
    or

    Code:
    if (var == something) cout << "something else";

    why is someone using without brackets {}? Is there a difference on those 2?

    Would be glad if someone could tell me

    Dag

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Try
    Code:
    if (var == something) cout << "this"; cout << "that";
    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.

  3. #3
    Registered User dan20n's Avatar
    Join Date
    Jan 2005
    Posts
    24
    if statements don’t need braces when you only have one statement after it. Some chose to include it anyway for one reason or another, formatting etc. At the end of the day I don’t believe there is much difference.

    </spoon feed>
    Meg: Everybody! Guess what I am?
    Stewie: Hm, the end result of a drunken back-seat grope-fest and a broken prophylactic?

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    At the end of the day, some other programmer looked at the place where you omitted the braces and has added another statement, forgetting to add braces and introducing a bug ...
    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

  5. #5
    Registered User
    Join Date
    Feb 2005
    Location
    oslo
    Posts
    26
    The compiler I use has no problem compiling and executing this

    Code:
    if (var == something) cout << "this"; cout << "that";

  6. #6
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    Quote Originally Posted by Dag_
    The compiler I use has no problem compiling and executing this

    Code:
    if (var == something) cout << "this"; cout << "that";
    Try passing !something through that and see what happens.

  7. #7
    Registered User
    Join Date
    Feb 2005
    Location
    oslo
    Posts
    26
    Ok got it.
    thanks

  8. #8
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Quote Originally Posted by CornedBee
    At the end of the day, some other programmer looked at the place where you omitted the braces and has added another statement, forgetting to add braces and introducing a bug ...
    and the next day when his superior is still getting the "this compiler sucks" excuse, he decides to take one look at the code and the n00b is subsequently "let go".

    if missing curly braces on an if statement is enough to trip you up, you really need to get out of the field...

    basically, I leave curly braces off one (two) liners for readability and to save space... for example, I prefer
    Code:
    if(this)
        dothis();
    to
    Code:
    if(this)
    {
        dothis();
    }
    but I really can't stand
    Code:
    if(this) dothis();
    IMO, I see the second example as very n00bish...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  9. #9
    Registered User
    Join Date
    Feb 2005
    Location
    oslo
    Posts
    26
    major_small why are you so rude?
    I was just wondering why someone was using this over using braces.

    Also I'm new to c++ programming - so what? new people is not allowed to ask questions in here? Even if I'm a "n00b", no one asked for your opinion whether I should "stay in the field" or not.

    This is my second language and I'm used to indentation over braces. Dont come with this ........

  10. #10
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Quote Originally Posted by Dag_
    Also I'm new to c++ programming - so what? new people is not allowed to ask questions in here? Even if I'm a "n00b", no one asked for your opinion whether I should "stay in the field" or not.
    if you think that was rude, then how about this: maybe you should learn the flow of english before attempting to learn the flow of any language

    first, I wasn't talking about you at all. I was talking about CornedBee's example (which was blatantly hinted at by my quote). Let me spell it out for you - in CornedBee's example, he was talking about how you should use braces so another person doesnt' introduce a bug in your program. I simply stated that if somebody couldn't find a bug like that in a program, they have no business doing it as their job.

    now to be even more rude: I think you're a stupid n00b that needs to pay more attention.




    </rant>
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  11. #11
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    I always do it like this...
    Code:
    if(this)
    {
        dothis();
    }
    ... hint, I don't regard myself as a newbie.

    All 3 of the last professional software houses I have worked in have stated in their installations standards that "though shalt" always use a compound statement even if one is not rquired. Failure to do so would cause the code to fail the QA inspection, and you do it again, and probably have the cost of the second QA inspection charged against your budget.

    m_s:

    No need to be nasty.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  12. #12
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    I'd like to second Adrian. Among many other rules and regulations, our coding standard at work says that curly braces are a requirement. Failure to comply with the coding standards means no bonus, even if the project was on time and otherwise working.

    There is no harm in setting the braces and it costs nothing. If it saves one tired programmer from making one tiny mistake, it has paid off.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  13. #13
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    I do
    Code:
    if(this) {
      that();
    }
    but that's essentially the same as adrian and nvoigt said.

    Actually, I pretty much follow Linus Torvald's coding standards, with some slight alterations.

    And yes, such bugs can be damn hard to find. Mis-indentation can happen easily, and by the time you run the code beautifier over it, you probably don't look back at this particular section.
    Of course, that's provided someone actually goes looking for the bug, because this particular if-branch just might be one that's taken in 99% of the cases, so it takes weeks to even notice that there's a bug. By that time, hundreds of other little changes have been made, and finding the one responsible is not as easy as you make it seem, major_small.

    It costs you practically nothing. Five keystrokes. (Six if you have the opening brace alone.) And for that you have one potential problem less. Don't you think it's worth it?
    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. Curly brace question
    By kenryuakuma in forum C++ Programming
    Replies: 12
    Last Post: 12-16-2008, 07:06 PM
  2. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  3. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  4. question about .net to 6.0 change causing errors
    By jverkoey in forum C++ Programming
    Replies: 17
    Last Post: 03-23-2004, 10:45 AM
  5. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 03:57 PM