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.
[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!