3

Is there any particular reason for != to be used more than ==?

I have noticed that != seems more common, but wondered if there was a reason for this.

ArjunShankar
  • 23,020
  • 5
  • 61
  • 83
Toby
  • 9,696
  • 16
  • 68
  • 132
  • 10
    You know they do different things, right ? – cnicutar Jul 27 '12 at 09:57
  • 4
    I was curious if this is true. I grepped for it in GCC sources, and here's what I got: `grep -B 2 "==" *.c | grep "if" | wc -l`: 22.3k hits. The same query with `!=` gave only: 9.6k hits. Looks like GCC developers like `==` more. – ArjunShankar Jul 27 '12 at 10:11
  • 5
    @ArjunShankar: I did the same thing with the Linux sources: `==` has 137k occurrences, and `!=` has only 83k occurrences. It seems like the data doesn't support the assertion in the question. – Greg Hewgill Jul 27 '12 at 10:15
  • 1
    @GregHewgill - Yes. Bias either way is an interesting thing to see. Another interesting statistic to see would be: "how frequently is an `if` with `==` *taken*, and how frequently does the same happen for `!=`. – ArjunShankar Jul 27 '12 at 10:18
  • 1
    The purpose of each statement is entirely difference, one is testing for equality and one is testing to see if something is equal to everything but the equality statment. – Security Hound Jul 27 '12 at 10:20
  • @Ramhound - I think the OP is aware of this. The question, as I understand it, is: Why programmers prefer one form over the other when writing code? – ArjunShankar Jul 27 '12 at 10:22

6 Answers6

3

If you have code like this:

if (a == b)
{
    // block 1
}
else
{
    // block 2
}

It can be rewritten as:

if (a != b)
{
    // block 2
}
else
{
    // block 1
}

These two examples do the same thing. Neither is more efficient than the other. If one is used more than the other it may be personal preference.

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
  • 6
    The second version does have a slight advantage that it is more difficult to make the mistake of just using a single = by accident. – Ed Heal Jul 27 '12 at 10:00
  • 2
    @EdHeal true but most (all?) major compilers will raise warnings when doing that – stijn Jul 27 '12 at 10:02
  • @Mark - Isnt != faster as compared to == ? – chochim Jul 27 '12 at 10:03
  • 3
    @chochim: Why would that be true? – Greg Hewgill Jul 27 '12 at 10:04
  • 2
    @chochim look here - http://stackoverflow.com/questions/1029992/is-the-inequality-operator-faster-than-the-equality-operator – TheNewOne Jul 27 '12 at 10:04
  • 1
    @stijn - Trouble is that lots of programmers do ignore warnings. I personally don't but have been on projects where lots of warnings are issued. – Ed Heal Jul 27 '12 at 10:08
  • unfortunately that is true. Thoug I'd say those programmers will have other more serious problems as well. If you can't even listen to the compiler, your whole mindset is imo wrong. – stijn Jul 27 '12 at 10:23
  • 1
    I find the first version a lot easier to read. The second one looks a lot like a double negation - if not equal, and not not equal. – Bo Persson Jul 27 '12 at 11:55
2

You should use == or != according to the logical condition you are trying to express. If you care about both the true and false conditions, i.e. both the if and else parts then the net effect of switching (if it's a simple comparison) is just which code needs to appear in which part.

There is a coding style, see (the book) Code Complete's section on Forming Boolean Expressions Positively which suggests that boolean expressions that express something positive are easier to understand than those expressing something negative.

If you only care about the condition when it evaluates to true, e.g. you only care when you have equality (for ==) or do not have equality (for !=) then you'll not require an else section if you pick the correct one.

borrible
  • 17,120
  • 7
  • 53
  • 75
1

Most of the time the preference of using != or == will depend on the content:

resultOfOperationOrCall = operationOrCall(...);
if (resultOfOperationOrCall != somePredefinedErrorTypeValue) {
// Normal processing
} else {
// Exception/error processing
}

Here using != is logically clearer than using ==

Germann Arlington
  • 3,315
  • 2
  • 17
  • 19
  • What's wrong with `if (resultOfOperationOrCall == resultOK)` ? – Bo Persson Jul 27 '12 at 11:57
  • I don't see how `!=` is logically clearer than using using `==` here. The code as written is unnecessarily fragile. It assumes the absence of a specific error is the same as success, which is usually a bad assumption. What if that function returns other errors? Even if it doesn't today, what if it's changed to return other errors in the future? Also, I personally prefer having failure paths first; they're usually short, and it's harder to follow code when the failure path is in some `else` clause that's dozens of lines away from the `if` condition. – jamesdlin Jul 27 '12 at 12:14
  • If you have somePredefinedErrorTypeValue but DON'T have normal result values. In my comment somePredefinedErrorTypeValue may be null, anything that is NOT null is normal, and only null is an error (as an example) – Germann Arlington Jul 27 '12 at 12:38
1

I prefer != because it's more explicit (and there is less chances to write = as mistake).

md5
  • 23,373
  • 3
  • 44
  • 93
  • != or == comparison is done on CPU register all bits at once in one tick – Germann Arlington Jul 27 '12 at 10:51
  • 1
    Why does `==` need to test all the bits? It only needs to test up to the first mismatch. What if all but the last bit were the same? `!=` still has to test all the bits in this case. There is no difference here between `==` and `!=`. – CB Bailey Jul 27 '12 at 11:11
  • If you are afraid of typos, how do you avoid writing `<=` by mistake? – Bo Persson Jul 27 '12 at 12:01
0

As assembly code for both == and != present so no issue of efficient or inefficient code for x86 architecture.So it is up to you .you can use any one of them.

if for some machine if assembly code not available then efficient or inefficient comes it to picture as that is achieved by compiler by performing some additional operation(that is adding additional assembly code).

rajesh6115
  • 705
  • 9
  • 21
0

Its wholly and solely depends on the perception & need of user. I don't see any reason on why would one use != more than ==