Article 3Z0ZC CodeSOD: Switch On Suppression

CodeSOD: Switch On Suppression

by
Remy Porter
from The Daily WTF on (#3Z0ZC)

Krista noticed our article explaining that switches were replacements for ifs. She sent in a version she found in her codebase, around the same idea:

@SuppressWarnings("incomplete-switch")@Transactionalpublic void removeAssetFromPackage(Package pkg, Asset assetToRemove) {pkg.getAssets().remove(assetToRemove);// Delete from DB and asset store.removeAsset(pkg, assetToRemove);// If we're removing LIVE asset, also delete AsyncJobs.switch (assetToRemove.getType()) {case LIVE:asyncJobService.removeAsyncJobsForPresentation(pkg);break;}// Flush package cache.cacheInvalidationService.invalidatePresenationCache(pkg);}

Once again, we use a switch instead of an if. Perhaps this was premature flexibility- there are obviously other states the getType method could return. Maybe, someday in the future, they'll need to do other things inside that switch. Until then, it's just the sort of thing the compiler will throw warnings about, since there's no default case.

Oh, except, of course, they suppressed the warning up top.

A quick search in Krista's codebase for @SuppressWarnings("incomplete-switch") finds dozens of usages of this pattern.

raygun50.png [Advertisement] Forget logs. Next time you're struggling to replicate error, crash and performance issues in your apps - Think Raygun! Installs in minutes. Learn more. TheDailyWtf?d=yIl2AUoC8zA182JjnXOLdE
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