Re: if statement not working

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

David:

if s[1..2] = "/*" then ...
could never work fine. It gives an error message telling conditions must be
atoms.

----- Original Message -----
From: "David Cuny" <dcuny at LANSET.COM>
To: "EUforum" <EUforum at topica.com>
Subject: Re: if statement not working



Graeme wrote:

> But seriously, having the interpreter return a valid result
> from accessing non-existant data is just way stupid.

Is there really a need to make the attack so personal?

Euphoria is sequence based. On first contact with Euphoria, giddy with power
once you grasp the power of the sequence, it appears that using slices to
get
substrings is the way to do things.

Euphoria doesn't offer any function for extracting substrings, so it
reinforces that notion. So the hapless newbie is led down the garden path,
writing stuff like:

   if s[1..2] = "/*"

which initially works fine, and then one day appears to blow up for no
reason.

Now, I know the 'proper' way to deal with this is to add a guard to the
statement:

   if length(s) >= 2 and s[1..2] = "/*" then

but this is bad, because you've decoupled the size test from the actual
string. So if the string is changed later (and it will be) but the guard
accidentally left alone:

   if length(s) >= 2 and s[1..3] = "-- " then

the result is unsafe code. And it'll pass tests just fine, until one day it
mysteriously crashes again.

Now, you could try writing:

   if match( s, "/* " ) = 1 then

which is inefficient for large strings, but it's safe. The problem is, it
only works or finding the *first* occurance of the string. So these two are
*not* the same tests:

   if match( s, "--" ) = 10
   if length(s) >= 11 and s[10..11] = "--" then

To reiterate:

0. The goal is to write safe string handling code.
1. Slices (by themselves) are unsafe when working with strings.
2. Guarded slices are unsafe because the guard isn't coupled to the string.
3. Euphoria doesn't provide any built-in routines for creating substrings.

I think that extracting substrings is common enough operation - and one that
traps newbies and me far too often - that it deserves a builtin function:

   s1 = substring( s2, n1, n2 )

which would behave the same as s2[n1,n2], except pad the string if n1 or n2
>
length(s2).

I'll grant that something like this would be more efficient for tests:

   n1 = contains_substring( s1, n1, n2, s2 )

but I think the addition of the first would go a long ways toward making
safer Euphoria code.

-- David Cuny

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

Search



Quick Links

User menu

Not signed in.

Misc Menu