Article 1VBWR CodeSOD: Exceptional Condition

CodeSOD: Exceptional Condition

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

"This is part of a home-grown transpiler"", Adam wrote. I could stop there, but this particular transpiler has a" unique way of deciding if it should handle module imports.

Given a file, this Groovy code will check each line of the file to see if it includes an import line, and then return true or false, as appropriate.

private static boolean shouldConvert(String headerPath) { File headerFile = new File(headerPath) try { headerFile.eachLine { if (it.contains("MODULE_INCLUDE core.") || it.contains("import core.")) { throw new Exception("true, we found a good import") } } // Never found a valid MODULE_INCLUDE or import; don't convert it throw new Exception("false, no good import found") } catch (Exception e) { return e.getMessage().contains("true") }}

Now, I'm no Groovy expert, but I suspect that there's probably an easier way to return a boolean value without throwing exceptions and checking the content of the exception message.

Adam adds: "I especially envisioned changing the second exception to something like 'false, it is true that we didn't find a good import.'"

buildmaster-icon.png [Advertisement] Application Release Automation - build complex release pipelines all managed from one central dashboard, accessibility for the whole team. Download and learn more today! TheDailyWtf?d=yIl2AUoC8zAhoZO-1CB2z4
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