A New Java Library for Amazing Productivity
Join the DZone community and get the full member experience.Join For Free
editor's note: this article has been republished with the permission of the original author, graham lea .
i’ve found this great java library that can make developers more efficient in pretty much every source file they write. it has the following features:a broad and powerful collections framework:
- collection methods that greatly reduce boilerplate
- immutable collections that don’t have mutation methods (unlike java.util classes where e.g. list.add() throws an exception if the list is immutable)
- an awesome switch-like function that doesn’t just match numbers, enums, chars and strings, but can succinctly match all kinds of patterns in lots of different classes, even in your own classes
- an annotation that automatically writes meaningful equals, hashcode and tostring methods for classes whose fields don’t change (without using reflection)
- a feature for avoiding the duplication between fields and constructor parameters by declaring fields in your constructor signature
- the ability to automatically generate property getters and setters at compile time
- the option to define default values for parameters of methods rather than creating lots of overriding methods to do the same thing
- the option of writing the names of parameters when you call a method to make your code more readable
- a solution for using mixins, i.e. multiple inheritance of method definitions (not just interfaces) without ambiguity/the diamond problem
- a nifty little class that avoids nullpointerexceptions by avoiding null altogether and instead expressing in your source code that you’re passing around or returning something that might not be present, forcing you to handle the possible ‘nothing’ but only when you want to use it
- a way to give the appearance of adding new methods to existing classes (only where you want to use them), similar to c#’s extension methods and objective-c’s categories.
- the ability to easily pass around blocks of code for other classes to execute (without having to wrap them in a callable class and a call() method signature)
- some neat features for doing easy concurrent programs like parallel collections and an actor package
- like all good libraries, you don’t have to convert your whole system to use it all at once; you can just use it in places where it makes the most sense to begin with. it will integrate very smoothly with all your existing code.
there are of course a few costs that come along with the benefits of using this java library, but they’re fairly minor:
- your developers will have to learn how to use it. true of any new library you introduce to your team, really, and the efficiency gains will outstrip the learning cost pretty quickly.
- your code will look a bit different. this might take some people a bit of getting used to, but part of the reason it will look different is because there’ll be less code to write, so the dev team can get more done.
- if you’re growing your team, there may not be heaps of developers in the market that have experience with this library, but if you make a habit of hiring really smart programmers who can do pretty much anything you throw at them (that’s the approach we take at tyro), then this shouldn’t be a problem.
personally, i think this library is a godsend, and i think there’ll be a lot of teams picking it up over the next five years, just like the popularity of spring has swept the globe due to all the efficiencies it bestows on developers. the library, by the way, is called ‘ scala ‘, and it was invented by a guy called martin odersky . scala has been in development since 2001 and it’s …..
hang on, there seems to be a question up the back. yes? …..
okay, the question is, “isn’t scala a language?” well, of course it is. that’s what i’m talking about. the scala language….
i’ve been calling it a “library”? have i? oh, sorry, i didn’t realise. well, yes, you’re right, it is a language, although it also has a large useful library as well. but seriously, mate, what’s the difference?
seriously, what’s the difference?!
take a step back and think about this for a second: why is it such a big deal to start using another “language” on a project, whereas for a “library” or “framework” the decision is much easier?
i know there’s sensible arguments we might use against employing a new language:
- there’s a learning curve for developers who haven’t used the language before
- there’s a technology risk if the language contains serious bugs or ceases to be supported
- there may be difficulties integrating the language with existing code
- if you decide to stick with the language, there’ll provably be legacy code from previous languages for a long time
- it’s harder to hire developers who already know the language if it’s relatively new or novel
all of these are valid concerns and worth thinking about, but all of these concerns can just as easily be levelled at libraries and frameworks. (read through the list again, but replace “language” with “framework”.)
my real issue though is that this dichotomy is false. there isn’t a separation between libraries, frameworks and languages – it’s actually a sliding scale. what we’ve traditionally called libraries and frameworks will these days often introduce significant changes to the style of a codebase and even bring languages of their own tailgating in behind them.
two examples of this are hibernate and spring : great projects, widely-adopted, delivering huge productivity gains. hibernate brings with it hql , a new language, similar to sql, but different in the details. employing hibernate also means placing annotations throughout your domain model code. while the syntax of annotations is part of the java spec , the number and complexity of annotations included in a spec like jpa 2 mean that they become a language unto themselves – a non-java language that just happens to live inside our .java files. similarly, spring does a lot for us by enhancing plain old java classes, but the mechanisms for defining the enhancement are a language of their own, either in the form of <bean> configuration xml files or, again, using a suite of annotations .
as we’ve adopted these frameworks over the past decade, we’ve all changed our code to look vastly different to the java of a decade ago, but we’ve embraced these changes because of the productivity the technology has delivered. yet, when presented with the idea of adopting a new core language that would deliver similar productivity benefits, many teams baulk at the idea because… well, it would make the code look different!
what are you going to do?
if scala really was a “library” that was able to deliver all the benefits that i listed above, wouldn’t you and your team be desperate to start using it? and yet, because it has the label “language” attached to it, many people don’t even consider the possibility of using it, let alone whether the benefits outweigh the costs. it’s time to stop the xenoglossophobia (other language scardey-cat-ness), rise above the fear of learning something a bit different (because we’re doing that anyway!) and start evaluating all technologies on their merits.
and once you’ve thought about that for a while, start coding in scala.
update: if you’re a scala programmer reading this, i’m now running a short survey to get some numbers around whether scala really is more productive . would love it if you could take 2 minutes to contribute.
Published at DZone with permission of Allen Coin. See the original article here.
Opinions expressed by DZone contributors are their own.