Search⌘ K
AI Features

Properties of assert

Explore how assert statements in D programming verify assumptions to avoid errors and maintain program correctness. Understand their role as passive checks without side effects, how they aid in unit testing and contract programming, and when assert checks can be disabled for optimized performance.

Using assert checks

Use assert checks even if absolutely true. A large set of program errors are caused by assumptions that are thought to be absolutely true.
For that reason, take advantage of assert checks even if they feel unnecessary. Let’s look at the following function that returns the days of months in a given year:

int[] monthDays(int year) { 
    int[] days = [
       31, februaryDays(year),
       31, 30, 31, 30, 31, 31, 30, 31, 30, 31 
    ];

    assert((sum(days) == 365) || 
           (sum(days) == 366));

    return days;
}
...