Name

$Assertions Compiler Directive

Syntax

{$C+}            // default
{$Assertions On} // default
{$C-}
{$Assertions Off}

Scope

Local

Description

$C or $Assertions enables or disables the Assert procedure. When enabled (the default), the compiler generates code to test assertions. When disabled, the compiler ignores all calls to the Assert procedure and does not generate any code for them.

Tips and Tricks

A common misconception is that assertions are primarily for debugging, and that they should be disabled for release. Assertions ensure the proper functioning of your program. If an assertion fails, that means the state of your program is different from anything you anticipated. If the program were to continue running, its behavior would be completely unpredictable. The results might include corruption of the user’s data or other dire consequences. It is better to report the assertion violation and terminate the program.

If an assertion in an inner loop causes performance problems, you can disable just that assertion by using conditional compilation with the $Assertions compiler directive.

Example

Assert(List.Count > 0, 'Internal error: List must not be empty'),
for I := 0 to List.Count-1 do
begin
{$ifndef DEBUG}
 {$Assertions Off}
{$endif}
  Assert(IsValid(List[I]));
{$ifndef DEBUG}
 {$Assertions On}
{$endif}
  DoSomething(List[I]);
end;

See Also

Assert Procedure, $IfDef Compiler Directive, $IfNDef Compiler Directive
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset