Parameter Constness


#1

I’m curious about the usage of “const” as parameters in methods and functions of JUCE, where the parameter will not change - in implementation files, and “seemingly spuriously” in headers. Does anybody know if doing provides an advantage (ie: readability, affecting code generation) - or is it a stylistic preference?

Take for example juce::Array::ensureStorageAllocated:

The parameter does not change in the method, and is therefore “const.” Though, the guarantee of it not being modified is self-explanatory - therefore does not “need” to be const (unless there’s something I’m not seeing at a glance)…


#2

Years ago, I used to constify parameters like that in headers to match the implementation files, but don’t do that any more as there’s no benefit to it, and it just clutters the function definitions. I’m gradually removing the consts from header files whenever I happen to spot one, which is why they seem to be randomly scattered.

In implementation files I like to use const wherever possible though. It doesn’t make much difference to what the compiler produces, but using more constness is a good style, I think.


#3

Ah, so it’s all part of an older stage of an ever-evolving style. :slight_smile: Curiosity satisfied - thanks!