Jassert vs bound checking


#1

Hi guys,

I was wondering, when do you use jasserts and when do you use bounds checking?

For example, if an out of range value is passed to a slider, should it be clipped within the slider range, or should there be a jassert to tell the programmer that the value is out of range?
And what should happen if a negative value is passed to a combobox?

I think in the first case the value should be clipped, and in the second case there should be a jassert.
But, in many cases it is not so clear.

What are your thoughts about this?

Thanks in advance,
Jelle


#2

Bounds checking should be done to keep code running with sensible data. An assert is purely a debug tool (which isn’t even included in release code), to alert you when something is incorrect from a programming standpoint. They are not mutually exclusive. For example, I have a routine that takes a pixel Y value, and returns a midi note. After the calculation I use an assert to let me know if an out of range value has been produced, but I then clip it within bounds. The assert lets me know if there is something wrong in the code that is calling it, and the clipping keeps the code usable and running.