1. Sequences - parameter checking responsibility

I would like some opinions on coding style.
Assume a global function which takes a sequence as
a parameter, i.e.,

global function xyz (seq s)
--- code
end function

Because a sequence is so generic, any sequence will pass type
checking without necessarily being in the
the correct form for that function.

Should the responsibility beyond type checking belong
to the function or to programmer who calls it?

On the one hand, I like to see functions that are
'bullet-proof' -- if I pass the right 'type' of parameter
to it, I want the function to handle all situations.

On the other hand, with sequences, there can be an awful
lot of checking that must be done, which can slow down the
function.

If the responsiblity is on the programmer who calls the
function, where should the function writer store the
information, so the programmer can code the sequence
correctly?

In my real-life example the sequence passed as the parameter
must contain at least one, but may contain multiple sequences.
Also the sequences within sequences must be composed of atoms,
not other sequences.
Thus these are all acceptable:

xyz {"2"}
xyz {"2", "5", "99"}
xyz {"2", "5", "99", "20220"}

A sequence containing atoms, is not acceptable
xzy { 3, 4, "6"}  -- not valid

And this is not valid:
xzy ( {"345, "567"}, "567"} -- not valid

As far a type checking goes, the outer most brackets define both
the valid and invalid parameters as 'sequences', so both
pass through type checking.

Also, if there were some terminology developed to describe these
situations better, documentation would be clearer and less
wordy.

Comments?  Suggestions?
--dae at paclink.com--

new topic     » topic index » view message » categorize

2. Re: Sequences - parameter checking responsibility

> Because a sequence is so generic, any sequence will pass type
> checking without necessarily being in the
> the correct form for that function.
        So make a type...

> Should the responsibility beyond type checking belong
> to the function or to programmer who calls it?
        That should be optional...
        If you use a type the programmer has a choice..
                ..in developping time have type_check on.
                ..when your program is finished turn it off.
        If type_check is on, it will generate an error if the value passed
is wrong. The advantage is you know you it's because of wrong
arguments and not a bug in the routine...
        For speed, turn type check off when you KNOW you program will not
give any errors.
        Setting type_check makes sure you get the right error message.
        A program isn't finished until there are no errors in it.. so....
here's your solution.
        BTW You should only check the value passed when it will produce an
error if it's wrong. That's the rule... all values that do NOT
generate an error should be allowed, you never know where you can use
your function for later on.

> On the other hand, with sequences, there can be an awful
> lot of checking that must be done, which can slow down the
> function.
        Yeah..but in design time...speed is not needed and when its finished
put a without type_check on top of your program.

> If the responsiblity is on the programmer who calls the
> function, where should the function writer store the
> information, so the programmer can code the sequence
> correctly?
        Well, it will always generate an error the programmer should deal
with, if its a type_check error you know that you gave bad values to
your function.

> In my real-life example the sequence passed as the parameter
> must contain at least one, but may contain multiple sequences.
> Also the sequences within sequences must be composed of atoms,
> not other sequences.

        You mean 2D-sequences ? (Like images and stuff)
        I guess so...

        Here is what it should look like:
        In your program type:

        "with type_check"   to turn it  on
        (all calls after this will have their values checked.)


        "without type_check"  to turn it off
        (all calls after this allow all objects)

----------- Code begins here: ---------------------------------------

        type 2D_Sequence (sequence s)
                for j = 1 to length(s)
                        if sequence (s[j]) then return 0 end if
                end if
                return 1                  -- Value ok
        end type

        global function xyz ( 2D_Sequence s )
        -- code
        end function
------------------------------ Code ends here --------------------

new topic     » goto parent     » topic index » view message » categorize

3. Re: Sequences - parameter checking responsibility

On Mon, 30 Jun 1997 12:58:49 -0700 d a edmunds <dae at PACLINK.COM> writes:
>Thus these are all acceptable:
>
>xyz {"2"}
>xyz {"2", "5", "99"}
>xyz {"2", "5", "99", "20220"}
>
>A sequence containing atoms, is not acceptable
>xzy { 3, 4, "6"}  -- not valid
>
>And this is not valid:
>xzy ( {"345, "567"}, "567"} -- not valid
>
>As far a type checking goes, the outer most brackets define both
>the valid and invalid parameters as 'sequences', so both
>pass through type checking.
>
>Also, if there were some terminology developed to describe these
>situations better, documentation would be clearer and less
>wordy.
>
>Comments?  Suggestions?
>--dae at paclink.com--
>

If the programmer handles it then this code will
take care of it.

function xyz(sequence s)
    integer bad

-- this loop checks for validity.
    bad = -1
    for a = 1 to length(s) do
        if sequence(s[a]) then
            if sequence(s[a][1]) then
                return bad
            end if
        else
            return bad
        end if
    end for

--(Your code goes here)
    return 0
end function

sequence s

--if good print 0
--if bad print -1

? xyz ({"2"})
? xyz ({"2", "5", "99"})
? xyz ({"2", "5", "99", "20220"})

--A sequence containing atoms, is not acceptable
? xzy ({ 3, 4, "6"})  -- not valid

--And this is not valid:
? xzy ({{"345, "567"}, "567"}) -- not valid

new topic     » goto parent     » topic index » view message » categorize

4. Re: Sequences - parameter checking responsibility

> > In my real-life example the sequence passed as the parameter
> > must contain at least one, but may contain multiple sequences.
                    ^^^^^^^^^- I almost missed that...
> > Also the sequences within sequences must be composed of atoms,
> > not other sequences.
    ^^^^^^^^^^^^^^^^^^^
    You missed this part, Ralf...

>         Here is what it should look like:
>         In your program type:

    If it looks like that, { {"345, "567"}, "567"} will pass...

> ----------- Code begins here: ---------------------------------------
>
>         type 2D_Sequence (sequence s)
          atom len len=length(s)
                  if len<1 then return(0) end if
>                 for j = 1 to length(s)
                      if atom(s[j]) then return(0) end if
                      for i= 1 to length(s[j]) do
                          if sequence(s[j][i]) then return(0) end if
                      end for
>                 end for
>                 return 1                  -- Value ok
>         end type
>
>         global function xyz ( 2D_Sequence s )
>         -- code
>         end function
> ------------------------------ Code ends here --------------------

I've been to lazy to write this for myself, but now I have it...

>> Edmunds
> Ralf
Anders
--------------------------------------------------------------
Anders Eurenius <c96aes at cs.umu.se> ICQ UIN:1453793
Computer Science/Engineering student at the university of Umea
--------------------------------------------------------------

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu