2
if (0 == strcmp(val1,val2)) or (strcmp(val1,val2)  == 0)

vs

if(! strcmp(val1,val2))

Any studies or style guides that support one these styles over the other?

closed as primarily opinion-based by gnat, Robert Harvey, user22815, Bart van Ingen Schenau, Nicol Bolas Mar 14 '17 at 2:36

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    The first style (where the operands are reversed) is a holdover from those days when compilers weren't sophisticated enough to warn you about using an = instead of an ==. As to checking for zero, every c programmer already understands that you don't have to. – Robert Harvey Mar 9 '17 at 16:32
  • So part of my question was going to be that I find the second style harder to read vs == 0 in either direction. So I was wondering if its just a me readability thing or if there is some general guidance which I couldn't find. – Travis Mar 9 '17 at 16:34
  • Nobody uses the first style anymore, unless they're deeply ingrained in the past. – Robert Harvey Mar 9 '17 at 16:37
  • 1
    Every popular language has a consensus style and you should follow it, even if it feels unnatural to you. Simplest is usually best, especially in C. The language and library was explicitly designed so we can write "if (! strcmp(...))" so that's what you should write. – kevin cline Mar 9 '17 at 17:13
  • 5
    Since you've included the C++ tag, I'll mention that for it, the proper course of action is usually "none of the above". Use std::string, so this is if (val1 != val2). – Jerry Coffin Mar 9 '17 at 18:17
4

My preference is strcmp(x, y) == 0. The reason for this is that this style uses the same operator that would be naturally used in the expression x == y (that just compares pointers for equality and thus doesn't do what you want).

Also, you can do inequality comparisons as well: if you want to check whether x < y, you can do it by strcmp(x, y) < 0. The operator is the same.

Now, the only reason for 0 == strcmp(x, y) is that some old compilers may not warn if you use the = operator inside an if statement. The problem with this way is that if you want to check x < y, you do it by 0 > strcmp(x, y) so the operator just switched to the opposite operator. Not at all intuitive. Because modern compilers warn about "if (x = 0)" there is no reason to use these so-called "Yoda conditions".

Also, !strcmp(x,y) looks like "x not equal with y", which is not what it does. So, I would recommend not to use the ! operator with strcmp().

  • 1
    "Also, !strcmp(x,y) looks like "x not equal with y", which is not what it does. So, I would recommend not to use the ! operator with strcmp()" this is how I kinda feel too. But I wasn't sure if it was something I should try to push for my teams style or not. – Travis Mar 10 '17 at 17:14
2

One mnemonic I find useful is "not means naught" whenever I am converting an integer into a bool.

As to the unnaturalness, unfortunately you have to get over it, because it is an example of idiomatic usage of the language.

If you are allowed to define utility functions, you can wrap the whole thing into new functions like bool Equals(...) and bool EqualsIgnoreCase(...), which would make a C#-turns-C++ programmer happy.

However, unless your entire team agrees, and your project's stakeholders also accept the consequence that the code base doesn't conform to C++ idioms, borrowing heavily from the idioms of a different language is ill-advised. It prevents the stakeholders from choosing to open-source the project, or making it difficult to recruit more experienced C++ developers into the project.

Not the answer you're looking for? Browse other questions tagged or ask your own question.