4

I've been working on a Maven project consisting entirely of Java, and lately started to mix Scala code into it.

I'm amazed by the great expressiveness Scala offers, the easy use of scala-maven-plugin, and especially the incredible interoperability between Java and Scala.

However, I hit one inconvenience; according to the Maven's convention, Java's source code goes into src/main/java, whereas Scala's into src/main/scala. I found it quite cumbersome because I have to frequently go back and forth Java and Scala source files and every time I have to traverse the deep hierarchy of package directories (I often close tabs to keep my editor from cluttered).

So the question is: Is it recommended to maintain separate directories src/main/java and src/main/scala? If so, why?

To add more background, I've been working on the web application framework Wicket, whose convention is to put the HTML files alongside with their corresponding Java files. If we keep the directories separated, naturally the HTML files are separated as well (I don't think putting Scala files and corresponding HTML files in different directories makes sense). And then it goes "why I can't find Foo.html? Oh, I was looking for the wrong directory."

The source files themselves are very easy to distinguish both by humans and by machines by inspecting their extensions. I configured pom.xml to handle both Java and Scala put together in src/main/java and it worked (compiles and runs). On the other hand, separating directories poses a risk of defining conflicting classes in Java and in Scala, a careless mistake.

Well, I don't want to name a directory java if it contained not only Java's but also Scala's. But this is the only point I can come up with for separating directories.
(Edit: I've come up with a workaround-interpretation; let us think java stands for Java Virtual Machine. In this way, having src/main/c doesn't contradict if we ever decided to use JNI because C doesn't run on JVM.)

Oh, and one more point; my project is not intended as an open-source project; development convenience is preferred than trying hard to follow conventions.

This question is inspired by this answer.

Community
  • 1
  • 1
Ohashi
  • 397
  • 3
  • 14
  • 1
    I use an IDE and how the files are laid out doesn't really matter. I can use ctrl+click on any class to open it up. Perhaps it's time to switch from using an editor to using a tool design for Java/Scala. Also IDEs do much, much more for you. You won't believe how much time you wasted before you used an IDE. – Peter Lawrey Oct 18 '16 at 11:12
  • 1
    I know IDE. I had used Eclipse for over 5 years and switched to a terminal-based environment (vim). I'm using eclim so technically I'm still using Eclipse but failing to configure it to work with a Java/Scala mixed project. I'm afraid I don't want to go back to that mouse-oriented environment. And I'm fed up with configuring "another OS" just to use filer/jumper/import-organizer etc... If only it was not an E but a T; I love to use _Tools_ by my own hands to study what's going on under a hood while I work on coding. – Ohashi Oct 18 '16 at 11:51
  • I use IntelliJ on lots of different machines and I don't find I need to reconfigure anything (perhaps I am just happy to live with the defaults) You can use hotkeys for every function and I don't feel I use the mouse that much. – Peter Lawrey Oct 18 '16 at 12:31

1 Answers1

0

I'd say yes, re-use code as much as possible. Maybe in future you can use this Java piece somewhere else...

As you probably know, you can use Java in Scala projects but not Scala in Java projects. So in this specific example it will help you with (future?) Java projects. If you want to re-use a piece of your Java code you can do that in either Java projects as well as Scala projects.

So i.m.h.o. it doesn't stop at the src/main/... but you should really put them even in different components.


Btw, little side note: if I'm correct, Wicket allows you to put the html somewhere else too, even in a different project... I saw it being handy (only) once, where we had to create different frontend for different clients of us. The java code stayed the same, the wicket-id's as well, but the html changed everywhere. Though it did give us some problems as well using the Qwicky plugin, as it could not find the html files in our IDE anymore.

Jeroen van Dijk-Jun
  • 1,028
  • 10
  • 22
  • 1
    Do you mean source-compatibility? Then it's programmers' responsibility to understand the code. `java` can run scala programs with appropriate classpath settings, so binary compatibility (or run-ability) shouldn't be an issue. – Ohashi Oct 18 '16 at 11:40
  • Ah yes you're right. Then still in some way I'd go for splitting in the different folders, or even projects just to not get mixed up I guess... – Jeroen van Dijk-Jun Oct 18 '16 at 20:52
  • In some way it feels like modifications are done easier in the future, but I can't find a really good reason yet.. – Jeroen van Dijk-Jun Oct 18 '16 at 21:00
  • If Java code and Scala code depend on each other (which in my case), separating them into separate directory doesn't make sense. Also, if we wanted to re-separate the mixed directory again, we can write a script for that. I already have distinct projects for UI and core logics for reusability. In term of reusability, Scala is more like a framework rather than a language. It still doesn't make sense to separate _by language_ for me. – Ohashi Oct 18 '16 at 22:52