This chapter will focus on writing good practice code, including various tips, tricks, and optimizations you can apply.
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 theTI Connect software, which is necessary to transfer your programs to the calculator.
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) andspeed 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:
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 pressing2ND[CATALOG]. Some commands allow you to drastically shorten and speed up your TI-BASIC programs. For example, theabs( function automatically chops of the negative sign of a number.abs(-2) will return2. This is faster and more compact than writing out if/then statements that chop off negative signs.
Whenever you can, replace them withWhile,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.
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 theDisp function and saves space (e.g.,:INPUT "What number?",A instead of:Disp "What number?" :Input A).
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:A+abs((2X+3)Y) you should type:A+abs(Y(2X+3 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.
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.
(x + y) / z can bez-1(x + y. This is because there is a-1 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* command is not necessary, since it can replaced with nothing:A*B becomesAB andA/(B*(C+4*F)) becomesA/(B(C+4F.
It may not sound like a lot, but it adds up quickly!
Whenever possible, use a built in function instead of using a loop. For example, instead of writing:
Write:
Instead of typing out3.14 or2.72, useπ ande, respectively. The variables have much more precision and allow you to save space by using the token instead of the value.
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 theAns variable. Simply state the formula in your code without saving it into a variable. This puts the answer/result into theAns variable itself. TheAns variable does not take up extra space because it always exists and always changes.
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 (prgmSQUARE does the repeated task of squaring the number):
prgmSQUARE:
Any use ofIf Then Else,While, or other statements that are ended byEnd causes the program to use some more memory, because it has to remember that that statement has to be closed by anEnd. Avoid usingGoto 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.
Ans can store just about anything. If you have a big statement containing a number that you frequently reference, for example100, it is possible to useAns to save space. For example:
can be reduced to:
which could be further reduced to (it is actually more efficient to use100 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 intoAns.
This is one optimization that can speed up your games tremendously. Instead of letting the program run through an entireFor( /While /Repeat loop, you can end it prematurely with anIf:
The above example will break theFor( loop if the value is4.
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 ofIf statements to find the string length of a number, you can use thelog( operator inside yourOutput( orText( commands to dynamically find its string length and display it at the correct position.
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.
An easy workaround to pause the current state of your program for a small amount of time would be to use a blankFor( 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.
If you must program on your calculator, there are several ways to make editing easier.
{i} as the last argument in your circle command. For example, this quickly draws a circle of radius 5 centered at (0,0)::Circle(0,0,5,{i}).
Previous:Errors
Next:Advanced Programming
Table of Contents:TI-Basic Z80 Programming