Robert Kaufmann
Robert Kaufmann

Reputation: 778

Conditions: Negation or equivalence to false?

During the last year or so, I have made myself accustomed to using equivalency to false instead of negation in conditional statements and logic assignments.

For instance, I would use:

- if(item.IsEnabled == false)
- return x == false;

instead of

- if(!item.IsEnabled)
- return !x;

The reason for this is that I find negation harder to see visually and easier to introduce accidental logical errors. I reason that by having only 'is' instead of 'is' and 'not', reading code ought to be easier (readability).

For example:

if this.Enabled is false and this.Visible

instead of

if not this.Enabled and this.Visible

Although I think this works the best for me, I could just be convincing myself of that it does. Sure, it does not matter in my private projects; what works best for me is to be preferred. However, in FOSS projects or working professionaly, I would obviously want to use what actually is best and be able to defend my position on the subject. Therefore, I want to ask: What is the best way of approaching this in general?

Thanks!

Upvotes: 2

Views: 99

Answers (1)

starsplusplus
starsplusplus

Reputation: 1253

They are exactly equivalent, so there are no optimisation issues or anything like that.

The only determining factors are readability and personal preference.

As for...

I would obviously want to use what actually is best and be able to defend my position on the subject

... sounds to me like you are able to defend your position fairly well. Just read what you wrote in the question :)

Upvotes: 1

Related Questions