Friday, 2 October 2015

[GUIDE] Programming languages alternatives for Android



Note to mods: this thread might be worth pinning! Using modern languages could highly improve code quality of Android apps.

OK, if you want native code, you can stick to C or C++, as you probably know interfacing native code with Java via JNI is neither pretty nor easy. But what about languages that compile to JVM (Dalvik/ART in Android case) that allow us to use tens of thousands of available java libraries in our projects? Java is quite nice but it's missing a lot of hot concepts available in modern languages like functional programming, duck typing and pattern matching. And as soon as you start using other JVM languages you'll discover Java is also extremely... verbose! I could use 5 Scala lines of code to do the same things I did in 50 lines of Java...

And do believe me, these languages mix wonderfuly with Android API. Once you start coding in them, you won't come back to Java. Note that all of them have Android Studio plugins, that make the coding experience as easy as clicking "Install Plugin"!

So, let's start with the king:

Scala

Website: link
Getting started: link
Why was it invented: Technically, Scala is a blend of object-oriented and functional programming concepts in a statically typed language. The fusion of object-oriented and functional programming shows up in many different aspects of Scala; it is probably more pervasive than in any other widely used language. The two programming styles have complementary strengths when it comes to scalability. Scala’s functional programming constructs make it easy to build interesting things quickly from simple parts. Its object-oriented constructs make it easy to structure larger systems and to adapt them to new demands. The combination of both styles in Scala makes it possible to express new kinds of programming patterns and component abstractions. It also leads to a legible and concise programming style.

A real beast of a language, I'm reading a fifth book on Scala right now, I have read tens of blog posts and I have a feeling like I haven't even scratched surface of it, although I've already incorporated Scala code into my XenoAmp project. If you're familiar with functional programming, you'll feel at home with Scala.

Let me rephrase the above paragraph: you PAINLESSLY incorporate below languages into your current project, without converting a single existing line! You just create new file that has extension different than .java! :D

Things like function literals, tuples, pattern matching and surprisingly flexible syntax of the language allow to create... a language within language, note very nice Scala/Android UI library named Scaloid and how it maintains its own minimal animation-only sub-language called Snails!

Scaloid activity layout example

Also read a blog post here about how can you write a clean code like this (note: NO SEMICOLONS!!! :D):


Code:


Scala
        findView[Button](R.id.button).onClick { view : View =>
            Toast.makeText(this, "You have clicked the button", Toast.LENGTH_LONG).show()
        }


instead of this:


Code:


Java
        Button v= (Button) findViewById(R.id.button);
        v.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(this, "You have clicked the button", Toast.LENGTH_LONG).show();
            }
        });


Kotlin

Website: link
Getting started: link
Why was it invented:
to create a Java-compatible language,
that compiles at least as fast as Java,
make it safer than Java, i.e. statically check for common pitfalls such as null pointer dereference,
make it more concise than Java by supporting variable type inference, higher-order functions (closures), extension functions, mixins and first-class delegation, etc;
and, keeping the useful level of expressiveness (see above), make it way simpler than the most mature competitor – Scala.




A language created by creators of IntelliJ Idea (base of AndroidStudio), with Android in mind. It's also functional, syntax based on Scala syntax, but much simplier and easier to learn. Also has a tool to convert your Java code to Kotlin!

Ceylon

Website: link
Getting started: couldn't find a good primer, but you could figure it yourself
Why was it invented:
to be easy to learn and understand for Java and C# developers,
to eliminate some of Java’s verbosity, while retaining its readability,
to improve upon Java's typesafety,
to provide a declarative syntax for expressing hierarchical information like user interface definition, externalized data, and system configuration, thereby eliminating Java's dependence upon XML,
to support and encourage a more “functional” style of programming with immutable objects and higher-order functions,
to provide great support for meta-programming, thus making it easier to write frameworks, and
to provide built-in modularity.


Supposedly more powerful than Kotlin, with better syntax for lambdas (function literals). Again a language with Scala-like syntax.

I might write a bit more about each language as soon as I start using them, so stay tuned. And share your experience with alternatives in this thread!



No comments:

Post a Comment