On GList anti-patterns and good documentation

In Gnome, open source, Programming on April 15, 2011 by oli4444

Several people posted GList anti-patterns, calling the code completely broken. Although I agree such code is broken, it must be said that the glib documentation for a very long time didn’t even specify if g_list_append() would return the first or the last item of the list. I know for sure that some of the early Bluefish code called append() in a loop. Currently there is a note in the glib documentation that calling g_list_append() has to traverse the list every time. That note is a major improvement, but for a beginning programmer it would be better to show an example that g_list_prepend() in a loop followed by a g_list_reverse() is actually faster than g_list_append() in a loop.

There are quite a few linked list implementations that look like the glib GQueue implementation (so they keep a pointer to the head and the tail) in which append() is equally fast to prepend(), so programmers coming from another language or library might not even think about the difference between the two. Perhaps we should  recommend beginning programmers to use a GQueue instead of a GList ?


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: