Turning around WHERE
posted Tue 17 Jun 2014 by Michael Galloy under IDLMotivated by this idiom from Python, I have been experimenting with using a small helper routine from my library, MG_ANY
. MY_ANY
flips around the return value and count
parameter of WHERE
and is used something like the following:
if (mg_any(condition, indices=ind)) then begin
; use ind for purpose
endif
This replaces the following standard code where count
is checked after the fact (and often neglected by novice programmers):
ind = where(condition, count)
if (count gt 0L) then begin
; use ind for purpose
end
To me, this small change of flipping the return value with a value passed back through a parameter/keyword makes this more readable and harder to write a bug into.