Comparison Style

Dave Angel davea at davea.name
Thu Apr 25 23:43:08 EDT 2013


On 04/25/2013 10:48 PM, Chris Angelico wrote:

     <SNIP>

> Also, this protection helps only when the "constant"
> is actually something the compiler knows is a constant - it doesn't
> work in a search function, for instance:
>
> char *strchr(char *string, char findme) {
>      while (*string) {
>          if (*string==findme) return string;
>          ++string;
>      }
>      return 0;
> }

Sure, but if I were coding in C again, I'd have made that function signature

char *strchr(char *string, const char findme) {
    or maybe
char *strchr(const char *string, const char findme) {

>
> If you switch the order of operands in that, the compiler won't help
> you.

Yes, it would.

> Plus it "reads" wrong. So the convention is still
> variable==constant.

In my case, after having it drilled in that you're "supposed" to put the 
constant first, I realized that I never had any problem with using =, 
because as soon as I questioned the order, I just double-checked that I 
was using ==.  At that point, there was no longer any benefit to making 
the order backwards.


-- 
DaveA



More information about the Python-list mailing list