|
| burningsunorama@gmail.com wrote:
> Hi guys!
>
> This is maybe a too 'academic problem', but I would like to hear your
> opinions, something like pros and cons for each approach....
In fact, it is neither academic nor a problem. :-)
> .. 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
> compiler in this case checks of any assignment or possible change of
> the parameter/variable and gives an potential error message, thus
> ensuring robustness of the code and also, imho, by following this
> convention the readability and understanding of the code for future
> re/viewers is better.
> 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 ( and since
> is it YOU who writes the body of a function YOU take care of proper
> content of the variable ).
> The debate was mainly about the parameters given by value, with
> reference parameters we basically agreed that const is useful, but in
> essence this is imho the same...
Not at all. (a) The code with references to const is usually smaller and
faster (because of additional optimizations the compiler can make). (b)
The code is safer and harder to break by inadvertent modification of
data passed by reference.
> The fact is that we develop time and memory critical system, but
> overall such subtle memory and speed differences do not have critical
> impact on the whole system since it runs on hardware which is powerful
> and dimensed enough to host the sw; therefore, this is, IMHO, a
> completely nonsense, and the questions are:
?? Speed/size either is or is not critical, cannot be both ways :)
> - are the benefits gained in more efficient code worth of losing
> quick
> comprehension of the code ?
> - are those benefits really so high that you can ignore possible
> future
> bugs ?
> - does it even make sense to reason in this context in terms like
> speed
> and memory efficiency ?
>
> Thanks for comments..:)
>
const qualification of a parameter passed by value doesn't increase or
decrease efficiency in itself, so long as you are honest. If you slap a
const on a parameter only to assign it to an intermediate variable, you
are deceiving yourself and a re/viewer, and usually lose efficiency.
[Disclaimer: this is entirely different from passing a pointer to
(const) data, whether under the guise of a reference or not. C/C++ can
easily confuse even an expert: foo(char x[2006]) passes a pointer by
value (= pointed-to data by reference). So when you see foo(type_t x)
you don't know whether your are passing a value or a reference until you
have inspected the typedef for type_t.]
HTH
-Ark
|
|