Should I pass a parameter as a const in a function if I don't intend to modify it?

119 Views Asked by At

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:

  1. Is it good practice to use const for parameters that won’t be modified?
  2. Does marking a parameter as const have any impact on performance or code optimization?
  3. Are there any potential pitfalls or scenarios where using const might lead to unexpected behaviour?
  4. 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.

0

There are 0 best solutions below