Linux - Checking assumptions in the code using commands from the glib library
Repository
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
- commonly used to validate the arguments provided by the calling function
- 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!
Thank you for your contribution.
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!