Properties of assert

This lesson explains different properties associated with assert in detail.

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;
}

Get hands-on with 1400+ tech skills courses.