Re: I want to convert a list of integers to their negative self
Re: I want to convert a list of integers to their negative self
- Subject: Re: I want to convert a list of integers to their negative self
- From: email@hidden
- Date: Thu, 1 Mar 2001 11:44:26 -0800
While this topic has been very well covered there is one aspect that has
not been mentioned:
Variable names. This is a terrific example, especially for newbies as to
why it's so helpful to use descriptive variables.
When I first pasted an ran this sample script:
---------------------
set B to (the count of every item of A)
set This_List to {}
set c to 1
repeat with i from 1 to B
--
set c to c * i
set item c of A to D as integer
set E to D + D - D
set This_List to (This_List & D) as string
end repeat
display dialog This_List
--------------------
I got an error that the variable "A" wasn't defined.
I fixed that with this line: set A to {"-1", "-3", "-7", "", ""}
and ran it again and got an error that the variable "D" wasn't defined.
Granted this is a tiny snippet, but in both cases I had to read through the
script to figure out what was missing.
In a longer script that becomes a nightmare!
If I were seeing the same kinds of errors in writing the following snippet:
------------
set integerList to {"1", "3", "7", "", "", "x"}
set listLength to (the count of every item of integerList)
set negativeIntegerList to {}
repeat with thisInteger in integerList
try
set negativeInteger to thisInteger * -1
set the end of negativeIntegerList to negativeInteger
on error ErrText number ErrNumber
display dialog ErrText & ErrNumber
if ErrNumber is -1700 then
set the end of negativeIntegerList to ""
end if
end try
end repeat
return negativeIntegerList
---------------
My debugging task becomes much easier:
the variable integerList is not defined
the variable listLength is not defied
the variable negaiveInteger is not defined
This really aids in pinpointing where the errors are and what's happening
is your script.
>
In recent months, I have become especially fond of using gStrEtc...
>
type variable names, ie: descriptive of both scope and type. I find
>
that when I go back to re-examine complicated scripts that I had
>
written several months ago, that the variable naming conventions
>
are helpful.
>
>>and>>>Microsoft has done an excellent job of codifying one form of it
and using it reasonably consistently and successfully in-house.
Yeah, and we all know how stable and bug-free their software is. ; >
IMHO, a descriptive variable name "thisInteger" is much more valuable than
"gStrEtc".
ES