Recommendations for clean code bases
public string get_text () {}
if (a == 5) {
return 4;
}
for (i = 0; i < maximum; i++) {}
my_function_name ();
var my_instance = new Object ();// Space before parentheses since it's normal method:
button.label = set_label_string ();
// No space before parentheses since it's gettext-localized string:
button.tooltip_text = _("Download");c = (n * 2) + 4;if (condition) {
// ...
} else {
// ...
}
// other codepublic int my_function (int a, string b, long c, int d, int e) {
if (a == 5) {
b = 3;
c += 2;
return d;
}
return e;
}if (my_var > 2) {
print ("hello\n");
}if (a == 4) {
b = 1;
print ("Yay");
} else if (a == 3) {
b = 3;
print ("Not so good");
}switch (week_day) {
case "Monday":
message ("Let's work!");
break;
case "Tuesday":
case "Wednesday":
message ("What about watching a movie?");
break;
default:
message ("You don't have any recommendation.");
break;
}<component type="desktop">
<name>Calendar</name>
<description>
<p>A slim, lightweight calendar app that syncs and manages multiple calendars in one place, like Google Calendar, Outlook and CalDAV.</p>
</description>
<releases>
<release version="5.0" date="2019-02-28" urgency="medium">
<description>
<p>Add a search entry for calendars</p>
</description>
</release>
</releases>
<screenshots>
<screenshot type="default">
<image>https://raw.githubusercontent.com/elementary/calendar/master/data/screenshot.png</image>
</screenshot>
</screenshots>
</component>/* User chose number five */
if (c == 5) {
a = 3;
b = 4;
d = -1; // Values larger than 5 are undefined
}/// TRANSLATORS: The first %s is search term, the second is the name of default browser
title = _("Search for %s in %s").printf (query, get_default_browser_name ());var my_variable = 5;
public void my_function_name () {
do_stuff ();
}public class UserListBox : Gtk.ListBox {
private enum OperatingSystem {
ELEMENTARY_OS,
UBUNTU
}
}public const string UBUNTU = "ubuntu";
private enum OperatingSystem {
ELEMENTARY_OS,
UBUNTU
}public const string ACTION_GO_BACK = "action-go-back";/* OK */
((Gtk.Entry) widget).max_width_chars
/* NOT OK as this approach requires a check for null */
(widget as Gtk.Entry).max_width_charsset_can_focus (false);can_focus = false;var label = new Gtk.Label ("Test Label");
label.set_ellipsize (Pango.EllipsizeMode.END);
label.set_valign (Gtk.Align.END);
label.set_width_chars (33);
label.set_xalign (0);var label = new Gtk.Label ("Test Label") {
ellipsize = Pango.EllipsizeMode.END,
valign = Gtk.Align.END,
width_chars = 33,
xalign = 0
};private int _number;
public int get_number () {
return _number;
}
public void set_number (int value) {
_number = value;
}public int number { get; set; }private int _number;
public int number {
get {
// We can run extra code here before returning the property. For example,
// we can multiply it by 2
return _number * 2;
}
set {
// We can run extra code here before/after updating the value. For example,
// we could check the validity of the new value or trigger some other state
// changes
_number = value;
}
}GLib.print ("Hello World");print ("Hello World");var string = @"Error parsing config: $(config_path)";var string = "Error parsing config: %s".printf (config_path);critical ("Error parsing config: %s", config_path);var message_dialog = new Granite.MessageDialog.with_image_from_icon_name (
"Basic Information and a Suggestion",
"Further details, including information that explains any unobvious consequences of actions.",
"phone",
Gtk.ButtonsType.CANCEL
);# EditorConfig <https://EditorConfig.org>
root = true
# elementary defaults
[*]
charset = utf-8
end_of_line = lf
indent_size = tab
indent_style = space
insert_final_newline = true
max_line_length = 80
tab_width = 4
# Markup files
[{*.html,*.xml,*.xml.in,*.yml}]
tab_width = 2public class MyClass : Object {
public int foo { get; construct; }
public MyClass (int foo) {
Object (foo: foo);
}
construct {
if (foo > 0) {
// Do stuff
} else {
// Do other stuff
}
}
}public int foo { get; construct; }public MyClass (int foo) {
Object (foo: foo);
}construct {
if (foo) {
// Do stuff
} else {
// Do other stuff
}
}public class Row : Object {
public string name { get; construct; }
public Icon icon { get; construct; }
public Row (string name, Icon icon) {
Object (
name: name,
icon: icon
);
}
public Row.from_device (Device device) {
Object (
name: device.name,
icon: device.icon
);
}
construct {
var icon = new Gtk.Image.from_gicon (icon, Gtk.IconSize.DND);
var label = new Gtk.Label (name);
}
}public Row (string name, Icon icon) {
Object (
name: name,
icon: icon
);
}
public Row.from_device (Device device) {
Object (
name: device.name,
icon: device.icon
);
}construct {
var icon = new Gtk.Image.from_gicon (icon);
var label = new Gtk.Label (name);
}