Topic: Use of CONST in method parameters...

Topic: Use of CONST in method parameters...

Historically I've never really used the CONST keyword (capitalised to just make it stand out - I'm not shouting really) to pre-pend parameters in methods however I purchased a copy of FixInsight (which is excellent) and one of the most common messages is a missing CONST in front of strings.

The help suggests that this can improve compiler performance for managed types and user-defined types (I read this as interfaces, strings and classes) but it is silent on its use for other simple types.

Couple this with all the C++ reading I've been doing where they seem to suggest ALL parameters should be CONST unless required otherwise, I've started to amend my code to use CONST.

I did some performance testing of some old monolithic code converted to in-line sub-functions with compiler switches for the in-lining and the CONST keyword and I believe I found improvement in the performance for simple types as well as the managed / user-defined.

So my question is... what are peoples opinions of the use to CONST for managed / user-define types and simple types?

Comments

  1. I have been doing this for a very very long time. Personally i always cringe when i see parameters being altered in a method/function in any language. Not that i am an advocate for functional programming but it has advantages. Functions take parameters and return a value, but i see code floating around which actually relies on changing a parameter for the calling code to decide how to procede further. Yuck. So even if it doesnt give advantages with compiling, I would still heartily suggest it as good form. It at least makes code better in my opinion.

    ReplyDelete
  2. I'm using const for everything except interfaces, mostly to prevent assignment to parameter instead of local variables

    ReplyDelete
  3. David Hoyle It's potentially misleading. Consider a const pointer in Delphi - you can change the data at whatever location it points to. You just can't change the pointer variable itself. Stefan had an example of records, which I'll take his word for, I haven't looked at that situation myself.

    Const in C++ has different meanings depending on where the keyword is placed. Check this out: isocpp.org - Standard C++

    Also, C++ allows methods to be const, saying they don't mutate the state. I really like this.

    ReplyDelete

Post a Comment