Use construct properties

2. February 2007

This is a comment for all people writing GObject-style libraries that may have to be wrapped for other programming languages (C++, etc). And as you only write cool libraries which will be that useful that they will one day be wrapped, this applies to all your libraries.

Construct properties
Add a property for every parameter of your _new method. These parameters can be restricted to G_PARAM_WRITABLE|G_PARAM_CONSTRUCT_ONLY if it is useless to set them after initialisation. The actual setting is done by overriding the set_property method of GObject. You can also ref/unref you parameters if necessary there.

The *_new method
The new method should just consist of one call to g_object_new(MY_TYPE, “foo”, foo, “bar”, bar, …, NULL). NEVER add anything else to this method!

The _init method
Here you can do as much initialisation work as you like. This method is called by the GObject system and thus will be accessed from all language bindings.

When wrapping a library it is lots of work and takes lots of time to convert every parameter to a construction property and to move all other initialisation code from _new to _init. Besides it is clean and everybody will start loving you…(and you would have saved me at least two hours today!)