TITLE: == false AUTHOR: Eugene Wallingford DATE: December 20, 2017 3:51 PM DESC: ----- BODY: While grading one last project for the semester, I ran across this gem:
if checkInputParameters() == False: [...]This code was not written by a beginning programmer, but a senior who likely will graduate in May. Sigh. I inherited this course late in the semester, so it would be nice if I could absolve myself of responsibility for allowing a student to develop such a bad habit. But this isn't the first time I've seen this programming pattern. I've seen it in my courses and in other faculty's courses, in first-year courses and fourth-year courses. In fact, I was so certain that I blogged about the pattern before that I spent several minutes trolling the archive. Alas, I never found the entry. Don't let anyone tell you that Twitter can't be helpful. Within minutes of tweeting my dismay, two colleagues suggested new strategies for dealing with this anti-pattern. Sylvain Leroux invoked Star Trek:
James T. Kirk "Kobayashi Maru"-style solution: Hack CPython to properly parse that new syntax:Maybe I'll turn this into a programming exercise for the next student in my compiler class who wanders down the path of temptation. Deep in my heart, though, I know that enterprising programmers will use the new type of statement to write this:
unless checkInputParameters(): ...
unless checkInputParameters() == True: [...]Agile Hulk suggested a fix:
if (checkInputParameters() == False) == True:This might actually be a productive pedagogical tack to follow: light-hearted and spot on, highlighting the anti-pattern's flaw by applying it to its own output. ("Anti-pattern eats its own dog food.") With any luck, some students will be enlightened, Zen-like. Others will furrow their brow and say "What?" ... and even if we look past the boolean crime in our featured code snippet, we really ought to take on that function name. 'Tis the season, though, and the semester is over. I'll save that rant for another time. -----