Subject: Calc-TI - PROPOSED PROGRAMMING STANDARDS
From: ShdwSoftwr@aol.com
Date: Sun, 28 Aug 94 23:24:35 EDT
Message-ID: <9408282324.tn437138@aol.com>

Here are Shadow Software's proposed programming standards. I'm sure this
document is far from complete because I just thought of this stuff off the
top of my head. Shadow Software has been using these standards for quite
sometime now and they seem to help.

Please help me revise this document. If you have any spelling or grammar
corrections, please send e-mail directly to us. If you want to propose some
changes or additions, please post to calc-ti so that we can discuss them. I
would really like to get a standard document that we can pass around so that
we can have more low in memory-high in quality-easy to use programs. To
improve new TI-85 programmers skills even more it might be wise to create a
special TI-85 programming tips document. It could contain undocumented
features of the calculator and other tidbits.

TI Programming Standards v1.0
Original document by Shadow Software.

CONTENTS:

INTRODUCTION
SECTION 1: VARIABLES
SECTION 2: PARENTHESES
SECTION 3: "MACROS"
SECTION 4: SUB-PROGRAMS
SECTION 5: LABELS
SECTION 6: USER INTERFACE

INTRODUCTION
    Due to the lack of memory and local variables on the TI-85 calculator,
Shadow Software believes that measures should be taken to make programs take
up as little memory as possible. Here is the Shadow Software programming
standard. We hope that you will follow this standard to insure that programs
work together and take up less memory.

SECTION 1: VARIABLES

     There are a few different things that one can do to conserve memory and
ensure that programs work together when using variables.

1. Use one letter variable names. Variables are usually referenced many times
in a program. Each of these references takes memory. The shorter the
variables name is the less memory each reference is going to take up. If you
have a 1 letter variable, that is referenced 15 times in a program, the total
number of references are only going to take up 15 bytes while a 2 letter
variable referenced 15 times is going to take up 30 bytes in references.

2. Right before a program quits, have it store null strings into all the
variables it uses. If you are not familiar with what a null string is, it is
a string containing nothing. Just two quotation marks. ("") The reason to
store an empty string into your variables is to give other programs memory to
work with. The reason to use a null string is because a null string is the
data type that takes the least amount of memory. A one letter empty string
variable takes 7 bytes as opposed to a 15 byte one letter real variable. The
most efficient way to store a null string into all the variables that your
program uses is to store a null string into a variable whose name is one
letter long and then to store that variable into the rest of your variables.
Like this:

    ""->x:x->A:x->B:x->C

3. Reuse variables as much as you can. If you use variable q in one part of
your program but only need what's stored in it temporarily, then use variable
q again in another part of your program for a different purpose. The simple
fact is the less variables you create, the less memory you will use.

4. Choose unique names for any variables that you need to keep around for the
next time the program is run. These variables will be referred to as global
variables from now on, even though all variables are global on the TI-85. To
insure that other programs do not write over variables your program will need
the next time it is run, give those variables unique and long names. Do not
use common an obvious names such as "HISCORE." Instead, at least use prefixes
such as "GMEHISCR." Using prefixes for all your global variables will help to
keep them together so that it is easier for the user to delete them. It is
also good to make your prefixes relate to your program in some way such as
the first three letters of your program's name so that the user can tell what
global variables go with what program.

5. If referencing global variables in a program a lot, then store the global
variables in a variable whose name is one letter long and then store that
variable back to the global variable when the program quits. This technique
will save memory because global variable names should be long and referencing
them a lot will take up excess memory.

SECTION 2: PARENTHESES

1. As documented in the TI-85 manual, end parentheses (")") can be left off
of expressions and the expressions will still evaluate correctly. Each
parentheses takes up only one byte but parentheses are used many time in
programs and so taking out the end parentheses saves memory and is easy to
do. There are cases when end parentheses cannot be taken out like this:
"sub(S,(H*V)+1,2". Not every end parentheses was able to be taken out. You
may think that the end parentheses cannot be taken out of this expression:
"(Q/R)+3" but with a little shifting of the terms you can get rid of the
parentheses: "3+(Q/R". It is advantageous for you to look for these
situations to conserve as much memory as possible. Another case where you
might think that removing parentheses is not possible is this: "If L(2)>1" If
you were to take away the ending parentheses, and run the program, it would
crash on this line. All you have to do though is switch the expression
around: "If 1<L(2". Sure, this might look a little odd but you just saved
your users memory and now they can put more of your programs on their
calculator.

