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
|
|
|