HACKING.md 4.02 KB
Newer Older
Guido Gunther's avatar
Guido Gunther committed
1 2 3 4 5 6 7 8
Building
========
For build instructions see the README.md

Pull requests
=============
Before filing a pull request run the tests:

9 10 11
```sh
ninja -C _build test
```
Guido Gunther's avatar
Guido Gunther committed
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36

Use descriptive commit messages, see

   https://wiki.gnome.org/Git/CommitMessages

and check

   https://wiki.openstack.org/wiki/GitCommitMessages

for good examples.

Coding Style
============
We mostly use kernel style but

* Use spaces, never tabs
* Use 2 spaces for inentation

GTK+ style function argument indentation
----------------------------------------
Use GTK+ style function argument indentation. It's harder for renames but it's
what GNOME upstream projects do.

*Good*:

37 38 39 40 41
```c
static gboolean
button_clicked_cb (HdyDialerCycleButton *self,
                   GdkEventButton       *event)
```
Guido Gunther's avatar
Guido Gunther committed
42 43 44

*Bad*:

45 46 47 48
```c
static gboolean
button_clicked_cb (HdyDialerCycleButton *self, GdkEventButton *event)
```
Guido Gunther's avatar
Guido Gunther committed
49 50 51 52 53 54 55 56


Braces
------
Everything besides functions and structs have the opening curly brace on the same line.

*Good*:

57 58 59 60 61
```c
if (i < 0) {
    ...
}
```
Guido Gunther's avatar
Guido Gunther committed
62 63 64

*Bad*:

65 66 67 68 69 70
```c
if (i < 0)
{
    ...
}
```
Guido Gunther's avatar
Guido Gunther committed
71 72 73 74 75 76 77 78


Signals
-------
Prefix signal enum names with *SIGNAL_*.

*Good*:

79 80 81 82 83 84 85
```c
enum {
  SIGNAL_CYCLE_START = 0,
  SIGNAL_CYCLE_END,
  SIGNAL_LAST_SIGNAL,
};
```
Guido Gunther's avatar
Guido Gunther committed
86 87 88 89 90 91 92 93 94 95 96

Also note that the last element ends with a comma to reduce diff noise when
adding further signals.


Properties
----------
Prefix property enum names with *PROP_*.

*Good*:

97 98 99 100 101 102 103
```c
enum {
  PROP_0 = 0,
  PROP_CYCLE_TIMEOUT,
  PROP_LAST_PROP,
};
```
Guido Gunther's avatar
Guido Gunther committed
104 105 106 107 108 109 110 111 112 113

Also note that the last element ends with a comma to reduce diff noise when
adding further properties.

Comment style
-------------
In comments use full sentences and proper capitalization, punctation.

*Good*:

114 115 116
```c
/* Make sure we don't overflow. */
```
Guido Gunther's avatar
Guido Gunther committed
117 118 119

*Bad:*

120 121 122
```c
/* overflow check */
```
Guido Gunther's avatar
Guido Gunther committed
123 124 125 126 127 128 129 130


Callbacks
---------
Signal callbacks have a *_cb* suffix.

*Good*:

131 132 133
```c
g_signal_connect(self, "clicked", G_CALLBACK (button_clicked_cb), NULL);
```
Guido Gunther's avatar
Guido Gunther committed
134 135 136

*Bad*:

137 138 139
```c
g_signal_connect(self, "clicked", G_CALLBACK (handle_button_clicked), NULL);
```
Guido Gunther's avatar
Guido Gunther committed
140 141 142 143 144 145 146 147


Static functions
----------------
Static functions don't need the class prefix.  E.g. with a type foo_bar:

*Good*:

148 149 150 151 152
```c
static gboolean
button_clicked_cb (HdyDialerCycleButton *self,
                   GdkEventButton       *event)
```
Guido Gunther's avatar
Guido Gunther committed
153 154 155

*Bad*:

156 157 158 159 160
```c
static gboolean
foo_bar_button_clicked_cb (HdyDialerCycleButton *self,
                           GdkEventButton       *event)
```
Guido Gunther's avatar
Guido Gunther committed
161

162 163 164 165 166 167
Note however that virtual methods like
*<class_name>_{init,constructed,finalize,dispose}* do use the class prefix.
These functions are usually never called directly but only assigned once in
*<class_name>_constructed* so the longer name is kind of acceptable. This also
helps to distinguish virtual methods from regular private methods.

Guido Gunther's avatar
Guido Gunther committed
168 169 170 171 172 173 174
Self argument
-------------
The first argument is usually the object itself so call it *self*. E.g. for a
non public function:

*Good*:

175 176 177 178 179 180 181 182 183
```c
static gboolean
expire_cb (FooButton *self)
{
  g_return_val_if_fail (BAR_IS_FOO_BUTTON (self), FALSE);
  ...
  return FALSE;
}
```
Guido Gunther's avatar
Guido Gunther committed
184 185 186 187 188

And for a public function:

*Good*:

189 190 191 192 193
```c
gint
foo_button_get_state (FooButton *self)
{
  FooButtonPrivate *priv = bar_foo_get_instance_private(self);
Guido Gunther's avatar
Guido Gunther committed
194

195 196 197 198
  g_return_val_if_fail (BAR_IS_FOO_BUTTON (self), -1);
  return priv->state;
}
```
Guido Gunther's avatar
Guido Gunther committed
199 200 201 202 203 204 205 206 207 208 209 210

User interface files
--------------------
User interface files should end in *.ui*. If there are multiple ui
files put them in a ui/ subdirectory below the sources
(e.g. *src/ui/main-window.ui*).

### Properties
Use minus signs instead of underscores in property names:

*Good*:

211 212 213
```xml
<property name="margin-left">12</property>
```
Guido Gunther's avatar
Guido Gunther committed
214 215 216

*Bad":

217 218 219
```xml
<property name="margin_left">12</property>
```
220 221 222 223 224 225 226 227

Automatic cleanup
-----------------
It's recommended to use `g_auto()`, `g_autoptr()`, `g_autofree()` for
automatic resource cleanup when possible.

*Good*:

228 229 230
```c
g_autoptr(GdkPixbuf) sigterm = pixbuf = gtk_icon_info_load_icon (info, NULL);
```
231 232 233

*Bad*:

234 235 236 237 238
```c
GdkPixbuf *pixbuf = gtk_icon_info_load_icon (info, NULL);
...
g_object_unref (pixbuf);
```
239 240 241

Using the above is fine since libhandy doesn't target any older glib versions
or non GCC/Clang compilers at the moment.