Re: Re-use the argument?
In article <g2h74u$5h3$1@r egistered.motza rella.org>,
Richard <rgrdev@gmail.c omwrote:
[...]
There's clearly no general rule against modifying variables, at least
in traditional imperative languages like C. But that doesn't mean
you should use any available variable as a temporary. Some variables
are intended for a single purpose, and are named accordingly. Some
variables ("i" for example) are traditionally used for a succession
of values in a loop. The key is to be clear. For example, consider
a function to determine kinetic energy given mass and speed:
double ke(double mass, double speed)
{
return mass * speed * speed;
}
It would clearly be wrong to write:
double ke(double mass, double speed)
{
speed *= speed;
speed *= mass;
return speed;
}
because that would use the variable "speed" for something which
is not a speed. On the other hand, this would be quite reasonable:
int mystrlen(char *s)
{
int l = 0;
while(*s++)
l++;
return l;
}
"s" is clearly just a string, and it remains a string as you proceed
through it.
Perhaps still reasonable is:
double ftoc(double temp)
{
temp -= 32;
temp /= 1.8;
return temp;
}
because temp represents a temperature throughout, though its units
change. But it would clearly be wrong if the variable were called
"tempinfahrenhe it".
So I don't think there's a special rule for re-using argument
variables. You should just apply the same reasoning you would for any
variable: does it make sense to use this variable for this purpose?
-- Richard
--
In the selection of the two characters immediately succeeding the numeral 9,
consideration shall be given to their replacement by the graphics 10 and 11 to
facilitate the adoption of the code in the sterling monetary area. (X3.4-1963)
In article <g2h74u$5h3$1@r egistered.motza rella.org>,
Richard <rgrdev@gmail.c omwrote:
>>>Generally it is considered bad form to modify an argument to a
>>>function. It makes things a bit more difficult to read.
>>>function. It makes things a bit more difficult to read.
>>What a load of nonsense. If its not a const its no more than a local
>>variable.
>>variable.
>I really am not interested in that. Its effectively a local variable and
>I have never seen anyone object, in the real world, to it being
>manipulated.
>I have never seen anyone object, in the real world, to it being
>manipulated.
in traditional imperative languages like C. But that doesn't mean
you should use any available variable as a temporary. Some variables
are intended for a single purpose, and are named accordingly. Some
variables ("i" for example) are traditionally used for a succession
of values in a loop. The key is to be clear. For example, consider
a function to determine kinetic energy given mass and speed:
double ke(double mass, double speed)
{
return mass * speed * speed;
}
It would clearly be wrong to write:
double ke(double mass, double speed)
{
speed *= speed;
speed *= mass;
return speed;
}
because that would use the variable "speed" for something which
is not a speed. On the other hand, this would be quite reasonable:
int mystrlen(char *s)
{
int l = 0;
while(*s++)
l++;
return l;
}
"s" is clearly just a string, and it remains a string as you proceed
through it.
Perhaps still reasonable is:
double ftoc(double temp)
{
temp -= 32;
temp /= 1.8;
return temp;
}
because temp represents a temperature throughout, though its units
change. But it would clearly be wrong if the variable were called
"tempinfahrenhe it".
So I don't think there's a special rule for re-using argument
variables. You should just apply the same reasoning you would for any
variable: does it make sense to use this variable for this purpose?
-- Richard
--
In the selection of the two characters immediately succeeding the numeral 9,
consideration shall be given to their replacement by the graphics 10 and 11 to
facilitate the adoption of the code in the sterling monetary area. (X3.4-1963)
Comment