| Thad Smith 2006-07-29, 6:59 pm |
| burningsunorama@gmail.com wrote:
> .. Recently we've had at work a little talk about the way of providing
> const modifier for function parameters. From my point of view are, of
> course, design requirements always more important and thus one should
> always use const keyword with parameters whose values shouldn't get
> changed inside a function ( lets call these ,In' parameters ).
....
> The opposite opinion was based on fact, that when you need to
> use/declare a local variable inside the function, the code is less
> efficient (because you lose speed and memory usage is higher), since
> you could you use a stack variable provided as a parameter
Although both approaches work (and I have used both), I lean towards the
first. My rule of thumb is to specify the usage of each variable as a
comment where it is defined. In the case of function parameters, their
comment reflects the interface, not the implementation. Changing their
value usually would invalidate that specification, so I tend to use
local variables, instead. There are exceptions where a count parameter
is decremented to zero, with a comment such as "number of items to
process", which remains correct as the items are processed. I suppose
my dilemma could be solved by having an external definition and an
internal definition of a parameter, but that gets messy.
--
Thad
|