Search code examples
terminology

is there a name for this conditional-checking technique?


so, theres a technique I use in a fair amount of programming languages and projects; and I'm wondering if it has a general, language-agnostic "official" term to describe it.

basically, I nicknamed it "trip switch checking." Its where if you need to check that several variables have specific values, possibly of different types, you first set a boolean to "false" and then in either a loop or several if statements, you check what you need to by setting the boolean to true if any of the other variables don't meet your requirements.

I call it trip-switching because the boolean remains false if the "switch" isn't "tripped," using an analogy from a common safety mechanism on industrial machinery where if something moves too far or gets too close or etc. a physical switch is actually bumped into and it shuts the whole thing down. the idea is not to switch it back off until the obstruction is cleared- the machine cant turn itself back on automatically.

pseudocode example, a function that returns true if the trip switch wasn't hit:

function tripswitchcheck()
{
  boolean tripswitch = false

  if(idontnwantthis == true)
  {
     tripswitch = true
  }
  if(iwantthis == false)
  {
     tripswitch = true
  }
  //...etc...basically do stuff to check stuff. if any values are undesired, true the tripswitch.

  return !tripswitch
}

It may be bad practice depending on the language and nature of the project, but it works, and that's outside of the scope of this question.


Solution

  • First of all, why would you not just have a positive flag in that example:

    function tripswitchcheck()
    {
      boolean success = true;
    
      if(idontnwantthis == true)
      {
         success = false;
      }
      if(iwantthis == false)
      {
         success = false;
      }
      //...etc...basically do stuff to check stuff. if any values are undesired, falsify success
    
      return success
    }
    

    Secondly, why check the conditions if you already know you have failure. Say that we don't have a forward goto in the language, or else, or early returns (or we don't want to return because something else is done based on the success flag before returning):

    function tripswitchcheck()
    {
      boolean success = true;
    
      if(idontnwantthis == true)
      {
         success = false;
      }
      if(success && iwantthis == false)  // if success is false, fall through
      {
         success = false;
      }
      if(success && !othercondition())   // likewise
      {
         success = false;
      }
      //...etc...
    
      return success
    }
    

    There are reasons not to use else if some of the conditions make use of earlier results.

    Anyway, this just comes from basic logic and scientific reasoning: we have a hypothesis and look for reasons why it is not true.

    The name for it is perhaps "single exit rule" and such: the function exits through a single return statement.