2. Use parentheses and plus signs instead of "and" and "or" when comparing
multiple items. This technique saves memory and speeds up your programs. An
expression like this: "If A==B and C>1 or D==2" becomes this: "If
(A==B)(C>1)+(D==2". In this statement, 3 bytes are saved and if you were to
run the last two statements in a time test, the latter of the would win.

SECTION 3: "MACROS"

   Equations are not just for graphing, they can save memory and typing too.

1. If an expression appears multiple times in a program then put that
expression in an equation and substitute the name of the equation for that
expression. Like this:

Sample Program:
1->j
1->q
"BOOGY BOOGY!"->S
sub(S,j,q->D
j+1->j
q+1->q
sub(S,j,q->F
j+1->j
q+1->q
sub(S,j,q->G
j+1->j
q+1->q
sub(S,j,q->H

The program using macros:

y1=sub(S,j,q
y2=j+1
y3=q+1
1->j
1->q
"BOOGY BOOGY!"->S
y1->D
y2->j
y3->q
y1->F
y2->j
y3->q
y1->G
y2->j
y3->q
y1->H

As you can see this will save memory. The y graphing variables are used so
that it is easier to tell what is a macro and what is a real variable. It is
not wise to write over equations that the user has defined though so it is
wise to use the SAVER and RESTORE programs that Texas Instruments supplied
with the TI-GRAPH LINK software. See the next section. If you can define you
macros before you start programming, you can save typing and time. It is not
easy to know exactly what statements will appear multiple times though so it
is easier to add the macros after the program is completed.

SECTION 4: SUB-PROGRAMS

1. If you have a big section of code that needs to be executed multiple times
it is wise to make a sub-program for it. Then have the main program call your
sub-program each time the code needs to be run. This saves memory because the
code will only appear once. This can also save you typing time.

2. Name your sub-programs starting with the alpha character (\LC-alpha\) to
keep them out of the user's way. Also use a prefix after the alpha character
to keep your sub-programs grouped together so that the user can delete them
easier.

3. To keep from annoying your users, use the SAVER and RESTORE programs that
Texas Instruments supplies with the TI-GRAPH LINK software. The SAVER
programs saves practically every preference that the user can set and the
current graphing data in all graphing modes. Call it at the beginning of your
program. The RESTORE program restores all the saved preferences. Call this
program right before your program is about to quit. Here are the text
versions of these programs. NOTE: The programs below are not exact copies of
the programs that are on the TI-GRAPH LINK disks. Shadow Software has removed
a Disp command from each program.

\START\
\COMMENT=Program file dated 10/23/93, 09:30
\NAME=SAVER
\FILE=SAVER.85p
Pol
StGDB myPol
Param
StGDB myParam
DifEq
StGDB myDifEq
Func
StGDB myFunc
zxMin\->\myzxMin
zxMax\->\myzxMax
zyMin\->\myzyMin
zyMax\->\myzyMax
zxScl\->\myzxScl
zyScl\->\myzyScl
ztMin\->\myztMin
ztMax\->\myztMax
ztPlot\->\myztPlot
ztStep\->\myztStep
\z-@Min\\->\my\z-@Min\
\z-@Max\\->\my\z-@Max\
\z-@Step\\->\my\z-@Step\
\LC-Delta\\->\my\LC-Delta\
tol\->\mytol
\STOP\

\START\
\COMMENT=Program file dated 10/23/93, 09:30
\NAME=RESTORE
\FILE=RESTORE.85p
Pol
RcGDB myPol
Param
RcGDB myParam
DifEq
RcGDB myDifEq
Func
RcGDB myFunc
myzxMin\->\zxMin
myzxMax\->\zxMax
myzyMin\->\zyMin
myzyMax\->\zyMax
myzxScl\->\zxScl
myzyScl\->\zyScl
myztMin\->\ztMin
myztMax\->\ztMax
myztPlot\->\ztPlot
myztStep\->\ztStep
my\z-@Min\\->\\z-@Min\
my\z-@Max\\->\\z-@Max\
my\z-@Step\\->\\z-@Step\
my\LC-Delta\\->\\LC-Delta\
mytol\->\tol
Normal
Float
Radian
RectC
Dec
RectV
dxDer1
\STOP\


4. If you have a loop in your program, that waits for the user to press a
key, then use the following sub-program along with the following code in your
loop to save energy.

Put this line on the outside of your loop.
1000->C

Put these lines on the inside of your loop.
C-1->C
If C<=0:Then
1000->C
\LC-alpha\PWRSAVR
End If

\START\
\COMMENT=Program file dated 08/28/94, 22:27
\NAME=\LC-alpha\PWRSAVR
\FILE=xPWRSAVR.85p
ClLCD
Disp "Power saving mode has","been activated."
Menu(1,"Cont",B
Lbl B
\STOP\

This program doesn't save power by itself but in conjunction with the TI-85's
auto shutoff mechanism, the power will be turned of roughly five minutes
after the menu command is executed.

SECTION 5: LABELS

1. When defining labels in your programs, use one letter label names such as:
Lbl A. Just like variables, going to labels can end up taking up a lot of
space. One letter labels will conserve memory. Remember you can use lowercase
letters for labels too.

SECTION 6: USER INTERFACE

1. Make your programs as easy to use as possible. Even if your program will
take up more memory.

2. Be consistent.

3. When using menus (created by the menu command) put commands common command
in the same spot in each menu. On the main menu put a quit command at the
fifth spot.  On any menus that branch off from the main menu put an exit menu
item in the fifth spot. Always capitalize quit, exit, and other constant menu
items such as help. Due to the five item per menu limitation, you might want
to have a more command to go to a logical extension of a menu. If you have a
more command then you may put it in the fifth menu item spot providing that
on the next menu there is an exit command in the fifth spot and that there is
a back command in the fourth menu item spot. The back command should also be
capitalized.
    To help explain menus, put the name of the section that the user is in on
at the first line of the TI-85 screen. Then on subsequent lines, put either a
bullet character or a negative sign and a one line explanation of what the
menu item corresponding to the line does. For example An explanation of menu
item one would go on line two and an explanation of menu item 2 would go on
line three. Do not explain capitalized menu items such as quit and exit. Do
not skip the explanation of menu items. For example do not explain menu item
one and menu item three but not two.

++++++++++++++++++++
Contacting Shadow Software:
++++++++++++++++++++


-Electronic Mail:

America Online: ShdwSoftwr
Internet: shdwsoftwr@aol.com


-Snail Mail:

Shadow Software
342 Eastmoor SE
Grand Rapids, MI 49546-2227
United States of America

****************

Subject: Calc-TI - MEMORY USAGE
From: dan.eble@commlink.org (Dan Eble)
Date: Mon, 29 Aug 94 19:21:00 +0000
Message-ID: <34.7871.5.0CC643B6@commlink.org>

Hey, Shadow Software! There are a few flaws in your Standards document.

First, when you store null strings into variables, don't use
lowercase-x. In fact, NEVER use lowercase-x or any other "system"
variable (i.e. x, y, t, r, theta, xMin, etc.). These variables are used
by the calculator for specific purposes, and the 85 has a unique way of
identifying this in the program, which adds an extra byte to your
program every time you use one of these variables. So, use uppercase-X
instead: ""->X:X->A:X->B or (if you REALLY wan't to use lowercase-x)
""->x:x->A:A->B.
Second, regarding the use of "and," "or," and parentheses, you say that
this:
        If A==B and C>1 or D==2

is three bytes longer than this:

        If (A==B)(C>1)+(D==2

It is, but only if your program isn't tokenized yet. Run the program
each way and look at the size _afterward_. Using "and" and "or" is
actually 6 bytes shorter.
         ^^^^^^^^^^^^^^^

Now, some extra info. Skip this part if you want other people to be able
to easily read your code. Here are extra ways to cut down program size.
I have measured the speed of these alternatives in simple For loops and
found they satisfy me. You may want to do your own tests.

Original Code   Bytes       Replace With   Bytes     Bytes Saved
                  
N+1->N            9         IS>(N,pi:        7            2
N-1->N            9         DS<(N,pi:        7            2
0                 3         sin pi           2            1
-1                4         cos pi           2            2
1                 3         sign pi          2            1
3                 3         int pi           2            1

Is seems to me that there was one other thing... oh well, I'll let you
know if I remember it. These substitutions saved me MANY bytes. One byte
doesn't seem much by itself, but think of how often the numbers 0 and 1
come up in programs. Incrementation and decrementation are very common,
too. If you discover any other methods of squeezing programs, please let
me know. Thanks, I hope you find this info useful.

Dan.Eble@CommLink.org (Dan Eble; Westlake, Ohio)
