1

When I use inline declarations, should I prefer const over var?

In all online examples, and even in Delphi's own documentation, I see that var is being used. However, I think that const often better expresses my intentions, and prevents accidental modifications.

Small example to demonstrate what I mean:

{$APPTYPE CONSOLE}

program VarVsConst;

uses
  Spring.Collections,
  System.SysUtils;

function UsingVar:string;
begin
  var dict := TCollections.CreateDictionary<string, Integer>;
  dict.Add ('one', 1);
  var pair := dict.ExtractPair('one');
  Result := pair.Value.ToString;
end;

function UsingConst:string;
begin
  const dict = TCollections.CreateDictionary<string, Integer>;
  dict.Add ('one', 1);
  const pair = dict.ExtractPair('one');
  Result := pair.Value.ToString;
end;

begin
  Writeln(UsingVar);
  Writeln(UsingConst);
  Readln;
end.

So, are there any downsides or dangers to the UsingConst implementation?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Wouter van Nifterick
  • 23,603
  • 7
  • 78
  • 122

1 Answers1

0

I suppose a downside could be that a program is more easily "hacked" into. It's a static value. Not sure how much more of a downside when you've got a var assignment like str := 'password' but you get the idea, this is more of an "in general" const opinion. In your example I really don't see a difference as far as this goes, but there are people smarter than me who could spot it if there.

I like const, once it's assigned you can't fubar things accidentally. If I'm that worried about a hack I can change things after the fact, closer to release.

  • 1
    I'm afraid I don't see your point. Besides, a `const X := TMyObject.Create` doesn't make the object `X` points to immutable at all (but the `X` pointer cannot be changed). – Andreas Rejbrand Jul 13 '21 at 16:46
  • 2
    The generated code is identical between the two versions. The constness is compiler enforced. Potential for hacking is not an issue at all. – David Heffernan Jul 14 '21 at 07:42
  • To me, this question could be viewed two ways. One, in my specific example, are there any downsides. Or two, as a general rule, are there any downsides to using const vs var. I mean, he does ask the question both ways at beginning and end. I think your comments are spot on and agree with you in regards to his example. Hence why I said "in your example I don't see much of a difference" but I think I wasn't clear enough on that. Using const in a dynamic memory allocation is much safer, than say using const UserName = "Fred". – Jerry Irons Jul 14 '21 at 14:15
  • A last comment, this was not meant as a problem with Delphi, but more of a programming in general thought, so it may or may not apply in a specific forum to a language. Unfortunately in today's world, ransomware is always out there lurking in the shadows. I've been writing software for a long long time, not that it makes me smarter or better than anybody, it's just what I love to do. What I detest most of all is how I have to play security cop more and more these days. I miss the old days of just solving problems and making things faster and more efficient. – Jerry Irons Jul 16 '21 at 13:16