<p>I have a large classic ASP app that I have to maintain, and I repeatedly find myself thwarted by the lack of short-circuit evaluation capability.  E.g., VBScript won't let you get away with:</p>

<pre><code>if not isNull(Rs("myField")) and Rs("myField") &lt;&gt; 0 then
...
</code></pre>

<p>...because if Rs("myField") is null, you get an error in the second condition, comparing null to 0.  So I'll typically end up doing this instead:</p>

<pre><code>dim myField
if isNull(Rs("myField")) then 
    myField = 0
else
    myField = Rs("myField")
end if

if myField &lt;&gt; 0 then
...
</code></pre>

<p>Obviously, the verboseness is pretty appalling.  Looking around this large code base, the best workaround I've found is to use a function the original programmer wrote, called TernaryOp, which basically grafts in ternary operator-like functionality, but I'm still stuck using a temporary variable that would not be necessary in a more full-featured language.  Is there a better way?  Some super-secret way that short-circuiting really does exist in VBScript?</p>

[{'answer_id': 59606, 'author': 'busse', 'author_id': 5702, 'author_profile': 'https://Stackoverflow.com/users/5702', 'pm_score': 3, 'selected': False, 'text': '<p>Nested IFs (only slightly less verbose):</p>\n\n<pre><code>if not isNull(Rs("myField")) Then\n   if Rs("myField") &lt;&gt; 0 then\n</code></pre>\n'}, {'answer_id': 59615, 'author': 'Danimal', 'author_id': 2757, 'author_profile': 'https://Stackoverflow.com/users/2757', 'pm_score': 0, 'selected': False, 'text': '<p>Would that there were, my friend -- TernaryOp is your only hope. </p>\n'}, {'answer_id': 59618, 'author': 'oglester', 'author_id': 2017, 'author_profile': 'https://Stackoverflow.com/users/2017', 'pm_score': 4, 'selected': True, 'text': '<p>Maybe not the best way, but it certainly works... Also, if you are in vb6 or .net, you can have different methods that cast to proper type too.</p>\n\n<pre><code>if cint( getVal( rs("blah"), "" ) )&lt;&gt; 0 then\n  \'do something\nend if\n\n\nfunction getVal( v, replacementVal )\n  if v is nothing then\n    getVal = replacementVal\n  else\n    getVal = v\n  end if\nend function\n</code></pre>\n'}, {'answer_id': 59631, 'author': 'Marshall', 'author_id': 1302, 'author_profile': 'https://Stackoverflow.com/users/1302', 'pm_score': 1, 'selected': False, 'text': '<p>Yeah it\'s not the best solution but what we use is something like this</p>\n\n<pre><code>function ReplaceNull(s)\n    if IsNull(s) or s = "" then\n        ReplaceNull = "&amp;nbsp;"\n    else\n        ReplaceNull = s\n    end if\nend function\n</code></pre>\n'}, {'answer_id': 59693, 'author': 'dewde', 'author_id': 2640, 'author_profile': 'https://Stackoverflow.com/users/2640', 'pm_score': 3, 'selected': False, 'text': '<p>I always used Select Case statements to short circuit logic in VB. Something like..</p>\n\n<pre><code>Select Case True\n\nCase isNull(Rs("myField"))\n\n    myField = 0\n\nCase (Rs("myField") &lt;&gt; 0)\n\n    myField = Rs("myField")\n\nCase Else\n\n    myField = -1        \n\nEnd Select\n</code></pre>\n\n<p>My syntax may be off, been a while. If the first case pops, everything else is ignored.</p>\n'}, {'answer_id': 139545, 'author': 'Cirieno', 'author_id': 17615, 'author_profile': 'https://Stackoverflow.com/users/17615', 'pm_score': 0, 'selected': False, 'text': '<p>Two options come to mind:</p>\n\n<p>1) use <code>len()</code> or <code>lenb()</code> to discover if there is any data in the variable:</p>\n\n<pre><code>if not lenb(rs("myField"))=0 then...\n</code></pre>\n\n<p>2) use a function that returns a boolean:</p>\n\n<pre><code>if not isNothing(rs("myField")) then...\n</code></pre>\n\n<p>where <code>isNothing()</code> is a function like so:</p>\n\n<pre><code>function isNothing(vInput)\n    isNothing = false : vInput = trim(vInput)\n    if vartype(vInput)=0 or isEmpty(vInput) or isNull(vInput) or lenb(vInput)=0 then isNothing = true : end if \nend function\n</code></pre>\n'}, {'answer_id': 139572, 'author': 'Cirieno', 'author_id': 17615, 'author_profile': 'https://Stackoverflow.com/users/17615', 'pm_score': 2, 'selected': False, 'text': '<p>Or perhaps I got the wrong end of the question.  Did you mean something like <code>iIf()</code> in VB?  This works for me:</p>\n\n<pre><code>myField = returnIf(isNothing(rs("myField")), 0, rs("myField"))\n</code></pre>\n\n<p>where <code>returnIf()</code> is a function like so:</p>\n\n<pre><code>function returnIf(uExpression, uTrue, uFalse)\n    if (uExpression = true) then returnIf = uTrue else returnIf = uFalse : end if\nend function\n</code></pre>\n'}, {'answer_id': 22572341, 'author': 'Bond', 'author_id': 2237785, 'author_profile': 'https://Stackoverflow.com/users/2237785', 'pm_score': 2, 'selected': False, 'text': '<p>If you write it as two inline <code>IF</code> statements, you can achieve short-circuiting:</p>\n\n<pre><code>if not isNull(Rs("myField")) then if Rs("myField") &lt;&gt; 0 then ...\n</code></pre>\n\n<p>But your <code>then</code> action must appear on the same line as well. If you need multiple statements after <code>then</code>, you can separate them with <code>:</code> or move your code to a subroutine that you can call. For example:</p>\n\n<pre><code>if not isNull(Rs("myField")) then if Rs("myField") &lt;&gt; 0 then x = 1 : y = 2\n</code></pre>\n\n<p>Or</p>\n\n<pre><code>if not isNull(Rs("myField")) then if Rs("myField") &lt;&gt; 0 then DoSomething(Rs("myField"))\n</code></pre>\n'}, {'answer_id': 38354090, 'author': 'JemWritesCode', 'author_id': 3593119, 'author_profile': 'https://Stackoverflow.com/users/3593119', 'pm_score': 0, 'selected': False, 'text': '<p>You may be able to just use <code>Else</code> to catch nulls, ""s, etc.</p>\n\n<pre><code>If UCase(Rs("myField")) = "THING" then\n  \'Do Things\nelseif UCase(Rs("myField")) = "STUFF" then\n  \'Do Other Stuff\nelse\n  \'Invalid data, such as a NULL, "", etc.\n  \'Throw an error, do nothing, or default action\nEnd If\n</code></pre>\n\n<p>I\'ve tested this in my code and it\'s currently working. Might not be right for everyone\'s situation though. </p>\n'}]
['https://Stackoverflow.com/questions/59599', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1818/']