B
burningsunorama
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....
... 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...
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:
- 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..
This is maybe a too 'academic problem', but I would like to hear your
opinions, something like pros and cons for each approach....
... 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...
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:
- 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..