[thelist] Coning conventions, pet peeves
jason.handby at corestar.co.uk
Thu Apr 28 13:28:19 CDT 2016
> Over the last several years, I've been more frequently seeing code where
> the boolean value comes first in a comparison - if (true === pizzaStatus()).
> Who would write it like that? Do these people want me to recognize just how
> thoroughly they understand that if A equals B, B equals A?
> This infuriates me as it seems to be a perfect example of willfully ignoring
> conventions that make code easier to read.
> When writing code that will be read by another programmer, you want to
> allow the programmer to spend all their effort into understanding what the
> code DOES. This means it's equally important for the reader to spend no
> effort understanding what the code SAYS.
> True is pizza status.
> In English, the subject comes before the object when making a statement -
> "Pizza status is true!" not "True is pizza status". The meaning of the second
> formulation is clear but it takes half a second to understand it.
> Working with code requiring you to think for half a second about what it SAYS
> every few lines is much more exhausting than working with code that reads
> So, today, I saw someone say (false === strpos( $content, $chr)) and I had a
> conniption. I scanned the page to find the name of the author I was furious
> with to find it was an Arab name.
> Arabic reads right to left. Oh. Hebrew too. Chinese too, I think. Other
> languages. Hmm.
> Most of the time I see the "backwards" order it's on StackOverflow or some
> such thing - not in a book or article that has passed through an editorial
> review or maybe even been proofread by the author.
> When one is writing for an English reading audience, one should still use
> accepted conventions but I hope it will irritate me less when I see this
> transgression knowing that in many cases it arises out of the writer's trouble
> writing in an unfamiliar language rather than trying to be cute.
I don't personally do it - but the reason for doing it is to make it harder to actually perform an assignment when you mean to perform a comparison.
For example, in C / C#, if you meant to write
if (c == true)
and you actually wrote
if (c = true)
you'd inadvertently overwrite the contents of c. By putting the comparison the other way around - making sure that the thing to the left of the operator is not an lvalue - you make it harder to make this mistake.
Of course modern compilers will usually warn you if you do single-equals when they think you mean double-equals. And in your example of the pizzaStatus() function it's not necessary, as pizzaStatus() isn't an lvalue.
More information about the thelist