CodeX
Published in

CodeX

Avoid Yoda conditions in Perl you should

Paul VanDerWerf from Brunswick, Maine, USA, CC BY 2.0, via Wikimedia Commons

I remem­ber a brief time in the mid-​2000s insist­ing on so-​called ​“Yoda con­di­tions” in my Perl. I would place con­stants to the left of equal­i­ty com­par­isons. In case I acci­den­tal­ly typed a sin­gle = instead of ==, the com­pil­er would catch it instead of blithe­ly assign­ing a vari­able. E.g.:

if ( $foo == 42 ) { ... } # don’t do this
if ( 42 == $foo ) { ... } # do this
if ( $foo =

--

--

--

Everything connected with Tech & Code. Follow to join our 900K+ monthly readers

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Mark Gardner

Mark Gardner

Just another desperate Perl hacker

More from Medium

Why Clojure?

Euclid Algorithm with Proofs

Why you should write your code like a recipe

Pair Programming — why bother?