Mix Scala and Java in the one codebase

The minimum Java version, that MyCollab supports is Java 7. Since we write the product not the library so we are not asked to support the older Java 5 or 6. However, it is the challenge in writing code with Java 7 syntax. There are several things make our developers feel not comfortable, they are:

  • Getter/Setter: of course we can avoid to use Getter/Setter but set the scope of fields are public, but it is the Java convention
  • Mutable objects: it makes us very carefully, and sometimes there were few developers made mistakes because we passed the Java objects to fill an editable form if the users edit several attributes and cancel to submit the value but the form is already filled the text change to the attribute. We must use the Clone library to clone the object and pass to the form whenever we call that method. We receive a lot of potential issues from Findbugs tool because we use the mutable objects as well.
  • Boilerplate code when we do some actions over the collection objects. Just image how many lines of code we must write to find the element meets some criteria. That makes our Java sources are harder to capture the developers’ ideas. It looks like the machine language, not human language.

We have a list of the alternative programming languages to evaluate; it is interop with Java language. Finally, we have two candidates: Scala and Groovy that meet our requirements. We choose Scala over Groovy for its speed, purely functional programming, and default immutable objects. We move part of Java code to Scala, and we save more than 40% line of codes.

Screen Shot 2016-06-16 at 9.36.13 PMFor the fun factor, if we use the COCOMO model, we reduce the number lines of cost, it will reduce the project cost. That means from more than one year; my team did not provide any extra value :). Of course, it is just a fun fact because to discuss the correctness of the COCOMO model is out of our scope.
IntelliJ has the great Scala plugin that helps developer can convert the Java code to Scala code; you can play with both Java and Scala in the one Java project. The rest challenge is we let these two languages can be compiled well with the Maven tool. Thanks for the community help, it is not the tough task. We use the Maven plugin scala-maven-plugin and add it in our build process.

Th plugin Build Helper Maven Plugin will help us to add Scala sources and tests to the Maven build life cycle

and you put the Scala codes follow the Maven conventions, source files in the folder scr/main/scala and test files in the folder src/test/scala.

In the future blog post, I will share the challenges of Java and Scala interoperability issues and our constraint of support Java 7. Please keep following us.

Leave a Reply

Your email address will not be published. Required fields are marked *