Domanda

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?

È stato utile?

Soluzione

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.

Altri suggerimenti

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top