in_range()

new topic     » topic index » view thread      » older message » newer message

Here's a suggested addition to MISC.E - a function that returns TRUE if an
index is in the range of a sequence:

   in_range( index, sequence )

Now, if you're like me, you're probably thinking that this is too trivial to
warrant a new function - just write:

   if index > length( s ) then
      ... bad index

which is exactly what I have all over my code. But the test lets negative
numbers slip by, which is a Bad Thing. The new function does the test
correctly, and it's clear what the intent is:

   if not in_range( index, s ) then
      ... bad index

I'm suggesting that it be added to MISC.E because it's such a common test,
and it would produce safer code as a result.

-- David Cuny

new topic     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu