40 - Prescribed Use

by steen_hansen

HTML

<!DOCTYPE html><meta charset="utf-8" />
<title>40 Prescribed Use</title>


  
<div id='js-div' style="width:48%;" >
  <a href='https://github.com/steenhansen/type-czech'  target="_blank" class='git-hub'>GitHub Package</a>
  <div id='js-code'>the javascript</div>

  
</div>


<div id="the-explanation" style="width:52%">
  <div class='an-explantion'>40 - Prescribed Use</div>
  <div id='explanation-text'>
    <br>
The intended manner for using TypeCzech is to <span class='red-explanation'>toggle 
   runtime checking</span> by loading or not loading TypeCzech.js.
   The below code will render all TypeCzech calls into no-ops when
   TypeCzech.js is not loaded, virtually no cpu time is lost as 
   can be seen in /time-trials/time-differences.js.
<br>
<pre>
if (typeof TypeCzech === 'function')
  type_czech = TypeCzech('LOG-ERRORS')
else
  type_czech = { linkUp: (nop) => nop, isActive: (x) => false }
</pre>

Program with TypeCzech testing turned ON.
<pre>
&lt;script src="../TypeCzech.js"&gt;
</pre>
  
Program with TypeCzech testing turned OFF.
<pre>
&lt;!-- &lt;script src="../TypeCzech.js"&gt; --&gt;
</pre>







By using the below construct, <span class='blue-explanation'>non-existant PRE and POST checking functions 
will just be ignored</span> by TypeCzech. Useful for not including large blocks of
testing code to the browser that will be parsed. This style stops exceptions when non-existant checking functions are referenced.

<pre>
PRE_check_aLottery = (typeof PRE_check_aLottery === 'undefined')
                       ? undefined : PRE_check_aLottery

//  function PRE_check_aLottery (){
//    // this function is ignored if does not exist
//  }
</pre>
  
<b>Best practice</b> is to use both constructs to handle turning TypeCzech checking functions on and off.
<ul>
  <li>
    <div class='red-explanation'>if (typeof TypeCzech === 'function')</div>
    <div  class='red-explanation'>&nbsp;&nbsp;&nbsp;&nbsp;type_czech = TypeCzech('THROW-EXCEPTIONS')</div>
    <div ...