Code Style
Recommendations for clean code bases
Internally, elementary uses the following code style guide to ensure that code is consistently formatted both internally and across projects. Consistent and easily-legible code makes it easier for newcomers to learn and contribute. We'd like to recommend that in the spirit of Open Source collaboration, all Vala apps written in the wider ecosystem also follow these guidelines.
Whitespace
White space comes before opening parentheses or braces:
An exception is admitted for Gettext-localized strings, where no space should go between the underscore and the opening parenthese:
Whitespace goes between numbers and operators in all math-related code.
Lines consisting of closing brackets (}
or )
) should be followed by an empty line, except when followed by another closing bracket or an else
statement.
Indentation
Vala
Vala code is indented using 4 spaces for consistency and readability.
In classes, functions, loops and general flow control, the first brace is on the end of the first line ("One True Brace Style"), followed by the indented code, and a line closing the function with a brace:
On conditionals and loops, always use braces even if there's only one line of code:
Cuddled else and else if:
If you are checking the same variable more than twice, use switch/case instead of multiple else/if:
Markup
Markup languages like HTML, XML, and YML should use two-space indentation since they are much more verbose and likely to hit line-length issues sooner.
Classes and Files
A file should only contain one public class.
All files have the same name as the class in them. For example, a file containing the class AbstractAppGrid
should be called "AbstractAppGrid.vala"
Classes should be named in a descriptive way, and include the name of any parent classes. For example, a class that subclasses Gtk.ListBoxRow
and displays the names of contacts should be called ContactRow
.
Comments
Comments are either on the same line as the code they reference or in a special line.
Comments are indented alongside the code, and obvious comments do more harm than good.
Sometimes detailed descriptions in the context of translatable strings are necessary for disambiguation or to help in the creation of accurate translations. For these situations use /// TRANSLATORS:
comments.
Variable, Class, and Function Names
Variable and function names are all lower case and separated by underscores:
Classes and enums are Upper Camel Case (aka Pascal Case):
Constants and enum members should be all uppercase and separated by underscores:
The values of constant strings (such as when used with GLib.Action) should be lowercase and separated with dashes:
Casting
Avoid using as
keyword when casting as it might give null
as result, which could be forgotten to check.
Prefer Properties Over Get/Set Methods
In places or operations where you would otherwise use get
or set
, you should make use of =
instead.
For example, instead of using
you should use
Initialize Objects with Properites
This is especially clearer when initializing an object with many properties. Avoid the following
and instead do this
Create Classes with Properties
This goes for creating methods inside of classes as well. Instead of
you should use
or, where you need some extra logic in the getters and setters:
Preferring properties in classes enables the use of GLib.Object.bind_property ()
between classes instead of needing to create signals and handle changing properties manually.
Vala Namespaces
Referring to GLib is not necessary. If you want to print something instead of:
You can use
String Formatting
Avoid using literals when formatting strings:
Instead, prefer printf style placeholders:
Warnings in Vala use printf syntax by default:
GTK events
Gtk widgets are intended to respond to click events that can be described as "press + release" instead of "press". Usually it is better to respond to toggle
and release
events instead of press
.
Columns Per Line
Ideally, lines should have no more than 80 characters per line, because this is the default terminal size. However, as an exception, more characters could be added, because most people have wide-enough monitors nowadays. The hard limit is 120 characters.
Splitting Arguments Into Lines
For methods that take multiple arguments, it is not uncommon to have very long line lengths. In this case, treat parenthesis like brackets and split lines at commas like so:
EditorConfig
If you are using elementary Code or your code editor supports EditorConfig, you can use this as a default .editorconfig
file in your projects:
Last updated