GNOME Bugzilla – Bug 688447
Generated C code could prettier
Last modified: 2018-05-22 14:36:40 UTC
Some thought about the generated code that seem fixable. This would make people feel better about knowing that they can switch to the C code one day, and make it easier to debug problems in the generated code. 1. It would be nice if functions were laid out like in glib/gtk+ code: static ReturnType function_name() rather than static ReturnType function_name() 2. There should be newlines between things such as functions, defines and enums. Right now it is all bunched up together like this: void module_init (RygelPluginLoader* loader); #define RYGEL_EXAMPLE_SERVER_PLUGIN_NAME "ExampleServerPluginC" RygelExampleServerPlugin* rygel_example_server_plugin_new (void); RygelExampleServerPlugin* rygel_example_server_plugin_construct (GType object_type); GType rygel_example_server_plugin_get_type (void) G_GNUC_CONST; enum { RYGEL_EXAMPLE_SERVER_PLUGIN_DUMMY_PROPERTY }; #define RYGEL_EXAMPLE_SERVER_PLUGIN_TITLE "Example Server Plugin C" #define RYGEL_EXAMPLE_SERVER_PLUGIN_DESCRIPTION "An example Rygel server plugin implemented in vala." RygelExampleRootContainer* rygel_example_root_container_new (const gchar* title); RygelExampleRootContainer* rygel_example_root_container_construct (GType object_type, const gchar* title); GType rygel_example_root_container_get_type (void) G_GNUC_CONST; 3. Pointer type syntax could be like glib/GTK+. I don't like it (I like C++) but it's normal for C: For instance, now I have: RygelExampleServerPlugin * self = NULL; RygelExampleRootContainer* _tmp0_; (Notice the extra space on that first line). though this would be more normal: RygelExampleServerPlugin *self = NULL; RygelExampleRootContainer *_tmp0_; 4. There should be newlines between if() blocks and the following code. 5. There should be newlines between the initial variable declarations and the g_return_if*() checks, and then between those checks and the actual code. 6. This is harder to do right, but it would be nice if the lines were split, with an indent, at 80 (or so) columns.
Also, many of these tmp variables seem to be unnecessary, obscuring the code.
Some more points: It would be nice if it used G_DEFINE_TYPE() and similar when possible. I also find these *_construct() methods strange. They mean that objects cannot be instantiated with just a simple g_object_new() call and derived classes must call the _construct() function of the base class.
There are too many points here to make this a single fixable bug, but it serves as a useful heading for bugs relating to the C code writer. Adding dependent bugs allows for developers looking to improve Vala's ability to write C to a high standard to find relevant items to work on.
Created attachment 350382 [details] [review] ccode: Reformat function declaration and definition for better readability
Comment on attachment 350382 [details] [review] ccode: Reformat function declaration and definition for better readability Attachment 350382 [details] pushed as 3019b67 - ccode: Reformat function declaration and definition for better readability
For reference there is a GNOME C coding style guide: https://developer.gnome.org/programming-guidelines/stable/c-coding-style.html.en
-- GitLab Migration Automatic Message -- This bug has been migrated to GNOME's GitLab instance and has been closed from further activity. You can subscribe and participate further through the new bug through this link to our GitLab instance: https://gitlab.gnome.org/GNOME/vala/issues/336.