foundationkit / FoundationKit

Everything that really should be in Foundation, but isn't. Future-proof with ARC
Other
80 stars 9 forks source link

Code style for logical operators #16

Closed myell0w closed 13 years ago

myell0w commented 13 years ago

I don't like using logical operators for other types than BOOL, as already spotted somewhere:

if (!strcmp(s1,s2))

What do others think? I much prefer

if (strcmp(s1,s2) == 0)

in this example, for clarity. Same goes for

 if (!pointer)

vs

 if (pointer == nil)

in my opinion.

eaigner commented 13 years ago

I prefer the !result way because if you read from left to right I immediately read NOT result and it's more concise. But I'm fine with either way, since the ! way has it's own drawbacks (e.g. you can overlook it more easily).

MSch commented 13 years ago

I prefer !result since, as @eaigner said, you read it as NOT result. But this only applies to BOOL results.

For strcmp clearly you must never use !strcmp, since it doesn't return BOOL but an Ordering type LT | EQ | GT

eaigner commented 13 years ago

Well, ! evaluates if the first bit is 0 or not (resp. negates the bits). So it works for ints as well as for bools.

MSch commented 13 years ago

Yeah sure it works, this is C.

But ! implies that it's operating on a boolean value, not an 'enum'. So using ! makes no sense to me.

It's a smart trick that saves a few characters only to confuse people. Sure, everyone probably knows the real type of strcmp, but not everyone knows it for obscurefunction.

myell0w commented 13 years ago

Yes that's what I meant, for BOOL !value is of course fine, but not for other data types imho. Same applies to other logical operators like && imo.

sent from phone

Am 07.07.2011 um 20:33 schrieb MSchreply@reply.github.com:

I prefer !result since, as @eaigner said, you read it as NOT result. But this only applies to BOOL results.

For strcmp clearly you must never use !strcmp, since it doesn't return BOOL but an Ordering type LT | EQ | GT

Reply to this email directly or view it on GitHub: https://github.com/foundationkit/FoundationKit/issues/16#issuecomment-1524957

myell0w commented 13 years ago

Same argument here. One that doesn't know what strcmp or obscurefuction returns maybe thinks it's a BOOL and gets the logic wrong, when ! is used.

eaigner commented 13 years ago

As I said, I'm fine with either way. So ! for boolean types and == number for numbers?

MSch commented 13 years ago

! only for boolean types, proper operators (whatever they may be) for everything else

myell0w commented 13 years ago

Sounds like a deal.

!, && and || 

only for BOOL, other types get converted to BOOL by using an equation

MSch commented 13 years ago

Marking this as needs final approval by @steipete

steipete commented 13 years ago

Fine with me, you guys found a consent already.

To sum it up, "if (strcmp(s1,s2) == 0)" should definitely used like this, not with the ! thing. Also every other function/var that does not return BOOL.

And like Matthias said !, && and || only for BOOL.

But on pointers, i would still allow a ! as an expection - it's an often seen context and i honestly like it :)