Home Codacy News 3 new Scala code patterns

3 new Scala code patterns




Read about 3 new Scala code patterns we just added to our platform in our mission Codacy to support multiple coding languages effectively.

1. Avoid using DateTime.now without a timezone (Error Prone)

Using DateTime.now without specifying a timezone can lead to error prone code with regard to dealing with other timezones than those initially considered, as well as with daylight saving time.

For instance, the following example:

val now = DateTime.now

could easily be written as:

val now = DateTime.now(DateTimeZone.UTC)

2. Prefer using .nonEmpty (Code Style)

The collections libraries usually provide a method for determining if the iterable has, at least, one element, with:

val hasElements = SomeSequence.nonEmpty

This is a bit cleaner than verifying explicitly the length or the size:

val hasElements = SomeSequence.length > 0
 val hasElementsUsingSize = SomeSequence.size > 0

3. Prevent XML parsing attacks (Security)

XML External Entity (XXE) attacks can occur when an XML parser supports XML entities while processing XML received from an untrusted source.

Vulnerable Code:

SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
 parser.parse(inputStream, customHandler);

Solution using “Secure processing” mode:

SAXParserFactory spf = SAXParserFactory.newInstance();
 spf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
 SAXParser parser = spf.newSAXParser();
 parser.parse(inputStream, customHandler);

Solution disabling DTD:

SAXParserFactory spf = SAXParserFactory.newInstance();
 spf.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
 SAXParser parser = spf.newSAXParser();
 parser.parse(inputStream, customHandler);

Enabling patterns with Codacy

When new patterns are added, they’re disabled for already existing projects.

To enable them for a project first head to the project and hit “Code Patterns” on the top right corner. You’ll be able to filter the patterns by language or category:

selecting language for patterns

To enable the new patterns simply look for the “New” tag on them and hit their respective checkbox:

enabling patterns

If you’re interested, there’s also a “Fork pattern” button on the bottom right corner of the details of every Scala pattern.

fork pattern

Hitting this button will take you to our Beta Pattern Creator with the current pattern loaded; you can then edit it to create your own:

pattern creator - beta

Have fun, code well!

Edit: We just published an ebook: “The Ultimate Guide to Code Review” based on a survey of 680+ developers. Enjoy!

About Codacy

Codacy is used by thousands of developers to analyze billions of lines of code every day!

Getting started is easy – and free! Just use your  GitHub, Bitbucket or Google account to sign up.



Please enter your comment!
Please enter your name here

Subscribe to our newsletter

To be updated with all the latest news, offers and special announcements.

Recent posts

OWASP Top 10 vulnerabilities and how Codacy helps to address them

In today’s modern, digitized world, security is more important than ever to respond to growing threats. Every web application comes with system vulnerabilities, and...

2020, It’s a wrap! 🎊

Looking back at 2020, it has been a year of change and innovation for Codacy. We became fully remote and will continue to be,...

Migrating to React: Typed named routes in react-router and Typescript

INTRODUCTION If you’re a regular user of Codacy, you might have noticed a few changes over the course of this year on some pages. We’re...

Introducing Pulse to help companies achieve elite engineering performance

At Codacy, we envision a future where everyone can impact the world by crafting complex software with confidence and focus at the speed of...

First QA Engineer in a Startup

This is the story of how I joined a startup as the first QA Engineer in the company.  My name is Bruno Medley, I'm a...