5 Advantages of Dart Over JavaScript, Python, and Bash
After Google's announcement of Flutter 2 at the beginning of March 2021, interest in Dart climbed to new heights. Most people use Dart with Flutter.
Join the DZone community and get the full member experience.
Join For FreeAfter Google's announcement of Flutter 2 at the beginning of March 2021, interest in Dart climbed to new heights. Most people use Dart with Flutter. I use it for command-line HTTP API testing of RICOH THETA cameras. I arrived at Dart after five years of using Bash, Python, and JavaScript to automate testing. I kept switching languages due to problems with performance, code reuse, and scalability. Dart is not perfect. It has a much smaller community compared to JavaScript, is not as easy to use as Bash and Python and many libraries just don't work as expected with Dart. Few people have been using it for less time.
The story of why I chose Dart to help the RICOH THETA Developer community I am involved with may help you assess if you want to invest the time to learn the language to help your organization. The fantastic part about using Dart is that the language is simple to learn and is capable of handling some of the newest and most trendy programming paradigms. It's fun.
Starting With curl and Bash
We all start with curl. It is simple, fast, and we can send our friends working scripts built with curl in email or post them in a forum. Everyone either has curl installed or can install it easily. MacOS, Windows, Linux people all understand and use curl.
Here's an example of a simple curl command.
$ curl -d '{"name":"camera.takePicture"}' -H "Content-Type:application/json" -D - --digest -u "THETAYL001010xx:001010xx" -X POST 'http://192.168.2.101/osc/commands/execute'
It is also easy to put the curl commands in a Bash script. Bash runs on Mac and Linux easily. Thanks to WSL2 from Microsoft, you can also expect people using Windows to be familiar with Bash. Realistically with the prevalence of Linux on cloud-based servers, there is no problem sharing Bash scripts with people as most people understand Bash or can search for information on Bash.
Let's contrast this with Dart. Realistically, I expect no one else to use Dart. This is a significant downside. Obviously, I want to share my tests with other people to show that a system is working. I can't share Dart tests easily. I understood the limitations of Dart before moving. Let's look at why I moved to Dart.
The Problem With Bash and curl
The main reason I looked for an alternative to Bash is that there's some benefit if I can test the API from iOS and Android to see if there is something specific to iOS or Android that is causing a problem. For example, the RICOH THETA camera I test sends out MotionJPEG that displays a live video stream on a mobile phone. Although I can capture the individual frames from the command line and test the API that way, it is nicer to show people that the video is working on a mobile app.
5 Advantages to Using Dart
1. Same Dart Scripts Work in iOS and Android Native Apps Without Modification
There are also conditions where I want to test multiple API commands in sequence and it is nice to show the sequence on a mobile phone.
In the next example, I'm using the exact same files from the command line tool backend library in a basic Flutter app.
Sharing Dart Tests
Although it is nice to show people tests on a mobile phone, my primary focus is on command line tests. I actually don't need to show the API working on mobile phones that often. Although most people build mobile apps on iOS and Android, my tests of the API, are usually on a Windows 10 machine. For example, I'll take 1,000 pictures in rapid sequence or take a set of 7 pictures with different shutter speed values.
The tests are sharable as a sequence of steps, not actual code. In the example below, it's possible to use pseudocode to make a point.
xxxxxxxxxx
4- /osc/commands/execute camera.takePicture -> done
3- /osc/commands/execute camera.setOptions (shutter_speed: 0.04…) -> done
4- /osc/commands/execute camera.takePicture -> inProgress (“id”:“333”)
5- wait 4s
6- /osc/commands/status (“id”:“333”) -> inProgress
For people that want to replicate a precise test, I publish the following tools with source code:
- Dart library to access the API.
- Command-line tool with binaries for Mac, Linux, Windows, and Raspberry Pi.
- Bash scripts to automate running the command-line tool.
Although it first seems daunting to build a library, then a command-line tool, and then Bash scripts, it was easy to build up a set of standalone Dart scripts over time and then move them into a library. Like Bash, Python, and JavaScript, you can run Dart on the command line without compiling Dart for a specific platform.
In the example below, I'm running a Dart script with roughly the same ease as running python main.py
, bash main.sh
or node app.js
.
The word 'info' is the argument I am passing my application.
dart bin/main.dart info
I'm using the Dart args package, which is as used to build command-line interfaces as tools for other languages. Once I had roughly 50 individual Dart test scripts built up slowly over 6 months of on-and-off testing, I integrated the scripts into a library. I then built a command-line interface for the library.
2. Compile to Self-contained Snapshot
The Dart scripts can compile to self-contained snapshots that run on Linux, Mac, and Windows without requiring any other programs or libraries. On my GitHub releases page, I have binaries for those three platforms plus Raspberry Pi ARM to allow robotics and DIY developers easier access to the tools. In the example below, the Raspberry Pi archive is larger because I included more images in the documentation.
Although this type of distribution is possible with other languages, it is fast and easy with Dart. The binaries are also nice and small.
3. Static Typing, Finding Errors, and Optimization
Although Bash, Python, and JavaScript are awesome, I've always liked static typing. It's much easier to locate errors earlier in development and makes the complexity of the application easier to manage. Although this could be viewed as a matter of personal preference, some people, including me, really enjoy the benefits of static typing.
Static typing may also help with code optimization for the "average" programmer like me. As I'm not very good at optimizing JavaScript or Python code, it's easier for me to build more performant applications with Dart.
4. Null Safety and Reducing App Crashes
The network is a finicky beast. You never know what you actually receive. Humans can also forget to assign values to variables. Often, an app will crash when it tries to read in data such as a GPS coordinate from an object and the GPS coordinate variable is blank. It is null and null won't work. The app crashes and often doesn't give very good debugging information as to the source of the problem.
Languages like Kotlin and Swift have similar null safety features. Although it's possible to add these features to other languages, it's usually not the default or common way to manage variables.
As of Dart 2.12.0, null safety is optional. Dart is moving toward null safety as the default style.
This is another helper feature for the 'average' programmer like me. The feature, like static typing, protects me from my own errors. I like this because the reality is that I'm not a great programmer and can be more productive with tools that find my own errors.
5. Dart is Supported by Google
As Dart is the least popular out of the languages I covered in this article, it's important to me that a big company is committed to the success of Dart. I believe the support of Google helps with a frequent update cycle and fairly good management of bug fixes. Support for Dart and Flutter in editors like VS Code is excellent. Support in Android Studio is also excellent.
Disadvantages of Dart
While I like Dart, it's fair to point out the disadvantages of Dart and Flutter. Realistically, most people using Dart will also use Flutter.
The biggest problem is the lack of comparable library support. There are more libraries for JavaScript and Python. This could be a big problem, depending on your application.
Although Flutter Web is definitely usable, it's not great for text-heavy web sites like a blog. Flutter Web is great for interactive websites with many moving components and beautiful animations. Although you can embed an interactive Flutter component into a website written in HTML/CSS/JavaScript, you may run into a problem of dealing with different technologies. An alternative to Flutter Web is AngularDart. If you use AngularDart, you will run into a similar problem where most people are using something like React and you may run into more problems without an existing solution on Stack Overflow.
Conclusion
Dart is great for command-line applications. It's more than just the language for Flutter. I'm very happy with Dart as a language and I can also use the same command-line scripts in other applications such as iOS and Android applications or desktop applications. Most of the same code can also be used in web applications, depending on the network requirements of your application.
Dart is a simple language. It has enough features to build complex applications using modern paradigms. There is nothing revolutionary about Dart. It will likely seem familiar even if you've never used Dart before.
Dart helps protect programmers from their own errors. If you're already an expert at building bulletproof JavaScript applications with React, you should probably stick with JavaScript as many of the advantages of Dart are to make it easier to avoid introducing programmer error.
Bash is easier to use for small applications, but Dart is easier for more complex applications. It's more difficult to get Python on mobile and web applications compared to Dart.
While Flutter is getting all the media attention, I recommend using Dart alone and discovering the beauty of this simple language.
Opinions expressed by DZone contributors are their own.
Comments