1. Re: in_range

"Cuny, David" wrote:

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

Why not just define index like this:

type index_value(integer x)
   return x > 0
end type

index_value index

This would catch any negative (or zero) index values that pop up. Then
later, once you've fixed all the bugs giving you those negative index
values, you can code "without type_check" to speed things up.

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

I don't see the overriding necessity for an in_range() function. The
index_value() type above combined with "if index > length(s)" statements
seems sufficient, and the intent (checking to be sure an index is in range)
seems quite clear to me.

Of course, that's just my opinion...

 -- Gabriel Boehme

new topic     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu