Recent

Author Topic: to const or not to const  (Read 21000 times)

hinst

  • Sr. Member
  • ****
  • Posts: 303
to const or not to const
« on: June 16, 2014, 10:39:23 am »
I sometimes wish that FreePascal didn't have const modifier for function arguments at all. Because I often find myself thinking whether I should use it in every particular case or not
Code: [Select]
procedure WriteStuff(s: string);
//or
procedure WriteStuff(const s: string);
Code: [Select]
procedure CalcNumber(i: Integer);
//or
procedure CalcNumber(const i: Integer);

I am kind of aware of effects caused by const modifier. For example, when string argument is declared const, compiler does not enable reference counting for it (in this particular procedure)
However, it does not make things easier for me because I still have to think "do I need reference counting in this case or not". With reference counting enabled it feels somehow safer while with reference counting disabled the code should generally work faster

Do you put const everywhere or avoid it at all? or spend time thinking in each case?

What about class instance variables?
Code: [Select]
procedure PrintObject(o: TMyObject);
//or
procedure PrintObject(const o: TMyObject);

In 99% of procedures I do not assign values to arguments in procedure body. So should they be const? Not sure if const makes code more readable or otherwise
Too late to escape fate

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12888
  • FPC developer.
Re: to const or not to const
« Reply #1 on: June 16, 2014, 10:58:20 am »
Probably they should be const yes. But usually I only care for the distinction in reusable, somewhat performance sensitive code. Which is only like 1-2%. And even that is inflated because somewhat used to thinking about it due to work on the RTL.

garlar27

  • Hero Member
  • *****
  • Posts: 652
Re: to const or not to const
« Reply #2 on: June 16, 2014, 01:28:53 pm »
I put const everywhere with few exceptions.

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: to const or not to const
« Reply #3 on: June 16, 2014, 02:10:09 pm »
I put const everywhere with few exceptions.
+1
Everything is const unless there is a reason to make var. I never use by value parameters aka no designation at all never find any use for it really other than do not declare a new variable inside the procedure/method.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Leledumbo

  • Hero Member
  • *****
  • Posts: 8836
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: to const or not to const
« Reply #4 on: June 16, 2014, 04:53:59 pm »
Yep, const most of the time. Guarantee of a parameter being accidentally modified. If you need to modify, make a local copy first.

eny

  • Hero Member
  • *****
  • Posts: 1665
Re: to const or not to const
« Reply #5 on: June 16, 2014, 07:57:17 pm »
I put const everywhere with few exceptions.
+1
+1

A side effect concerning reference counting is that there is a bug in the handling of interfaces. Passing an interface as a const parameter will make your program a memory-leakage waterfall.
This is a bug in FPC, undocumented and you are not warned by the compiler.
(You have been warned now...)
All posts based on: Win11; stable Lazarus 4_4  (x64) 2026-02-12 (unless specified otherwise...)

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: to const or not to const
« Reply #6 on: June 16, 2014, 08:10:56 pm »
Code: [Select]
procedure CalcNumber(i: Integer);What's wrong with this? It's called by value, if you don't need the value anymore you can modify it (and you don't need new local var) and when procedure's over changes are forgotten.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

eny

  • Hero Member
  • *****
  • Posts: 1665
Re: to const or not to const
« Reply #7 on: June 16, 2014, 08:18:33 pm »
Code: [Select]
procedure CalcNumber(i: Integer);What's wrong with this? It's called by value, if you don't need the value anymore you can modify it (and you don't need new local var) and when procedure's over changes are forgotten.
It helps you in documenting the program and making it easier to understand by others.
Also the compiler helps you in blocking assignments or changing the variable.
So that in case you expect to return a value, you are hinted that that is not possible.
All posts based on: Win11; stable Lazarus 4_4  (x64) 2026-02-12 (unless specified otherwise...)

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: to const or not to const
« Reply #8 on: June 16, 2014, 08:30:47 pm »
AFAIR in past Lazarus autocomplete had created setters with const, then was a huge discussion on the mailing list and it resulted to change to non-const setters.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: to const or not to const
« Reply #9 on: June 16, 2014, 08:48:34 pm »
AFAIR in past Lazarus autocomplete had created setters with const, then was a huge discussion on the mailing list and it resulted to change to non-const setters.
So who is responsible for me doing all the const adding manually? and even more important why?
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: to const or not to const
« Reply #10 on: June 16, 2014, 09:06:56 pm »
Quote
So who is responsible for me doing all the const adding manually? and even more important why?

There were two big discussions in past. One was about "dangerous" with..do construction, AFAIR initiated by Ido Kanner.
The second, about const was initiated AFAIR by programmer with russian name. I just don't remember if it was Lazarus or FPC ML, and I can't find it in archives.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12888
  • FPC developer.
Re: to const or not to const
« Reply #11 on: June 16, 2014, 09:43:17 pm »
Code: [Select]
procedure CalcNumber(i: Integer);What's wrong with this? It's called by value, if you don't need the value anymore you can modify it (and you don't need new local var) and when procedure's over changes are forgotten.
It helps you in documenting the program and making it easier to understand by others.

It adds an unnecessary indirection and thus slows down on many ABIs :-)

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: to const or not to const
« Reply #12 on: June 16, 2014, 09:52:33 pm »
Quote
So who is responsible for me doing all the const adding manually? and even more important why?
You can try to put feature request to add one checkbox to Lazarus Options. Also, there is new define EnableCodeCompleteTemplates (under development !) where you can manually add "const" to ..lazarus/components/codetools/codecompletiontemplates.xml
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: to const or not to const
« Reply #13 on: June 16, 2014, 10:17:42 pm »
Quote
So who is responsible for me doing all the const adding manually? and even more important why?

There were two big discussions in past. One was about "dangerous" with..do construction, AFAIR initiated by Ido Kanner.
The second, about const was initiated AFAIR by programmer with russian name. I just don't remember if it was Lazarus or FPC ML, and I can't find it in archives.

I think there may have been several discussions of const. One that ran for several days was started in the fpc-devel ML by Chad Berchek on 4 July 2011, "const optimization is a serious bug".

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: to const or not to const
« Reply #14 on: June 16, 2014, 11:08:49 pm »
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

 

TinyPortal © 2005-2018