-
Notifications
You must be signed in to change notification settings - Fork 178
Ballerina By Example guidelines
Below is a list of guidelines that must be followed when updating and adding new BBEs and API Docs.
-
Make the BBEs small, simple, and straightforward. Do not create examples with large lines of code. Otherwise, it will require scrolling through a large example to understand the use case.
-
Create realistic examples whenever possible. Make sure the example, which explains some features shows a practical scenario when it can be used. However, sometimes, it may not be possible to create a small-enough practical scenario. It will be required to use your judgment to decide that.
-
After creating/updating BBEs, generate the HTML files and deploy them on the website to verify if everything is displayed correctly.
Info: For instructions on creating BBEs, see Create Ballerina By Examples.
-
Each new BBE should have its own directory named after it. The directory name should be in all lowercase letters. Use hyphens (
-
) in the directory names (e.g.,hello-world
) and underscores (_
) in the file names (e.g.,hello_world.bal
). -
If a new example is added/deleted, update the
index.json
file as well.Info: Individual BBEs can be configured to disable the playground link generation and to override the default GitHub edit URL by setting the
disablePlayground
andgithubLink
properties accordignly in theindex.json
file.For example,
{ "title": "Ballerina Basics", "column": 0, "category": "Language concepts", "samples": [ { "name": "Functions", "url": "functions", "githubLink": "https://github.com/ballerina-platform/ballerina-lang/tree/ballerina-1.2.x/examples/functions/", "disablePlayground": true } ] }
-
Service examples demonstrating client-server scenarios have a
.bal
file only for the server and two different output files. That is, one to display the server output (.server.out
file) and the other (.client.out
file) to display the cURL command and the output. These two separate output files can be introduced with.server
and.client
suffixed to the file names.Info: For example, see the
hello_world_service.server.out
andhello_world_service.client.out
files below of theHello world service
BBE. -
Unless it is really required, it is not encouraged to have multiple BAL files in the same sample. In that case, each BAL file can have its own name and the
.out
file should match the name of the.bal
file. For example,
-
Use language features to make the examples look elegant (and small). For example, string templates, functional iteration, anonymous functions, etc.
-
Use meaningful variable names, function names, etc.
-
Remove unused imports in
.bal
files. -
Format the Ballerina source using an IDE Plugin.
-
Do not set (too many or any) optional properties (e.g., JDBC connection pooling props). Users will figure it out with code assistance or with additional documentation as those will be covered in Learn guides. Also, all the possible options will be covered in the API docs.
-
Simplify error handling. Use
check
whenever possible. These examples do not need to show all possible error-handling situations. These possibilities can be shown in the actual error-handling BBEs. -
As a practice, use
ballerina/io
methods in main examples andballerina/log
methods in the examples with services. Do not use bothio:println
andlog:printInfo
in the same sample. -
Add comments in the BAL files as much as possible with “//” as they are used as a mechanism to describe the code.
-
The comments should be valid sentences (i.e. start with an upper case letter and end with a full stop etc.), and try to keep the code-level comments short.
-
Write BBE content in general markdown syntax in the MD file.
-
All titles should be in sentence case (i.e., only the first letter of the title should be in uppercase).
-
All keywords and any other word, which needs to be highlighted should be used with backquotes (e.g.,
xml
). Do not use a single quote as it will not get highlighted on the Ballerina website. -
The files will be displayed on the website in the order in which you include them in the markdown file.
-
Empty lines will be ignored when content is rendered and new lines will be rendered with the spacing specified for them.
-
The BBE generation script reads and converts the content of the Markdown file line by line. However, in the case of a Markdown code block spanned across multiple lines, it reads it as a whole. Also, it ignores any empty lines in the Markdown file.
-
To add a new line in the rendered BBE content, adding a new line in the usual Markdown format will be sufficient. If a line exceeds the character limit of the BBEs body section, the remaining text of the line will be shifted to a new line with the spacing of text in a paragraph.
As a best practice, add the bal
command and the corresponding output by truncating it as a common pattern in the .out
files. For example,
$ bal grpc --input grpc_bidirectional_streaming.proto --output stubs
Successfully extracted the library files.
Successfully generated the Ballerina file.