Where [message #63098] |
Tue, 28 October 2008 02:48  |
silviadncl
Messages: 4 Registered: October 2008
|
Junior Member |
|
|
Hallo!
Why IDL returns -1 when i do:
where(array eq 0.785)?
Array elements are not zero!
Thank you!
|
|
|
|
|
Re: WHERE [message #73370 is a reply to message #63098] |
Fri, 05 November 2010 08:20   |
penteado
Messages: 866 Registered: February 2018
|
Senior Member Administrator |
|
|
On Nov 5, 11:44 am, Paul <paulsta...@gmail.com> wrote:
> Now that -1 is a valid array index, does it make sense for WHERE to
> return it as a null result?
This was discussed at length last year, before the choices in 8.0 were
made. It absolutely has to keep returning -1 if called the same way,
or it would break a lot of old code - many tests are made comparing
the result with -1, instead of using the count argument. Living with
the consequences of the past, as David said.
New code can make use of the (new, obviously) null keyword, that makes
it return !null instead of -1. And the use of !null as index was made
to go nicely with that, so that doing assigning with a !null index
does nothing, with no errors, and retrieving with a !null index
returns !null, also with no errors.
|
|
|
|
|
Re: WHERE [message #73432 is a reply to message #73377] |
Mon, 08 November 2010 08:13  |
Paul Van Delst[1]
Messages: 1157 Registered: April 2002
|
Senior Member |
|
|
Hello,
Kenneth P. Bowman wrote:
> In article
> <8c77484b-ac8e-4579-ab69-347ff498411f@k14g2000pre.googlegroups.com>,
> Paul <paulstaten@gmail.com> wrote:
>
>> Now that -1 is a valid array index, does it make sense for WHERE to
>> return it as a null result?
>
> If you are not in the habit of always checking the 'count'
> returned by WHERE, now is the time to start. :-)
>
> Oh, and update your legacy code also.
Please permit me to reiterate/second/etc what Ken said above, as well as to apologise in advance for the preachy nature
of the following comments:
You should always program defensively - do not rely on a peculiar behaviour, e.g. in this case WHERE returning -1
meaning "something went wrong". Checking for the "count" result returned by WHERE not only avoids the issue you raised,
it also makes the code self-documenting. That is, the following
idx = WHERE( xArray EQ yScalar )
IF ( idx EQ -1 ) THEN ....
is really only understandable to the more experienced IDLer, whereas
idx = WHERE( xArray EQ yScalar, count=n_matches )
IF ( n_matches EQ 0 ) THEN ....
is much clearer. One should assume someone else will eventually be maintaining the code you write today. Be kind to them.
And, since I'm on a bit of proselytising roll right now, let me quote from James O. Coplien's forward in "Clean Code",
by Robert C. Martin:
"The French poet Paul Valery advises us that a poem is never done and bears continual rework, and to stop working on it
is abandonment. .... We abandon our code early, not because it is done, but because our value system focuses more on
outward appearance than on the substance of what we deliver. This inattentiveness costs us in the end: A bad penny
always shows up. Research, neither in industry nor in academia, humbles itself to the lowly station of keeping code
clean. .... Quality is the result of a million selfless acts of care -- not just of any great method that descends from
the heavens."
Yea, verily, here endeth the lesson. (Again, my apologies :o)
cheers,
paulv
|
|
|
Re: WHERE [message #73457 is a reply to message #63098] |
Fri, 05 November 2010 11:51  |
Paul[3]
Messages: 18 Registered: September 2010
|
Junior Member |
|
|
Ah, I hadn't checked for the NULL keyword...I'd wished for it in
passing, so thanks for the discussion! I'll pass that on to my friends
here in the department.
|
|
|