TI-Basic Z80 Programming/Tips, Tricks and Optimizations

This chapter will focus on writing good practice code, including various tips, tricks, and optimizations you can apply.

Editing on the computer
Editing programs on the calculator can be extremely cumbersome; scrolling is slow, and program structure is hard to visualize. Also, calculators can lose power or freeze, which will destroy the current program and all other programs not archived. TI provides freely-downloadable "Graph Link" software, which is difficult to use, but is easier than manually programming on the calculator. It allows you to display messages in lowercase and write-protect programs. Unfortunately, the Windows version is not integrated into the TI Connect software, which is necessary to transfer your programs to the calculator.
 * Graph Link allows you to type strings with lowercase letters, which results in a better user interface.
 * You can "lock" a program, which prevents display of the code on the calculator, so you (or your friend) cannot accidentally mess up the program.
 * Fortunately, the Mac OS X version of TI Connect is much more powerful than its Windows counterpart, and allows for editing programs.

Program Optimization
As in optimization with other program languages, most TI-BASIC programs can be optimized in two ways: size optimization (makes the program take up less room on the calculator) and speed optimization (makes the program run faster). Often, improving the size of the program helps increase its speed and vice versa. Some basic tips that apply on BASIC programs follow:

Learn more commands
TI graphing calculators have many commands built in. To access them go to the catalog. On the 83+/84+ the catalog can be displayed by pressing  [CATALOG]. Some commands allow you to drastically shorten and speed up your TI-BASIC programs. For example, the abs( function automatically chops of the negative sign of a number.  will return  . This is faster and more compact than writing out if/then statements that chop off negative signs.

Use Lbl/Goto loops only when needed
Whenever you can, replace them with While, For( and other loops. This makes your code smaller, faster and a lot easier to read. Sometimes you absolutely need labels; do not be afraid to use them, but replace them whenever practical.

Use text with the Input function
You can place a single line of text after the input function using quotation marks. Follow this by a comma, then a variable. This saves you from having to use the Disp function and saves space (e.g.,  instead of  ).

Leave off ending quotes and parentheses
At the end of every line in a TI-BASIC program, the calculator does not care if you leave off your ending quote or your parenthesis. So, instead of typing  you should type   to save space. However, you should do this only when releasing a program (keep the original!) since it is more difficult to understand and edit. Not all commands or functions may allow this.

Simplify equations
This can be a more difficult task to one who is unfamiliar with equation manipulation learned from many algebra classes. A smaller, more simplified equation usually can run faster and be smaller in size. It is an important note that parentheses also count as a single byte.

can be. This is because there is a  function right on the calculator that takes up a single byte, so in theory, you are saving a single byte by taking away a parenthesis. The <tt>*</tt> command is not necessary, since it can replaced with nothing:  becomes   and   becomes.

It may not sound like a lot, but it adds up quickly!

Find the function
Whenever possible, use a built in function instead of using a loop. For example, instead of writing:

Write:

Use constants that are available to you
Instead of typing out  or , use <tt>π</tt> and  , respectively. The variables have much more precision and allow you to save space by using the token instead of the value.

Use as few variables as you can
Variables take up 18 bytes. If you use all 27 variables (not including available string and list spaces), this uses 486 bytes. This is sometimes more than the program itself. Simplify formulas as best you can. You can always use the <tt>Ans</tt> variable. Simply state the formula in your code without saving it into a variable. This puts the answer/result into the <tt>Ans</tt> variable itself. The <tt>Ans</tt> variable does not take up extra space because it always exists and always changes.

Use Subroutines
Often, in larger programs (on the scale of 5+ KB) there are repeating portions of the program (or very close bits). Make small programs that perform complex, repeated tasks. For example, suppose you have a program that performs a complex calculation multiple times. Instead of rewriting the complex calculation multiple times, write it once as a different program. Then, you can mention the program name in one line multiple times instead of writing out something long and complicated multiple times. The way to do this is to treat the separate program as a "black box." The "black box" must manipulate values from the Ans variable. For example, consider these paired sets of code, which square a number twice and add one to it ( does the repeated task of squaring the number):



Watch out for stack overflows
Any use of <tt>If Then Else</tt>, <tt>While</tt>, or other statements that are ended by <tt>End</tt> causes the program to use some more memory, because it has to remember that that statement has to be closed by an <tt>End</tt>. Avoid using <tt>Goto</tt> to escape these blocks, or end them at your destination, otherwise if you do this often, you can get a memory error and you slow down your program fast.

Make good use of the 'Ans' variable
<tt>Ans</tt> can store just about anything. If you have a big statement containing a number that you frequently reference, for example, it is possible to use <tt>Ans</tt> to save space. For example:

can be reduced to:

which could be further reduced to (it is actually more efficient to use  since it is only needed once):

Having a single value on one line of code is, surprisingly to those who write in other languages, a valid statement. It stores the value into <tt>Ans</tt>.

Don't run code you don't use
This is one optimization that can speed up your games tremendously. Instead of letting the program run through an entire <tt>For(</tt> / <tt>While</tt> / <tt>Repeat</tt> loop, you can end it prematurely with an <tt>If</tt>:

The above example will break the <tt>For(</tt> loop if the value is.

Calculate positions at display
Instead of displaying text or a glyph for a program after its X,Y coordinates have been calculated, put the formulas where you would normally put the X,Y variables. For example, instead of going through a bunch of <tt>If</tt> statements to find the string length of a number, you can use the <tt>log(</tt> operator inside your <tt>Output(</tt> or <tt>Text(</tt> commands to dynamically find its string length and display it at the correct position.

Only clear what you need to
To save time for drawing graphics for games, don't use clear draw or clear home commands. The calculator takes a long time to clear the entire screen. Instead, you can output blank spaces to clear. For example, you have a symbol representing a spaceship. Instead of redrawing the whole screen when the spaceship's position changes, just draw blank spaces where its old position was and draw the new spaceship on top.

Pause calculations using <tt>For(</tt>
An easy workaround to pause the current state of your program for a small amount of time would be to use a blank <tt>For(</tt> loop. For example, to pause the game when a message pops up:

Use this wisely, however, since it will pause all user input as well. (unless you put some code in the for loop)

Nearly always optimization requires creativity to be implemented properly and successfully.

Tips
If you must program on your calculator, there are several ways to make editing easier.
 * Use CtlgHelp. Download and install the CtlgHelp app on your calculator. It requires 32 KB free space. This app is a useful syntax reference. To access help, highlight a command in a menu or the Catalog and press the key.
 * Use a Plus Silver Edition calculator. If you can afford it, buy a "Plus Silver Edition" calculator, which is faster and has more memory.
 * Backup your programs. When you finish writing or using a program, immediately archive it (this saves the program from RAM to an internal flash disk). Programs which are archived will survive a power loss and most crashes, but those in RAM will disappear. This is only recommended if you only have to protect a single program. Use groups to backup multiple programs. You may find CalcUtil useful because it will allow you to edit and run archived programs, thus protecting everything from accidental deletion.
 * If your program saves permanent data, such as a notepad or to-do list, archive the file after reading or writing. The data file will thus be protected against power loss or crashes. However, this will significantly degrade performance, so it should not be used for large quantities of replaceable data that must be accessed quickly (such as a list of random numbers).
 * Alternatively, you can back up multiple programs in one group, which can be created in the memory menu. Groups must contain at least two objects (programs or variables).

Tricks

 * Copy-and-paste code. Let's say you have a program with code you don't want to rewrite in your main program. First navigate to the place in your main program where you want to insert code. Then, press [INS]  [RCL]  PRGM. Navigate to the name of the program containing the code you want to copy. Then, press.
 * Draw fast circles. Insert  as the last argument in your circle command. For example, this quickly draws a circle of radius 5 centered at (0,0):.