Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h1>The four purposes of <code>assert</code></h1> <p>Assume you work on 200,000 lines of code with four colleagues Alice, Bernd, Carl, and Daphne. They call your code, you call their code.</p> <p>Then <code>assert</code> has <strong>four roles</strong>:</p> <ol> <li><p><strong>Inform Alice, Bernd, Carl, and Daphne what your code expects.</strong><br> Assume you have a method that processes a list of tuples and the program logic can break if those tuples are not immutable:</p> <pre><code>def mymethod(listOfTuples): assert(all(type(tp)==tuple for tp in listOfTuples)) </code></pre> <p>This is more trustworthy than equivalent information in the documentation and much easier to maintain.</p></li> <li><p><strong>Inform the computer what your code expects.</strong><br> <code>assert</code> enforces proper behavior from the callers of your code. If your code calls Alices's and Bernd's code calls yours, then without the <code>assert</code>, if the program crashes in Alices code, Bernd might assume it was Alice's fault, Alice investigates and might assume it was your fault, you investigate and tell Bernd it was in fact his. Lots of work lost.<br> With asserts, whoever gets a call wrong, they will quickly be able to see it was their fault, not yours. Alice, Bernd, and you all benefit. Saves immense amounts of time.</p></li> <li><p><strong>Inform the readers of your code (including yourself) what your code has achieved at some point.</strong><br> Assume you have a list of entries and each of them can be clean (which is good) or it can be smorsh, trale, gullup, or twinkled (which are all not acceptable). If it's smorsh it must be unsmorshed; if it's trale it must be baludoed; if it's gullup it must be trotted (and then possibly paced, too); if it's twinkled it must be twinkled again except on Thursdays. You get the idea: It's complicated stuff. But the end result is (or ought to be) that all entries are clean. The Right Thing(TM) to do is to summarize the effect of your cleaning loop as</p> <pre><code>assert(all(entry.isClean() for entry in mylist)) </code></pre> <p>This statements saves a headache for everybody trying to understand what <em>exactly</em> it is that the wonderful loop is achieving. And the most frequent of these people will likely be yourself.</p></li> <li><p><strong>Inform the computer what your code has achieved at some point.</strong><br> Should you ever forget to pace an entry needing it after trotting, the <code>assert</code> will save your day and avoid that your code breaks dear Daphne's much later.</p></li> </ol> <p>In my mind, <code>assert</code>'s two purposes of documentation (1 and 3) and safeguard (2 and 4) are equally valuable.<br> Informing the people may even be <em>more</em> valuable than informing the computer because it can prevent the very mistakes the <code>assert</code> aims to catch (in case 1) and plenty of subsequent mistakes in any case.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload