Re: g_assert vs. g_return_if_fail



On 01/20/2010 05:04 PM, Jiří Zárevúcky wrote:
> Behdad Esfahbod píše v St 20. 01. 2010 v 16:48 -0500:
>> On 01/20/2010 04:38 PM, Jiří Zárevúcky wrote:
>>> Behdad Esfahbod píše v St 20. 01. 2010 v 13:18 -0500:
>>>> On 01/20/2010 02:03 PM, Martin Kalbfuß wrote:
>>>>> Thanks,
>>>>>
>>>>> But why is it a disastrous idea?
>>>>>
>>>>> When It's a clear programming error, why not abort the program with
>>>>> g_assert?
>>>>
>>>> Because that can cause user data loss.
>>>>
>>>> behdad
>>>
>>> So there are two possibilities: g_assert when you want the application
>>> to abort, and g_return_if_fail when you don't. g_assert can be disabled
>>> compile-time for a release, right? In that case, it does nothing.
>>>
>>> So why not add a compile option to make g_return_if_fail fatal, and
>>> simply deprecate g_assert? What is the point of having it there?
>>
>> Can you please first state the problem you are trying to solve?
>>
>> behdad
> 
> No problem. I'm just wondering why there are two methods that do the
> same thing just slightly differently, while one of them shouldn't be
> used. So I guess my question is: What are the use cases for
> g_return_if_fail and what are the use cases for g_assert? (GLib newbie
> here)

g_return_if_fail() should be used at the beginning of functions, to check that
the input arguments are valid.  Specially useful in libraries, where the
calling code is not written by the author of library.

g_assert() is used to check for a condition that must always be true, aka
invariants.  Should be avoided in libraries except for things that are very,
very, very, very hard to fail.


behdad


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]