Don’t give me apples that are not red!

I have just stumbled on a beautiful example of a double negative in our code. We have a function that returns a list of object, and it takes a “filterFunction” as a parameter:

List<Apple> GetApples(Function<Apple,bool> filterFunction)

The trouble is, filterFunction works as exclusion function: is is supposed to return true for objects to be thrown out, and false for objects to be kept in the list. So, effectively, instead of saying “give me only red apples” you have to say “don’t give me apples that are not red”. Logically it’s the same thing, but human brain is not designed to easily handle double negatives.

For this reason, I always ask people not to use variable names like “IsNotValidUser” where “false” designates a valid user, and “true” means an invalid user. A flag named “IsValidUser” with reversed values is much easier to understand. However, I’ve never seen the “is not” logic applied to filter functions before. Maybe it has something to do with popularity of functional programming…


    1. It depends. I would still prefer “IsValidUser”. However, there are “negative” words that lost or almost lost connection with the corresponding “positive” word, like “inverted” or “impossible”. They definitely would be OK to use.


    1. Unfortunately, there is no straightforward way. I believe editing is opened for registered users, but I did not allow automatic registration: most of those trying to register and comment are spambots.


      1. Unfortunately there is also no email notification about new comments.
        Commenting in LiveJournal or DreamWidth environment is much more convenient.


Leave a Reply

Your email address will not be published. Required fields are marked *