Wiki

Changes between Initial Version and Version 1 of AssertStatement

Show
Ignore:
Timestamp:
12/20/10 02:20:29 (14 years ago)
Author:
todd.a
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • AssertStatement

    v1 v1  
     1The '''assert''' statement is used to verify an expected condition during runtime. 
     2 
     3=== Grammar === 
     4{{{ 
     5assert <condition>[, <info>] 
     6}}} 
     7 
     8If the condition is true, execution proceeds. Otherwise, an '''AssertException''' is raised. Assertions can be turned off at runtime with: 
     9{{{ 
     10#!cobra 
     11CobraCore.willCheckAssert = false 
     12}}} 
     13 
     14Assertions are useful for catching errors early when they are easier to diagnose. They also enable subsequent code to operate under the condition expressed by the assertion. Furthermore, they are source of technical documentation. 
     15 
     16The message of the '''!AssertException''' includes information to assist in debugging the assertion including: 
     17 
     18 * the filename and line number where the assert statement appeared 
     19 * the info argument, if provided 
     20 * a full breakdown of the condition's subexpressions and their values 
     21 
     22The breakdown of the condition is significant because it not only aids troubleshooting, but largely eliminates the need to specify the '''info''' argument. For example, rather than: 
     23{{{ 
     24#!cobra 
     25assert obj.foo > bar, 'obj=[obj], obj.foo=[obj.foo], bar=[bar]' 
     26}}} 
     27 
     28This is sufficient: 
     29{{{ 
     30#!cobra 
     31assert obj.foo > bar 
     32}}} 
     33 
     34...because if the assertion fails, the values of '''obj''', '''obj.foo''' and '''bar''' will be given in the exception's '''message'''. Note that reporting the subexpression values causes the subexpressions to execute a second time, so the reported values can be misleading due to side-effects. In practice, this is rare. 
     35 
     36Developers often disable assertions in production software, presumably for a speed boost. But in practice, the overhead of assert statements is often so small as to not be noticeable. Since assertions offer the benefit of easier failure analysis, they should generally be left on. At the same time, providing a switch to turn them off can be prudent because the assertions themselves can be buggy (meaning the assertion fails, but does indicate an actual problem with the program because the condition was poorly formulated). 
     37 
     38Strongly consider using '''require''' instead of '''assert''' when verifying the state of the object at the very beginning of a method or property, or verifying the state of the parameters.  
     39Strongly consider using '''ensure''' instead of '''assert''' when verifying the state of the object at the very ending of a method or property, or verifying the state of the return value. 
     40{{{ 
     41#!cobra 
     42# Example 1  
     43assert x > y  
     44 
     45# Example 2  
     46assert node # as in, the node is not nil  
     47 
     48# Example 3  
     49assert customers.count # as in, the count is not zero  
     50 
     51# Example 4  
     52assert name.length  
     53}}}