Linux - Checking assumptions in the code using commands from the glib library

in #utopian-io6 years ago

Repository

https://github.com/Vitusc

What Will I Learn?

You will learn how to check the assumptions in the code using the available commands in the glib library.
You will also learn how to verify the conditions correctly.

Requirements

  • Linux OS
  • Good knowledge of linux
  • Basics of programming in this system
  • Good understanding and reading of the code
  • Knowledge of the use of linux libraries.
  • Reading comprehension

Difficulty

  • Intermediate / Advanced

Tutorial Contents

The glib library provides a set of definitions that are often used to check
assumptions in the code. Thanks to them errors in programs are most often detected faster. It should be
place these definitions in sensitive areas of the code to check the necessary conditions. If a condition fails to be verified, the definitions print a warning in the console. It is possible that they will force a return to the calling function, and ultimately
ending the application.
Definitions are divided into two types

  1. commonly used to validate the arguments provided by the calling function
  2. verifying the conditions within the function.

Considering the correctness of arguments is the first step when we start the function process. They are so-called checking the necessary conditions. Two definitions:


g_return_val_if_fail(condition,returned_value) and g_return_if_fail(condition)

they print a warning, if (condition!=TRUE)
and they come back from the function. While the first of these
function returns

returned_value

it must be used for functions that are not declared in an empty context (void), the other is used in functions that do not pass values ​​(void functions).

in GNOME you can find an example of panel implementation - here is a clipping:

void
panel_clean_applet(AppletInfo *info)
{
g_return_if_fail(info != NULL);

if(info->widget) {
if(info -> type == APPLET_STATUS) {
status_applet_put_offscreen(info-data);
}
gtk_widget_destroy(info->widget);
}
}

You can try these definitions and let you know if you are leaving.
In case of any problems, you can speak in a comment.
Regards, @vitusc!

Sort:  

Thank you for your contribution.

  • There are sections in your tutorial that seem to be missing some parts, or are completely unintelligible. Try to review your content before posting it.
  • You barely touched the surface on providing useful tutorial for your users. You just listed basic info about couple of functions.
  • To deliver a useful tutorial, it is recommended to include steps to build and teach a user to accomplish something or reach a specific result.
  • Including results of running code in the shape of screenshots or code output would be very helpful.

Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, click here.


Need help? Chat with us on Discord.

[utopian-moderator]

Thank you for your review, @mcfarhat! Keep up the good work!