Article 5865G CodeSOD: Get My Switch

CodeSOD: Get My Switch

by
Remy Porter
from The Daily WTF on (#5865G)

You know how it is. The team is swamped, so you've pulled on some junior devs, given them the bare minimum of mentorship, and then turned them loose. Oh, sure, there are code reviews, but it's like, you just glance at it, because you're already so far behind on your own development tasks and you're sure it's fine.

And then months later, if you're like Richard, the requirements have changed, and now you've got to revisit the junior's TypeScript code to make some changes.

switch (false) {case (this.fooIsConfigured() === false && this.barIsConfigured() === false):this.contactAdministratorText = 'Foo & Bar';break;case this.fooIsConfigured():this.contactAdministratorText = 'Bar';break;case this.barIsConfigured():this.contactAdministratorText = 'Foo';break;}

We've seen more positive versions of this pattern before, where we switch on true. We've even seen the false version of this switch before.

What makes this one interesting, to me, is just how dedicated it is to this inverted approach to logic: if it's false that foo" is false and bar" is false, then obviously they're all true, thus we output a message to that effect. If one of those is false, we need to figure out which one that is, and then do the opposite, because if foo" is false, then clearly bar" must be true, so we output that.

Richard was able to remove this code, and then politely suggest that maybe they should be more diligent in their code reviews.

buildmaster-icon.png [Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how! TheDailyWtf?d=yIl2AUoC8zAHorRbRApbkU
External Content
Source RSS or Atom Feed
Feed Location http://syndication.thedailywtf.com/TheDailyWtf
Feed Title The Daily WTF
Feed Link http://thedailywtf.com/
Reply 0 comments