Consider the following procedure
procedure InTests;
var
N, K: Integer;
begin
N:= 1111;
if N in [6, 8, 10] // this is correct, readable and effective code
then ShowMessage('OK');
K:= 11;
if N in [6, 8, 10, K] // this is correct but less effective
then ShowMessage('OK'); // (compiler creates local 16-bytes set var)
K:= 1111;
if N in [6, 8, 10, K] // this is a bug (K > 255)
then ShowMessage('OK');
end;
in
operator instead of if
chain
if (N = 6) or (N = 8) or (N = 10)
then ShowMessage('OK');
makes the code more compact and readable, but Delphi documentation is silent about it, and you should be aware of potential problems.
The question is: should the in
operator usage with constants only in brackets, for example
if N in [6, 8, 10]
then ShowMessage('OK');
be considered a good practice in Delphi?