The Dart Language and its peculiarities
As an Android developer coding in Java, some things just felt much bigger than necessary. Even though I had nothing directly against Java, in the background, I was just on the lookout for a language which would feel modern and eliminate the immense boilerplate Java has.
Then Kotlin came along. It was a robust, modern language and yet it seemed far off from Java and never piqued my interest even though I gave it multiple tries.
When I heard about the Beta release of Flutter I decided to give it a try. Flutter, if you do not know, is an cross-platform app development framework by Google.
Building apps in Flutter used Dart and I gave it a try. And Dart turned out to be a language that fit everything I wanted in Java and more.
This article is meant to give you a short glimpse into Dart itself and some of the features it offers. It is not meant to be an exhaustive list of all concepts in Dart.
Let’s give the general language a look.
Dart has frameworks for both mobile and desktop, Flutter and AngularDart respectively. The language itself feels like a modern combination of all the best features from major languages plus a few of its own.
Lets see what Hello World looks like:
print() here is a top-level function unlike Java.
Dart does not have explicit keywords like public, private with functions but instead uses underscores to differenciate between public and private functions.
This saves a lot of time and improves code simplicity by a long margin.
Dart supports string interpolation, which reduces the long concatenation in strings.
Now, Dart itself has a lot of features carefully crafted by observing a lot of common patterns in other OOP languages. For example, look at common constructors in a language like Java.
Dart reduces this boilerplate by introducing a constructor like this:
The constructor takes in arguments(when creating an object) and automatically assigns them to the three values defined here(_length, _width and _height). The two classes above are equivalent in meaning. Dart just seems much cleaner, neater and more concise.
One other feature I find brilliant is a named constructor. You can make multiple constructors in Dart with different names which allows clarity in what the constructor is actually initialising. We’ll take an example from Dart docs themselves:
Here both Point and Point.origin() construct objects of Point but it’s very clear as to what each constructor is doing unlike other languages where you’re supposed to judge by number of parameters.
In the context of Android(a lot of other things too, but NullPointerExceptions are so common in Android that developers are always looking for a way out), one really useful thing in Dart is the “?.” operator, which only does something if the object is not null, which saves a null-check everytime.
Similarly the “??=” operator works for assignment.
The strongest part of Dart by far is how it works with asynchronous tasks. The async and await keywords make it a breeze and if you come from an Android background like me, it’s a revelation.
Dart uses functions which return a Future instead of a direct value and the concept feels new and reduces the amount of callbacks like you have in Java.
Asynchronous tasks in Dart deserve an article on their own. So, I won’t go into a lot of depth right now.
Functions are also first-class objects in Dart, borrowing from languages like JS and allowing for a lot of newer concepts over Java.
In my opinion, Dart has the most meaningful syntactic sugar out there, simplifying common tasks and making them almost effortless, and at the same time not reducing readability by the same.
One thing I didn’t cover are data types and a few things like the cascade notation and fat-arrow notation which I didn't feel are important to people looking to just get a glimpse of what Dart has to offer.
No matter where you come from, Dart is definitely worth a try, especially by Java/C# developers. Dart is available as a plugin on IntelliJ IDEA or using DartPad online.