Why Should You Program with Ballerina?
Cloud native open source language Ballerina provides the right level of abstraction for new APIs, new integrations and new logic for network interactions.
Apr 21st, 2022 8:05am by
Image by Angelica Vaihel from Pixabay
WSO2 sponsored this post.
Vishva Ahangama
Vishva is a senior lead marketing officer at WSO2, leading the planning and creation of content-driven experiences for its products. He holds a B.A. in English from Carleton University in Ottawa, Canada.
Ballerina Is Data and Network Oriented
With more services in the cloud, almost every enterprise application involves a network call. For developers, this adds the responsibility of working with networked resources in their code. Ballerina comes with a network-friendly type system with powerful features to handle data on the wire. Ballerina makes it easy to model data and send it back and forth over the network. The language has powerful tools to write, declare, process, query, structure, restructure and navigate data.
importballerina/http;
importballerina/io;
typeCountryrecord {
stringcountry;
intpopulation;
stringcontinent;
intcases;
intdeaths;
};
// Prints the top 10 countries having the highest case-fatality ratio.
publicfunctionmain() returnserror? {
http:ClientdiseaseEp = checknew ("https://disease.sh/v3");
Country[] countries = checkdiseaseEp->get("/covid-19/countries");
jsonsummary =
fromvar {country, continent, population, cases, deaths} incountries
wherepopulation >= 100000 && deaths >= 100
letdecimalcaseFatalityRatio = <decimal>deaths / <decimal>cases *
100
orderbycaseFatalityRatiodescending
limit10
select {country, continent, population, caseFatalityRatio};
io:println(summary);
}
With the Power of a Flexible Type System
A programming language’s type system is the foundation for representing data and implementing logic. While the developer must work with networked resources in their code, the programming language itself must aid in this operation. That’s why Ballerina’s network-friendly type system is specialized for this domain. For example, in typical programming languages, there are standards and protocols to define how to work with data that comes over the wire and bind it to the language (data binding). When data is received, a developer must bind it to a data structure in the language in order to manipulate it. Ballerina’s type system is capable of describing not just data in memory, but also data on the wire. In particular, a lot of work has been done to align closely with JSON, XML and other formats. When data comes over the wire, a developer can bring it into the language without any artificial, complicated data binding concern, effectively eliminating the data-binding concept from well-known data formats and network data structures. Ballerina’s type system is primarily structural with added support for nominal typing. This means that type compatibility is identified by considering the structure of the value rather than just relying on the type name. This is different from languages like Java, C++ and C# that have nominal type systems in which it is bound by the name of the actual type.Sequence Diagrams Model Network Interactions
In Ballerina, every program can be shown as a sequence diagram that illustrates distributed and concurrent interactions automatically. A function in a Ballerina program has equivalent representations both in textual syntax and as a sequence diagram. You can switch between the two views seamlessly. Ballerina’s unique graphical view isn’t something that was tacked on as a gimmick. It’s been designed deeply into the language to provide real insight into a function’s network interactions and its use of concurrency.
importballerina/http;
importballerinax/googleapis.sheets;
configurablestringgithubPAT = ?;
configurablestringrepository = "ballerina-platform/ballerina-lang";
configurablestringsheetsAccessToken = ?;
configurablestringspreadSheetId = ?;
configurablestringsheetName = "Sheet1";
typePRrecord {
stringurl;
stringtitle;
stringstate;
stringcreated_at;
stringupdated_at;
};
publicfunctionmain() returnserror? {
http:Clientgithub = checknew ("https://api.github.com/repos");
map<string> headers = {
"Accept": "application/vnd.github.v3+json",
"Authorization": "token " + githubPAT
};
PR[] prs = checkgithub->get(string`/${repository}/pulls`, headers);
sheets:Clientgsheets = checknew ({auth: {token: sheetsAccessToken}});
checkgsheets->appendRowToSheet(spreadSheetId, sheetName,
["Issue", "Title", "State", "Created At", "Updated At"]);
foreach var {url, title, state, created_at, updated_at} inprs {
checkgsheets->appendRowToSheet(spreadSheetId, sheetName,
[url, title, state, created_at, updated_at]);
}
}
While Ballerina has all the general-purpose functionality of a modern programming language, it stands out because it provides language features that make it easier to use, combine and create network services for the cloud. For an in-depth introductory explanation on the language features of Ballerina, we encourage you to watch the video below by Sanjiva Weerawarana, the language’s creator. You can also check out this blog.
Ballerina Swan Lake is available now. As an open source implementation released under the Apache License 2.0, it does not carry any licensing fees and can be freely downloaded at https://ballerina.io/downloads.
YOUTUBE.COM/THENEWSTACK
Tech moves fast, don't miss an episode. Subscribe to our YouTube
channel to stream all our podcasts, interviews, demos, and more.