Coding style in CPython implementation

Στέφανος Σωφρονίου stefanossofroniou542 at gmail.com
Sat Oct 28 19:20:54 EDT 2017


On Saturday, October 28, 2017 at 9:54:30 PM UTC+3, bartc wrote:
> 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)

Yep, I forgot to show the != NULL part; good catch!

> 
> > 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.)

I understand what you are saying and I don't have a styling preference, at least in CPython's case.

I do believe though that if (!d) is a lot clearer than if (d == NULL) as it is safer than falsely assigning NULL in d, by pure mistake.

But indeed it's a matter of taste.

I just wanted to see whether there is a certain favor around such coding style, that's all.

> 
> 
> 
> -- 
> bartc




More information about the Python-list mailing list