When I'm writing functions, I rarely add the const
keyword to my params regardless of whether I intend to modify the params or not. This is just due to habit from when I was a beginner in Delphi and not knowing about the const
keyword. But, it's the same for my company's entire Delphi codebase as well: we rarely pass constant parameters to functions and procedures.
So, now I'm wondering whether we should update our entire codebase and actually be adding the const
keyboard if we don't intend to modify the parameters in any way.
I'm wondering things like:
- Is it good practice to use
const
for parameters that won’t be modified? - Does marking a parameter as
const
have any impact on performance or code optimization? - Are there any potential pitfalls or scenarios where using
const
might lead to unexpected behaviour? - Are there cases in which it is better not to use
const
even if you aren't modifying the param?
In my brain, I'm thinking I should always be using const
if I'm not changing the values... but currently, I never use const
.