Use precise types

Using precise types will

  • Improve type checking at compile-time
  • Reduces the need for is checks and casts
  • Improve tooling experience: e.g., completion

Avoid overuse of var

Type inference allows the program to infer the actual data type of variables.

The use of var is a convenient way to use a generic type for a local variable and helps avoid repeated type declarations.

Code is usually more readable and maintainable when the type is declared explicitly. Therefore var should be used sparingly for variables used within a very limited scope, like in a foreach loop. Overusing it makes the code harder to understand.

Overuse of var

var name = "Alice";
var age = 30;
addUser(name, age);

Good Code

string name = "Alice";
int age = 30;
addUser(name, age);

Use application-defined types

Always use more application-defined type instead of json, any, anydata when you have more to do with that data.

Bad Code

http:Client httpClient = check new ("http://localhost:9090");
json albums = check httpClient->/albums;

Good Code

type Album readonly & record {|
    string title;
    string artist;
|};

http:Client httpClient = check new ("http://localhost:9090");
Album[] albums = check httpClient->/albums;

See Also: