[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: RE: [Q] newbie questions about timestamp, character code, ..
- From: "Jerome Vuarand" <jerome.vuarand@...>
- Date: Fri, 9 Feb 2007 15:16:00 -0500
Philippe Lhoste wrote:
> > Note that false is a valid table value, and in that case the test
> > above will fail. To be sure a key is not used check
> explicitly against nil.
>
> Yes, that's a bad habit from C (or older Lua versions...).
> I always test explicitly against nil, even when there is no
> ambiguity, and use the if foo then only with variables I know
> being booleans.
> Idem in other languages, whatever their tolerance for this idiom.
> Just a personal policy, of course.
In fact some considers that to be a *good* habit in C++, where you can
overload the cast-to-bool operator. This makes a good universal paradigm
to use: if something resolves to true it's valid (and you can use it).
It works very well with Resource Acquisition Is Initialization
technique. This is true for basic pointers, and you can easily make it
true for objects (smart pointers, file handlers, etc).