Coding style in CPython implementation

bartc bc at freeuk.com
Sat Oct 28 14:54:38 EDT 2017


On 28/10/2017 19:42, Στέφανος Σωφρονίου wrote:
> Greetings everyone.
> 
> I have noticed that in many if conditions the following syntax is used:
> 
> a) if (variable == NULL) { ... }
> b) if (variable == -1) { ... }
> c) if (variable != NULL) { ... }
> 
> What I wanted to ask is, is there a particular reason for not choosing
> 
> a) if (!variable) { ... } in place of if (variable == NULL) { ... },
> b) if (-1 == variable) { ... } in place of if (variable == -1) { ... }, and
> c) if (variable) { ... } in place of if (variable) { ... } ?

(Presumably you meant variable != NULL)

> Especially the (b) syntax is extremely dangerous to assign -1 to variable in case of an accidental mistyping of equals sign; it had happened countless times by now to to many of us that use various C-family languages.
> 
> Is there a particular reason for using this specific coding style?

Which one do you think is more readable?

Which style would you prefer to read?

Or do you prefer the more cryptic style in open source code?


(The = vs. == issue is of one of numerous design flaws in C, but 
uglifying source code to get around it isn't the answer.)



-- 
bartc



More information about the Python-list mailing list