|
-
September 24th, 2010, 02:09 AM
#16
Re: Is there any difference between using {} and not using them?
 Originally Posted by laserlight
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.
-
September 24th, 2010, 03:38 AM
#17
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
-
September 24th, 2010, 08:09 AM
#18
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
Whereas
Code:
if (true)
{
foo();
bar();
}
folds to
-
September 24th, 2010, 09:00 AM
#19
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.
-
September 24th, 2010, 09:26 AM
#20
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
-
September 24th, 2010, 09:31 AM
#21
Re: Is there any difference between using {} and not using them?
 Originally Posted by JohnW@Wessex
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.
-
September 24th, 2010, 01:55 PM
#22
Re: Is there any difference between using {} and not using them?
 Originally Posted by monarch_dodra
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.
-
September 24th, 2010, 02:05 PM
#23
Re: Is there any difference between using {} and not using them?
 Originally Posted by dshawul
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;
 Originally Posted by dshawul
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.
-
September 24th, 2010, 02:54 PM
#24
Re: Is there any difference between using {} and not using them?
 Originally Posted by potatoCode
EDIT:
I forgot to mention the case where nesting functions...
C++ has no support for this, very few languages do.
-
September 25th, 2010, 10:06 AM
#25
Re: Is there any difference between using {} and not using them?
 Originally Posted by ninja9578
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
-
September 25th, 2010, 07:43 PM
#26
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.
-
September 26th, 2010, 11:40 AM
#27
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.
-
September 27th, 2010, 09:33 AM
#28
Re: Is there any difference between using {} and not using them?
 Originally Posted by Feoggou
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.
-
September 28th, 2010, 11:13 AM
#29
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(); }
 Originally Posted by ninja9578
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.
-
September 28th, 2010, 12:55 PM
#30
Re: Is there any difference between using {} and not using them?
 Originally Posted by Zaccheus
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|