[Marble-devel] Some new suggested guidelines regarding usage of Q_ASSERT

Dennis Nienhüser earthwings at gentoo.org
Wed Aug 4 16:55:52 CEST 2010


Hi,

I agree, although I do like Q_ASSERT. Just make sure to use it right.

A small extension to the two points:

c) Never use Q_ASSERT in conjunction with code that should be executed. 
For example,

Q_ASSERT( connect( this, SIGNAL(foo()), this, SLOT(bar()));

will work in debug mode, but will not connect the signal to the slot in 
release mode. Q_ASSERT in release mode does nothing.

Regards,
Dennis

On 04.08.2010 14:52, Torsten Rahn wrote:
> I had seen some practices recently that I want to comment on:
>
> Usage of Q_ASSERT
> ============
>
> I personally have a bit of a dislike for Q_ASSERT. Why? Because it easily leads
> to sloppy Trial&Error coding and bad code quality:
>
> a) Either you KNOW for sure that a certain dangerous situation can NOT happen.
> Then you usually wouldn't bother to do anything about it because you are 100%
> sure that nothing can go wrong. Still writing a Q_ASSERT would feel stupid but
> might be a good pick to play 500% safe during development.
>
> b) Or you BELIEVE that a certain situation can not happen (because well, it
> doesn't seem to be reasonable and without understanding the full code it still
> probably won't happen since it didn't happen during testing. In that case please
> DO NOT use Q_ASSERT!
>
> Instead try to understand the code. If you succeed and if you then KNOW 100%
> that the situation can not happen then proceed with a).
> You are still unsure whether the dangerous situation could happen at some point?
> Then please, please, please find a decent way out that will handle the case
> without crashing! Once that is implemented you can still add a Q_ASSERT to get
> notified about the "unintended" dangerous situation. But don't add it without a
> "safe" fallback strategy.
>
> I think that especially when it comes to parsing XML Q_ASSERTs should really get
> avoided: When parsing XML code it's not reasonable at all to assume that a
> certain kind of input can never happen. It's really bad if you put an assert
> into that place because you realized that this might crash code under realistic
> circumstances. Then usage of Q_ASSERT without finding a way to bail out safely
> without crashing is really bad!
>
> Torsten
>
>
>
>    



More information about the Marble-devel mailing list