Quantcast
Channel: code patterns – Codacy | Blog
Viewing all articles
Browse latest Browse all 8

One less Exception

$
0
0

java.util.NoSuchElementException: No value found for 'USD'
at scala.Enumeration.withName(Enumeration.scala:124)

Dammit! I hate it when code blows in production.
For some cases there’s just no tests or static analysis that can cover you.

This is one of those cases. Calling the withName method with an inexistent value in an Enumeration will blow up. Sure, it throws an Exception, but since Scala has unchecked exceptions, the compiler won’t warn you.

Unfortunately, it wasn’t the first time we had exactly this problem, so we agreed to start avoiding the withName method and writing it as:

Since find returns an Option, the code needs to be ready to deal with the possibility of a non existing value.

 

We also want all of our users to stay safe, so today we’re adding a new code pattern: Prohibit withName on Enumeration.
You can check your code for this issue and fix it before it blows in production!

 

The post One less Exception appeared first on Codacy | Blog.


Viewing all articles
Browse latest Browse all 8

Trending Articles