Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there any advantages in using const parameters with an ordinal type?

Tags:

delphi

I know marking string parameters as const can make a huge performance difference, but what about ordinal types? Do I gain anything by making them const?

I've always used const parameters when handling strings, but never for Integer, Pointer, class instances, etc.

When using const I often have to create additional temporary variables, which replace the now write-protected parameters, so I'm wondering: Do I gain anything from marking ordinal parameters as const?

like image 320
Daniel Rikowski Avatar asked Oct 21 '09 13:10

Daniel Rikowski


People also ask

What is the advantage of using const?

Constants can make your program more readable. For example, you can declare: Const PI = 3.141592654. Then, within the body of your program, you can make calculations that have something to do with a circle. Constants can make your program more readable.

Which are advantages of using a constant property variable?

o Constants make our programs easier to read by replacing magic numbers and strings with readable names whose values are easy to understand. o Constants make our program easier to modify. o Constants make it easier to avoid mistakes in our programs.

Why might it be good for a function parameter to be const?

Declaring function parameters const indicates that the function promises not to change these values. In C, function arguments are passed by value rather than by reference. Although a function may change the values passed in, these changed values are discarded once the function returns.

Is it advisable to use const a lot in C++ programs?

Yes, you should use const whenever possible. It makes a contract that your code will not change something. Remember, a non-const variable can be passed in to a function that accepts a const parameter. You can always add const, but not take it away (not without a const cast which is a really bad idea).


2 Answers

You need to understand the reason, to avoid "cargo-cult programming." Marking strings as const makes a performance difference because you no longer need to use an interlocked increment and decrement of the refcount on the string, an operation that actually becomes more expensive, not less, as time goes by because more cores means more work that has to be done to keep atomic operations in sync. This is safe to do since the compiler enforces the "this variable will not be changed" constraint.

For ordinals, which are usually 4 bytes or less, there's no performance gain to be had. Using const as optimization only works when you're using value types that are larger than 4 bytes, such as arrays or records, or reference-counted types such as strings and interfaces.

However, there's another important advantage: code readability. If you pass something as const and it makes no difference whatsoever to the compiler, it can still make a difference to you, since you can read the code and see that the intention of it was to have this not be modified. That can be significant if you haven't seen the code before (someone else wrote it) or if you're coming back to it after a long time and don't remember exactly what you were thinking when you originally wrote it.

like image 85
Mason Wheeler Avatar answered Oct 13 '22 21:10

Mason Wheeler


You can't accidentally treat them like var parameters and have your code compile. So it makes your intentions clear.

like image 27
Craig Stuntz Avatar answered Oct 13 '22 23:10

Craig Stuntz