Not yet rated

Problem

Negative operators(!=) - such as the not equals or even worse the strict not equals operator(!==) are just so hard to wrap around in one's mind. Is there an easier way to keep your head above the water when you need to deal with negative operators?

Solution

Some say humans are negative by nature but well its not that true when it comes to how our brain things. Its really hard for us to think in a negative way it's actually much easier to think in a positive way and then flip it.

Detailed explanation

Its not that i like to think about the 1/2 empty cup that is in front of me but that's life some times in programing you need to think in negative terms and well if you remember doing some exams that test how quick your brain is you will probably remember fondly of questions such as

if a cat is not a bird and a bird is not a car does that mean a car is not a cat?

now if you had to take a few seconds to digest this your part of the rest of humanity where just having a lot of negatives is a negative thing ;)

 

so how can we solve this problem . well lets start with a programmatic problem lets assume we need to validate that a variable is strictly not 4 and strictly not 10 the most natural way to do it would probably be :
 

var a:int = 2;
trace(a!==4 && a!==10);

 

now this might not make your head heart now but this is a very simple scenario imagine it being with all variables and a big application where its not as easy as matching between 24 and 10:

trace(a!==b && b!==e);

 

and that is already starting to be scary.
 Right time for a simple solution to the rescue instead of thinking in negative terms what you can do is test the exact opposite (to make it positive) and then flip it:

trace(!(a===4 || a===10));

This is so much more human friendly . we are first checking to see if a is 4 or 10 and then reversing the result to create the negative outcome(the not).  hope you enjoyed this small tip and if you want to learn more about operators well would love it if you join my club this article is based on the course Smooth Operator.

 

So what i hope you take out of this is any time you encounter a complex negative try to think if you can simplify it by asking exactly for what you don't want and then flipping the result.

_
Ben Fhala

Flash/AS3 School  || Follow  @02geek ||  Youtube Channel || EventController  || More Recipes

In the spotlight: ActionScript 3.0 Developer basics focused on how to start developing 11 courses focused on the foundations of programing


P.S. If you want to call my attention to a request
add @02geek to the title or twitter
my nickname and a link to your post.


+
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

Report abuse

Related recipes