Scala offers significant benefits over Java such as more concise code that is 40% less than Java without being obscure. This improves developer productivity and reduces maintenance costs. Scala also facilitates functional programming techniques like higher order functions that allow for cleaner separation of concerns and improved reusability. Additionally, Scala's strong static types and functional features simplify parallel programming and creation of domain-specific languages.
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Scala a case4
1. Making a case for Scala :-
Conciseness1: 40% less code than java without being obscure; not simply due to
boilerplate reduction.
This is beneficial initially but especially more so for maintenance. It offers large
productivity and time to market gains, and makes for happier developers and their C
level executives.
More developers go to work more productive, deliver more business value, and feel
good at the end of the day.
a clean design is one that supports visual thinking so people can meet their
informational needs with a minimum of conscious effort. (www.visualmess.com)
A reasonable conjecture would be that the cost of developing and maintaining an
application grows with the size of the application. Adding code without value is
waste.
Scala has gone mainstream both in Financial Services and Stats communities,
particularly in Big Data Analysis due to functional composability and facilitating
strongly-typed simplified DSLs.
One reason for popularity is that verses an easy to understand DSL implemented in Scala
(without Scalas strong typing) one needs to have good IDE tool support for the DSL, and the
significant development effort for this tooling is avoided by using Scala DSLs at zero
cost. Time to market delays on DSL tool support are also avoided by using Scala.
http://news.efinancialcareers.com/uk-en/100268/smart-java-developers-need-to-skill-up-in-
scala/
Support for Higher order functions - functions which take functions as arguments.
As a metaphor, higher order functions can be seen as mechanical power tools, into
which different drills or blades can be inserted to perform different operations.
Higher order functions are a powerful abstraction tool to separate concerns at different
levels, for example data structure and processing of data elements embedded in the
data structure.
This is key for analyzing BigData & Geo where large data collections require specific,
highly optimized and, in consequence, complex data structures.
Functional Languages like Scala cleanly implement support for these concepts,
including functional composition/chaining.
Ease of Parallization of data processing is also a major
benefit. http://java.dzone.com/articles/higher-order-functions
Its not a case of Scala=good vs Java=bad. Scala is not a big leap from Java especially
now lambda support in Java 8 had been added (itself derived from Scala.)
An incremental adoption should be tried e.g. writing some of your test suite in Scala, or one
of your modules in Scala to get a feel for quality and benefit.
1
In Java, it feels very wasteful to keep repeating oneself due to weak abstractions and having
to write heaps of boilerplate code, all those getters, Comparators etc.
2. Frameworks matter. Scala leverages all the java stack and frameworks e.g.
Hibernate/JPA/JTA. Compatibility with Java Libraries and visa versa including Java
Generics.
Traits significantly improve code re-usability over Java. Traits are mix-ins; can be
used to implement strongly-typed compile-time dependency injection. They can also
be mixed-in at runtime. Traits improve design because a Single Responsibility
Principle can be implemented much more cleanly and easily than in Java. (A similar
concert to Dockers file system.)
Using functions makes code more concise, readable, elegant, and reusable, since
functions are at lower level than objects and their inputs/outputs are easily understand.
Just like data, functions are composable. Functions can also Improve design reuse via
the Single Responsibility Principle.
Scala has design idioms that reduce coding effort while increasing clarity. e.g.:
o case classes: that simplify domain object data classes by automatically
providing getters and setters, equals(), hashCode() ,toString(),copy().
o avoiding NPE (through 'Option: Some/None')
o avoiding complex nested If not null checks (through 'for comprehensions')
o non-blocking parallel task concurrency without obfuscating code with
exception handling or contingency workflows, timeouts etc. ('for
comprehensions')
o easy creation of functional data pipeline workflows, including error handling
and timeout contingencies ('for comprehensions')
o type aliasing - can be used to clarify primitive values used in maps/lists to
make maintenence easier
o DSL support to change legacy java APIs into simplified type-safe DSLs using
'implicits'
o higher order functions
o tuple datatypes allowing return values of more than one result
o more powerful 'case matching' statement than in Java that makes code simpler
and easier to read
o XML is first class citizen, part of the syntax.
o string parsing without + + + e.g. println(s"$activeTrj $scan")
o can easily enforce immutable state - concurrency friendly
o currying functional inputs - improves functional reuse opportunities, and
reduces bloat making code simpler to read.
Studies indicate Scala's cognitively-denser language constructs make code
comprehension much simpler than corresponding equivalent Java code.
With type-driven development you know your code will work as expected when you
compile it so you'll have no surprises at run-time.
These Benefits are documented as visible even amongst programmers with a limited grasp of
Scala. http://infoscience.epfl.ch/record/138586/files/dubochet2009coco.pdf
Moreover developers exposed to Scala describe using Java-like-style as “messy” and
“annoying”.
"Implementing the solution in Java would just be a bad approximation that makes me sad. It
takes 6 times the effort to complete, and is orders of magnitude more to maintain."
3. Concurrency implementations can be orders of magnitude simpler to implement in
Scala vs Java, since Scala was designed first and foremost to be a SCALAble
language for the JVM.
Scala can also leverage frameworks that solve concurrency problems that are becoming more
and more important e.g. Actors, STM, shared read-only variable state etc.
The language offers easier to understand DSL syntax than available in Java APIs when the
same 3rd partly libraries are used, even when underlying libraries are written in Java.
Scala offers a better programming model for concurrent programming. Java
implementations are very complex, very more so when dealing with exceptions
timeouts, and task contingencies are considered.
Encapsulation is improved: Unlike Java, public interface pollution can be avoided - as
scala methods support definition of inner methods.
No mixing of static and non-static code: Static objects are separated out into their own
distinct definitions.
In Scala, the issue of java type erasure does not occur with arrays. For lists there are
workarounds with using TypeTags, or ClassTags or Manifests (deprecated.)
Abstractions compose when types align. DSLs can model work seamlessly
across asynchronous boundaries. Futures and promises offer a way to design
compositional APIs that make this feasible.
Negatives:
You need to be clear on the Lang. features that should be used and enforce them via
compiler options and coding standards, because its easier to write baffling code than
in Java.
At the end of the day no framework is immune to bad practice e.g. in Spring: pretending the
Java new operator doesn't exist anymore, and creating 10,000 LO-XML that is not statically
typed.
You also need to avoid non-ascii method names and operators unless developing a math
library.
Compilation takes longer than Java, but this is mitigated with continuous background
compilation using SBT as best practice.
As with Java 8 Lambdas, stack traces become bigger but typically you only need look
at the 1st and last elements and ignore the rest. Definitely heaps better than Javascript
traces though.
Backwards compatibility has been an issue in the past, when one needed to recompile
source to new versions, but less so now.
Currently Less expertise of FP in marketplace, than OO, although this is changing
with Java 8 Lambdas. FP is easy to adopt across a small team if one person has
learned it already.
Scala support for Java 8 Lambda is available in v2.12.x, a wrapper function can
facilitate usage also in v2.11.8 as well as a compiler option.
Performance tuning comes with experience. Ideally one should know what to avoid
doing, just like in Java. Looking at the generated class files can clarify what is
happening at a low level. Familiar tools like JProfiler also support Scala.