Should I compare pointers with is of == ?
Justin Johansson
no at spam.com
Wed Oct 14 12:01:18 PDT 2009
Max Samukha Wrote:
> On Wed, 14 Oct 2009 14:20:24 -0400, Justin Johansson <no at spam.com>
> wrote:
>
> >#ponce Wrote:
> >
> >> It's a bit unclear to me.
> >>
> >> I know I must compare references with is but pointers ?
> >
> >Thanks for asking this question ponce; I've been getting into the habit of using 'is' for both pointers
> >and classes, so in similar vein to ponce's question, I'd like to ask if the following (where foo is
> >eother a pointer of class ref) is being overly pendantic in the case of null if tests:
> >
> >if (foo !is null) {
> > // can do something with foo
> >}
> >
> >as opposed to the shorter form, but possibly incorrect or less safe
> >
> >if (foo) {
> > // can do somthing with foo
> >}
> >
> >I think I would prefer the shorter form if its 100% good.
>
> If you compare pointers or class references, it is 100% good to use
> the shorter form. It is only 87% good if you compare arrays because
> the shorter form means "if (arr.ptr)". So, if you are in the camp of
> those who do not make a distinction between empty and null arrays you
> should always use "if (arr.length)".
Thanks Max for clear and concise answer.
Now I can sleep again. :-) Justin.
More information about the Digitalmars-d-learn
mailing list