All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.l10n.delphi.rules.community-delphi.TooManyVariables.html Maven / Gradle / Ivy

There is a newer version: 1.12.1
Show newest version

Too many variables can indicate that a routine is doing too many things.

Why is this an issue?

Using many variables in a routine can be an indicator that the routine is doing too many things. A routine should be a single logical task, not a number of disconnected tasks.

Using a large number of redundant variables in a short span of code can also adversely affect readability.

How to fix it

Consider breaking the routine up into multiple routines with different responsibilities:

procedure TMyObject.DoThing;
var
  MyFoo: TFoo;
  MyBar: TBar;
begin
  MyFoo := TFoo.Create;
  FMyFoo := MyFoo;

  MyBar := TBar.Create;
  FMyBar := MyBar;
end;
procedure TMyObject.DoFooThing;
var
  MyFoo: TFoo;
begin
  MyFoo := TFoo.Create;
  FMyFoo := MyFoo;
end;

procedure TMyObject.DoBarThing;
var
  MyFoo: TFoo;
begin
  MyBar := TBar.Create;
  FMyBar := MyBar;
end;

Another option is to inline unnecessarily verbose variables:

procedure TMyObject.DoThing;
var
  MyFoo: TFoo;
  MyBar: TBar;
begin
  MyFoo := TFoo.Create;
  FMyFoo := MyFoo;

  MyBar := TBar.Create;
  FMyBar := MyBar;
end;
procedure TMyObject.DoThing;
begin
  FMyFoo := TFoo.Create;
  FMyBar := TBar.Create;
end;




© 2015 - 2024 Weber Informatics LLC | Privacy Policy