T
Tomás
Let's say we have a function that returns the result of subtracting seven
from a given number. Would you write it as:
(A)
int SubtractSeven( int const x )
{
return x - 7;
}
(B)
int SubtractSeven( int x )
{
return x -= 7;
}
(C)
int SubtractSeven( int const& x )
{
return x - 7;
}
If we just wanted to "accomplish the task", without contemplating how fast
the code will run, or how much memory it will need, then I think the natural
choice is A. Do you agree with me?
If we consider efficiency, then we may think, "Why create that temporary? We
should just re-use the parameter variable.". This would make us lean toward
B.
Considering efficiency again, we may think to ourselves, "Why even create
that parameter variable? Let's just use the one we're supplied with (if
possible)". This would have some rationale to it (I'm open to correction
here) if we were dealing with an inline function... but if we're dealing
with an outline function, then we'd be playing around with pointers under
the hood, which, in the end, will be far less efficient than our original
example.
So... my aim is to draw up some conventions for the use of A, B and C.
Firstly, when do I use method C?
1: When I want to alter the supplied variable.
2: When I'm dealing with a user-defined type which consumes quite more
memory than an "int", or if the invokation of copy constructors would
involve considerable processing and dynamic allocation of memory.
Examples:
1:
void SubtractSevenFromGivenVariable(int &x)
{
x -= 7;
}
2:
unsigned CountAmountOfVowels( std::string const & str )
{
unsigned val;
//do stuff
return val;
}
Do you agree with me on when to use C?
If we had a small POD like the following:
struct XYCoord
{
unsigned x;
unsigned y;
};
then I would be probably not pass it by reference, and just pass it by
value. Do you agree with me here?
As for A and B... which shall it be?
I think that A is more natural, and is "the way things were supposed to be",
but I don't think we can deny the efficient reuse of a variable which we see
in B.
At the moment, I use the B method, although I'd be open to
advice/suggestions.
Do we have a const parameter and return a temporary? Or do we have a non-
const parameter, and return the result of subtracting seven from it?
-Tomás
from a given number. Would you write it as:
(A)
int SubtractSeven( int const x )
{
return x - 7;
}
(B)
int SubtractSeven( int x )
{
return x -= 7;
}
(C)
int SubtractSeven( int const& x )
{
return x - 7;
}
If we just wanted to "accomplish the task", without contemplating how fast
the code will run, or how much memory it will need, then I think the natural
choice is A. Do you agree with me?
If we consider efficiency, then we may think, "Why create that temporary? We
should just re-use the parameter variable.". This would make us lean toward
B.
Considering efficiency again, we may think to ourselves, "Why even create
that parameter variable? Let's just use the one we're supplied with (if
possible)". This would have some rationale to it (I'm open to correction
here) if we were dealing with an inline function... but if we're dealing
with an outline function, then we'd be playing around with pointers under
the hood, which, in the end, will be far less efficient than our original
example.
So... my aim is to draw up some conventions for the use of A, B and C.
Firstly, when do I use method C?
1: When I want to alter the supplied variable.
2: When I'm dealing with a user-defined type which consumes quite more
memory than an "int", or if the invokation of copy constructors would
involve considerable processing and dynamic allocation of memory.
Examples:
1:
void SubtractSevenFromGivenVariable(int &x)
{
x -= 7;
}
2:
unsigned CountAmountOfVowels( std::string const & str )
{
unsigned val;
//do stuff
return val;
}
Do you agree with me on when to use C?
If we had a small POD like the following:
struct XYCoord
{
unsigned x;
unsigned y;
};
then I would be probably not pass it by reference, and just pass it by
value. Do you agree with me here?
As for A and B... which shall it be?
I think that A is more natural, and is "the way things were supposed to be",
but I don't think we can deny the efficient reuse of a variable which we see
in B.
At the moment, I use the B method, although I'd be open to
advice/suggestions.
Do we have a const parameter and return a temporary? Or do we have a non-
const parameter, and return the result of subtracting seven from it?
-Tomás