Coding style in CPython implementation

Rhodri James rhodri at kynesim.co.uk
Mon Oct 30 10:31:00 EDT 2017


On 28/10/17 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) { ... },

"if (variable == NULL)" emphasises that "variable" is a pointer 
variable, aiding readability.

> b) if (-1 == variable) { ... } in place of if (variable == -1) { ... }, and

It's just not natural English.  It's safer to write it with the constant 
first, but modern compilers will warn you about assignments in 
conditions these days, and it's not worth the reduction in readability 
in my opinion.

(Also, if you aren't using the -Werror -Wall flags to the compiler, you 
deserve everything that will be coming to you.)

> c) if (variable) { ... } in place of if (variable) { ... } ?

I assume you mean "if (variable != NULL)" here.  Again, it emphasises 
the type; variable will be a pointer.

-- 
Rhodri James *-* Kynesim Ltd



More information about the Python-list mailing list