GTK + Feature Wrapper

The program is written as C, and the GUI is created by GTK +. No problem with the algorithms or what, I'm just not sure if this is one thing.

I plan to wrap some GTK features, so I can save a lot of source code lines. For instance:

GtkWidget *gtkwrap_label_markup_new(const gchar *txt) { GtkWidget *label; g_assert(txt); label = gtk_label_new(NULL); gtk_label_set_markup(GTK_LABEL(label), txt); return label; } 

I use text modified by pango many times, and this cover function has saved many lines of code. This may not be bad, but what if some other programmer one day reads my code, what will be the reaction? "... gtkwrap_label_markup_new? What ?!"

I just want to know if it is in the standard to wrap these functions. I do not want to look like an idiot, and I also do not want to learn some bad habits.;)

+4
source share
2 answers

There is nothing wrong with introducing a function to avoid code duplication. In fact, I would say that this is the exact opposite, the introduction of abstraction helps you develop your code faster and reduce the cost of fixing the problem or changing the design.

+6
source

Look at the GTK code if in doubt. For example, for gtk_label_set_markup you see the case when the str NULL variable is processed. Therefore, to check if this is suitable for your application, I think this is a bit overkill. If you want to be warned, you might prefer g_return_val_if_fail(str != NULL, NULL) to return NULL and print a warning if txt is NULL.

But if you really want to save a bunch of lines of code, use Glade to create your user interface, instead of building it in plain C.

+1
source

All Articles