[REBOL] Re: idioms to reduce logic clutter ?
From: gscottjones:mchsi at: 12-Oct-2002 8:35
Hi, Jason,
From: "Jason Cunliffe"
> Greetings smart people
Whoops, I guess that rules me out after all. I misread the subject as
saying, "Idiots to reduce logic clutter," and I assumed I would be perfect
to answer.
:-)
...
> Time to factor out and simplify the mess. Need strategic
> advice.. or examples of rebol idioms for idioms to keep
> manage logic, keep clutter in check, avoid nested 'either
> [using 'find perhaps functions, objects..]
...
original email:
http://www.escribe.com/internet/rebol/m26449.html
First, I'll throw in a few comments interspersed in the sample that you
submitted to be sure that we are on the same wavelength.
...
> either error? try [
> ;condition
> upass: cgi-obj/userpass
> ][
> ;error
> if = upass "" [
We know that a form GET only returns name-value pairs where a value was
supplied. At this point, I assume that cgi-obj contains the name-value
pairs returned from a submitted form. What I don't know is whether the
cgi-obj starts with defaults for all fields and then fills in the returned
values, or is created directly by decoding and parsing the returns. In the
former, then a given field will always be present; whereas, in the later
case, only fields with return values will be present. Each offers
opportunities for compressing the logic. Given that you are assigning upass
inside a try block, I am assuming that cgi-obj only contains returned
name-value pairs (otherwise an assignment error would "never" occur). If
so, then if an error occurs, it is only do to cgi-object not containing the
userpass path. Therefore, there may be no reason to check for:
> if <> upass "" [
As an aside, I assume that the quit command was supposed to be the final
line in the first if block, otherwise, one would never pass through to the
second if.
> ; ok user has submitted a name and password
> ; check to see if they match
> upass: cgi-obj/userpass
Just a minor point: this assignment is redundant with the one done in the
try block.
Given what has been presented, I could see the following compressed version
as offering similar functionality. Of course, I have no direct way to test
it.
;====================================
; TEST FOR PASSWORD
;====================================
upass: copy ""
loginstatus: false
either error? try [
;condition
upass: cgi-obj/userpass
][
;error: userpass must not have existed
print rejoin [
{<b>Bad login!</b>}
{no user password provided }
{<a href="upload.html">}{try again}{</a>}
{<br>}
]
quit
][
; ok user has submitted a name and password
; check to see if they match
if not loginstatus: equal? upass logindict/:uname [
;trouble in paradise - help them out..
print rejoin [
{<br>Username and password do not match. <br>Please }
{<a href="upload.html">}{try again}{</a>}
{<form method="POST" action="echo-login.r"
enctype="multipart/form-data">
<i>Forgot your login or need to register ? <br>
Please enter your email address here: </i>
<input type="text" name="email_address" value=""/>
<input type="submit" value="send login by email"/>
</form>}
]
quit
]
]
;if program execution passes through to here, then a valid
; user name and password cobination have been offered
; and loginstatus should now be "true"
; OK to continue with processing
;=======================
If this error checking script continues through a number of fields, I would
likely encapsulate the error generating code into a function that allows the
error issue statement to be passed as a parameter and that offers a
refinement switch that allows the user to (re)register if the error
warrants.
error-page: func [
err-mess [string!]
/register
][
print rejoin [
{<br>} err-mess {<br>Please }
{<a href="upload.html">}{try again}{</a>}
if register [
{<form method="POST" action="echo-login.r"
enctype="multipart/form-data">
<i>Forgot your login or need to register ? <br>
Please enter your email address here: </i>
<input type="text" name="email_address" value=""/>
<input type="submit" value="send login by email"/>
</form>}
]
]
]
Finally, I agree with Carl R. that 'all is a great way to check that
conditions are correct to continue. For example:
a: make object! [b: 1]
either all [
not error? try [a/b]
not error? try [a/c]
] [print 'yep][print 'nope]
a: make object! [b: 1 c: 2]
either all [
not error? try [a/b]
not error? try [a/c]
] [print 'yep][print 'nope]
where the various fields of cgi-object could be checked in various ways
(including existence in this example).
HTH
--Scott Jones