8

I was trying to show some code to another person when I subtly perceived that beside when declared variables are not used there is compiler hint messages, there is no hints or messages when a declared constant is not used. Following code is an example:

program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils,
  Math;

const
  intM: Integer = 1000;

var
  valorDouble, notusedvar: Double;

begin
  try
    valorDouble   := 0.001;
    Writeln('--- Codigo atual --');
    Writeln('Double   -> ', Trunc(valorDouble   * 1000));
    Writeln('--- Correcao?? --');
    Writeln('Trunc(1.0000001) -> ', Trunc(1.0000001));
    Writeln('Trunc(0.001 * 1000.0)   -> ', Trunc(0.001 * 1000.0));
    Writeln('Trunc(0.0010 * 1000.0)  -> ', Trunc(0.0010 * 1000.0));
    Writeln('Trunc(0.00100 * 1000.0) -> ', Trunc(0.00100 * 1000.0));

    Readln;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

Why there is no hint about the not used constant? There is any logical explanation about this difference?

EMBarbosa
  • 1,473
  • 1
  • 22
  • 76
  • 4
    Go look in *Windows.pas*. Imagine the torrent of messages you'd get if there were a warning for every constant in there that you *didn't* use. – Rob Kennedy Apr 16 '14 at 20:29
  • @Rob ...and otherwise any Windows API call that has constants pre-built into Delphi, as I demonstrate in my answer. – Jerry Dodge Apr 16 '14 at 22:10

2 Answers2

7

Let's jump straight into an example. Let's say you're writing a DLL with 1 exported function. One of those function's parameters is an integer...

procedure DoSomething(const Value: Integer); stdcall;

Now let's say you have defined multiple constants to represent all possible integer values this function might recognize...

const
  CON_ONE = 1;
  CON_TWO = 2;
  CON_THREE = 3;
  //Maybe hundreds

Now let's say when you implement this function, you only really need the first one CON_ONE but not the other two. Would you really want a hint for every one of these?

More realistic example is things like HKEY_LOCAL_MACHINE, HKEY_CURRENT_USER, etc. which come with Delphi (tied to Windows API calls). Just take a look at all the constants in Windows.pas. Imagine if all of these possible constants raised a compiler hint.

Essentially, when you get a compiler hint of an unused variable, it most often means a coding error (or just something you forgot to delete), whereas an unused constant typically means just an unimplemented capability.

Jerry Dodge
  • 26,858
  • 31
  • 155
  • 327
  • 2
    +1 this is the right track. An explanation of why unused vars are bad would be useful. – David Heffernan Apr 16 '14 at 22:02
  • @David which Ken's answer did quite well (apparently not as complete as it should be though) – Jerry Dodge Apr 16 '14 at 22:04
  • 2
    not really. Ken's entire answer is inaccurate. Especially the part about variables. The point is that if you declare a variable but do not use it that is often a sign that you made a coding error. Many many times that warning has found errors that I have made. – David Heffernan Apr 16 '14 at 22:07
4

Interesting question.

For constants (and variables) declared in the interface section of a unit, it is pretty easy to understand why unused exemplars do not trigger a compiler hint: everything within the interface section is published to the outside world for usage that the unit cannot be aware of. As programmer of a unit, you offer/present possible values which users of your unit may use in their code, although the unit does not necessarily has to use them in its implementation. See Consts.pas for example, a unit which sole purpose is to interface constants (ok, resourcestrings) for elsewhere usage.

For constants declared in the implementation section of a unit, and - as seen in your code sample - apparently also for constants declared in the program file, to me there seems to be no clear reason why there is no hint for unused constants.

And as encore, the reason why initialized variables in the implementation section do not trigger hints is because they actually are a concatenation of a variable declaration and an assignment statement, thus an initialized variable ís used from a compiler point of view.

NGLN
  • 43,011
  • 8
  • 105
  • 200
  • I think that part was answered by Ken White, but seems he removed his answer. – EMBarbosa Apr 17 '14 at 21:22
  • @embarbosa ken's answer talked about memory allocation. The real issue is the compiler trying to help find programming errors. NGLN is not talking about the issues ken talked about. – David Heffernan Apr 17 '14 at 21:35
  • @DavidHeffernan but even when a constant is declared inside a function, it doesn't trigger the hint. That doesn't seems to fit in any other explanation given yet. – EMBarbosa Apr 18 '14 at 15:56
  • Typed constants consume memory contrary to what ken said – David Heffernan Apr 18 '14 at 16:04
  • @DavidHeffernan you are right about that. I'm sorry that when reading my comments now seems that I don't understood that part. I did, but still I'm a little perplexed. Seems that the answer from Barry Kelly [here](http://stackoverflow.com/a/49060/460775) is useful. – EMBarbosa Apr 18 '14 at 16:17
  • I personally think that constants in local scope that are not used merit a hint or warning. But that scenario is less dangerous than in interface section. Even so it is not uncommon to define a bunch of constants for completeness and only use a subset of them. – David Heffernan Apr 18 '14 at 16:21