CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 3 FirstFirst 123 LastLast
Results 16 to 30 of 31
  1. #16
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Is there any difference between using {} and not using them?

    Quote Originally Posted by laserlight View Post
    There is a difference in scope and thus lifetime due to the use of RAII. I think dshawul already explained the idea: to release a resource - that may have a non-negligible cost to keep around - when it is no longer in use. In a way, it is akin to the idea of a scoped lock.
    Well, yeah. But he was making it sound like it was a bad thing to do.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  2. #17
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: Is there any difference between using {} and not using them?

    Another thing to remember is that having the 'DoSomething' on a different line allows you to debug more easily.

    Code:
    if(condition) DoSomething(); // Without 'stepping in' we don't know if DoSomething() was called or not.
    
    if(condition)
    { 
        DoSomething();  // We can put a breakpoint here.
    }
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  3. #18
    Join Date
    Jan 2009
    Posts
    1,689

    Re: Is there any difference between using {} and not using them?

    I still prefer putting the bracket on the end of the line, it makes it easier for me to see what it matches up with, plus it folds better.

    Code:
    if (true){
       foo();
       bar();
    }
    folds to
    Code:
    if (true){ ... }
    Whereas
    Code:
    if (true)
    {
       foo();
       bar();
    }
    folds to
    Code:
    if (true)
    { ... }

  4. #19
    Join Date
    Jan 2008
    Location
    California, USA
    Posts
    822

    Re: Is there any difference between using {} and not using them?

    I agree with monarch_dodra on going with what is accepted at the working environment.

    There's one thing that I can't quite decide which way to go, i.e.
    Code:
    // case 1
    template <typename Type>
    const T foo(const Type& arg1, const Type& arg2 /* and smoe more */)
    
    // case 2
    template <typename Type>
    const T foo(const Type& arg1,
                const Type& arg2
                /* and some more */)
    // case 3
    template <typename Type>
    const T foo(
                const Type& arg1,
                const type& arg2
                /* and some more */
                )
    The naming convention and the brackets, I can pretty much deal with because, well, it has been discussed to death, but the placement of the ending parenthesis has been not (well at least for me) Although I'm a big fan of having lots of small functions rather than a big one and design from the user's point of view, sometimes functions with many number of parameters are just ..there.

    This gets even worse when working with the templates because I agree with the notion that a good naming convention (and no, I don't favor the hungarain notation) can avoid writing a seperate document for it.

    I still can't make up my mind on this.

    EDIT:
    I forgot to mention the case where nesting functions...
    Last edited by potatoCode; September 24th, 2010 at 09:03 AM.

  5. #20
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: Is there any difference between using {} and not using them?

    I tend to use case 1 for functions with one or two parameters and case 2 for when there are more or when the parameter definitions are long, such as when using templates or deep namespaces.
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  6. #21
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Is there any difference between using {} and not using them?

    Quote Originally Posted by JohnW@Wessex View Post
    I tend to use case 1 for functions with one or two parameters and case 2 for when there are more or when the parameter definitions are long, such as when using templates or deep namespaces.
    Ditto.

    As for the closing parenthesis, or the position of the first parameter (first/second line), or the alignement (everything under the first parameter, or just 1 indentation), I think it is such subtle differences that nobody really cares. Or at least doesn't care enough to make it a standard.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  7. #22
    Join Date
    Sep 2010
    Posts
    39

    Re: Is there any difference between using {} and not using them?

    Quote Originally Posted by monarch_dodra View Post
    Well, yeah. But he was making it sound like it was a bad thing to do.
    I don't know where you got that from ?? I said I was forced to use scoping because I can't have two big local variables (ScalarField is like 1 million doubles f.i). At first I had c-style variable declarations all at the beginning. I switched to declaration of variables with in different scopes and now I can solve 6x bigger problems.

  8. #23
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Is there any difference between using {} and not using them?

    Quote Originally Posted by dshawul View Post
    I recently found myself using too many {} blocks to reclaim memory of big local variables with destructors.

    Code:
    {
          ScalarField   A ;
          do something with A;
          //destructor for A
    }  
    {
          ScalarField B;
          //destructor for B
    }
    is different from
    Code:
    ScalarField A;
    ScalarField B;
    dosomethig with A;
    dosomething with B;
    Quote Originally Posted by dshawul View Post
    I don't know where you got that from ?? I said I was forced to use scoping because I can't have two big local variables (ScalarField is like 1 million doubles f.i). At first I had c-style variable declarations all at the beginning. I switched to declaration of variables with in different scopes and now I can solve 6x bigger problems.
    Your "too many" made it sound to me like it was a bad thing to do. Which is confusing, because as you say, it is actually a good thing to limit scope. So I wanted to know. Guess I miss-understood.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  9. #24
    Join Date
    Jan 2009
    Posts
    1,689

    Re: Is there any difference between using {} and not using them?

    Quote Originally Posted by potatoCode View Post
    EDIT:
    I forgot to mention the case where nesting functions...
    C++ has no support for this, very few languages do.

  10. #25
    Join Date
    Jan 2008
    Location
    California, USA
    Posts
    822

    Re: Is there any difference between using {} and not using them?

    Quote Originally Posted by ninja9578 View Post
    C++ has no support for this, very few languages do.
    Haha. I meant nesting function CALLS
    But that 's' at the end of the word function was surely misleading

  11. #26
    Join Date
    Jan 2009
    Posts
    1,689

    Re: Is there any difference between using {} and not using them?

    Ooh, I see what you meant now. I've been writing php, which is one of the few languages with nested functions, I guess it was on my brain.

  12. #27
    Join Date
    Aug 2006
    Location
    Timisoara, Romania
    Posts
    433

    Re: Is there any difference between using {} and not using them?

    ok guys, thanks for the answers.
    I thought that maybe, internally, the computer executes a different number of operations/instructions, so that there would be less instructions/operations executed by this:

    Code:
    int x;
    
    if (condition)
       x = 5;
    than it would be by this:
    Code:
    int x;
    
    if (condition)
    {
      x = 5;
    }
    But, if you say that it's ok, that's nice. There is code that I sometimes change, from many instructions executed by an if (where you really need brackets) to a single instruction, and sometimes I leave the brackets there. I thought that it might be bad programming or something.

    Sorry, I think this question needed a bit of knowledge of assembly or alike (how it is implemented), but it was a c++ issue, so I guess this was the proper place to ask this question.

  13. #28
    Join Date
    May 2009
    Posts
    2,413

    Re: Is there any difference between using {} and not using them?

    Quote Originally Posted by Feoggou View Post
    Sorry, I think this question needed a bit of knowledge of assembly or alike (how it is implemented), but it was a c++ issue, so I guess this was the proper place to ask this question.
    Yes it's a C++ issue but above all it's a program design issue.

    Write the simplest most easy to understand code possible. Putting in a few extra not strictly necessary brackets or parentheses for the sake of clarity will never slow down your code. And never cram code into one-liners. It's not efficient, just error-prone. One line - one thought, is a good rule of thumb.

    There's a division of labour here. You write clear easy to maintain C++ code and the compiler will produce efficient assembly for you.

  14. #29
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: Is there any difference between using {} and not using them?

    I would always use braces, but I think it is ok to do the following:
    Code:
    switch (a)
    {
        case 1: { f(); break; } 
        case 2: { g(); break; }
        default: // Do nothing.
    };
    if(b) { f(); }



    Quote Originally Posted by ninja9578 View Post
    C++ has no support for this [local functions], very few languages do.
    Oh but it does!

    Code:
    int f(int a)
    {
        struct Local
        {
            inline static int g(int x)
            {
                return x * x;
            }
        };
    
    
        return Local::g(a) * Local::g(a);
    }
    I've used this in the past for very simple helper functions.

    Last edited by Zaccheus; September 28th, 2010 at 11:22 AM.
    My hobby projects:
    www.rclsoftware.org.uk

  15. #30
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Is there any difference between using {} and not using them?

    Quote Originally Posted by Zaccheus View Post
    I would always use braces, but I think it is ok to do the following:
    Code:
    switch (a)
    {
        case 1: { f(); break; } 
        case 2: { g(); break; }
        default: // Do nothing.
    };
    if(b) { f(); }

    I disagree. Multiple statements on one line are hard to read and hard to debug. It's a bad habit IMHO.

Page 2 of 3 FirstFirst 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured