Article 1VKTX CodeSOD: As The World Ternaries

CodeSOD: As The World Ternaries

by
Remy Porter
from The Daily WTF on (#1VKTX)

Ah, the ternary operator. At their worst they're a way to obfuscate your code. At their best, they're a lovely short-hand.

For example, you might use the ternary operator to validate the inputs of a function or handle a flag.

Adam Spofford found this creative use of the ternary operator in a game he's developing for:

 this.worldUuid = builder.worldId == null ? null : builder.worldId; this.position = builder.position == null ? null : builder.position; this.rotation = builder.rotation == null ? null : builder.rotation; this.scale = builder.scale == null ? null : builder.scale; this.worldUuid = builder.worldId; this.position = builder.position; this.rotation = builder.rotation; this.scale = builder.scale;

Curious about how this specific block came to be, Adam poked through the Git history. For starters, the previous version of the code was the last four lines- the sane lines. According to git blame, the project lead added the four ternary lines, with a git comment that simply read: "Constructing world details". That explains everything.

proget-icon.png [Advertisement] Universal Package Manager - ProGet easily integrates with your favorite Continuous Integration and Build Tools, acting as the central hub to all your essential components. Learn more today! TheDailyWtf?d=yIl2AUoC8zAgHgIRGN74H0
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