[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Does anyone have "assert" blindness ?
- From: Axel Kittenberger <axkibe@...>
- Date: Wed, 9 Mar 2011 15:28:22 +0100
> I agree with everything you say, but personally I wouldn't characterize it as
> a problem. When I'm first coding something, working hard to keep the design in
> my mind as my fingers put it into a source file, I have no time or mental
> energy to think of error handling details. So I assert anything that can go
> wrong. Later I can search for asserts and install proper error handling where
> necessary.
>
> I did this in C long before I'd ever heard of Lua -- it works well when
> writing new code when you don't want to divert attention to try/catch/finally,
> which has always seemed ponderous to me, and memorizing various exceptions.
>
Thats true, only 2 things to add.
1) Its perfectly ok to keep asserts in conditions that actually should
not happen. I always differ between errors that are raised, because
something is seriously wrong (internal fail), or errors that result of
wrong usage. In the second case a nice error handler/message is to be
written, the other case terminate as soon as possible, and maybe put a
error trace anywhere so the user can attach that to emails for
complaining at upstream.
2) Some asserts can enhance readabilty/understandabilty, like
asserting paramteres on function entry
function ceillog2(x)
assert(x > 0)
..
end
When a coder browses the function, it should come immediatly clear to
him, he shoud only use it with x as a number (or in lua a string in
the current locale convertable to a number), and larger than zero. And
a termination is much better than any funky behaviour. A proper
function header added is always better to have, but without, this
alone makes at least understanding foreign code a tad easier.