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

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

There is a newer version: 1.12.1
Show newest version

Why is this an issue?

Code in which all blocks are fenced by begin and end is simple and modular to understand. All blocks should use begin..end, except for repeat and case blocks.

While most Delphi structures allow begin and end to be omitted when there is only a single statement in the block, doing so often makes code more error-prone and difficult to read.

Also, if another statement is added at a later stage and begin and end are not added, the new statement will be unexpectedly outside of the block. While the code below may seem correct at a glance, the Exit will always be encountered and Foo.Bar will never be run.

if not Assigned(Foo) then
  Writeln('Foo not assigned!');
  Exit;

Foo.Bar;

How to fix it

Surround the statement with begin and end:
if not Assigned(MyObj) then
  Exit;
if not Assigned(MyObj) then
begin
  Exit;
end;




© 2015 - 2024 Weber Informatics LLC | Privacy Policy