Programming Tips
Get into the habit of testing your program often. Errors are a lot easier to spot and fix when you catch them early. See the Knowledgebase articles Debugging Your Code: Types of Errors and Debugging Your Code: Techniques.
(See also: Is There a Way to Test a Program While in Sesame Runtime)
Preview Your Programming PeriodicallyRun your program at sensible intervals to see if it is producing the expected results. The Write()/WriteLn() window is an excellent tool for checking the results of one or more programming statements. When programming anything other than a Mass Update, you are working in Sesame Designer on a copy of the database, not the live in-use database. So there is no reason not to test your programming in Preview mode:
- Close the Program Editor
- Click Save Layout Design
- Click Close Design Tab
- Open the Application menu
- Click Preview Application
Programming can take all your attention. You can be so focused on it that an hour or more can pass without your noticing it. Simply exiting the Program Editor and clicking Save Layout Design will save your work. When working in any computer program, follow the old maxim: Save early and often. Declare Each Variable and Assign it the Correct Type
Make sure all your declared variables are assigned a type. SBasic defaults to type: Int. Failure to assign a type to a variable might not produce an error on testing, but will likely give you an unexpected results when you run the program, particularly if the variable is really a string that Sesame is treating as a integer.
For more on declaring variables, see the "Variables" section in the Sesame Programming Guide. Use Correct Layout Element Names
One of the most common programming errors is to misidentify a layout element name when referring to it in programming. SBasic needs the name of the element, not it's label. You can drag the Program Editor window over to the side so you can see your form. If the form consists mainly of elements and their labels, the visible label may be the same as the invisible element name. However, if your program produces and identifier or name type error for a particular form element, and it's obvious that the reference to the element matches the element's label, then you need to double check the element name using Designer's Property Viewer. It could be that the label next to the element says "Shipping Date" but the element is named "shipping_date". If your programming references the element at all, you must use "shipping_date", not "Shipping Date". You can also use the Element Selector or the Element Browser to see your element names, found on the right-click menu of the Programming Editor. Labels are actually independent things in Sesame. You can change the text of a label in programming without affecting the text box or other form elements to which it refers.
(See Rules For Naming Layout Elements for more information on Element names.)
Use If-Then-ElseThe most frequently used construct in programming is the If-Then conditional statement. It says: If the specified condition is true, then take the specified action. If the condition is not true, then the action won't be taken. Example:
If Position = "" Then Position = "Unknown"
The If-Then-Else statement does more. It says: If the condition is true, then take action1. If it is not true, then take action2. The else statement tells Sesame what to do if the condition is not met.If Position = "" Then { Position = "Unknown" FullTitle = FullName } Else { FullTitle = FullName + ", " + Postion }
This program takes one action if Position is blank, but takes a different action otherwise. Sesame gives you several ways to write If-Then statements. The above program could be written in this way:
If Position = "" { Position = "Unknown" FullTitle = FullName } Else { FullTitle = FullName + ", " + Position }
The Then word in an If-Then is optional when the If condition is followed by statements on separate lines and (in this case) enclosed in French braces. The opening French brace means "Begin", while the closing French brace means "End".
Use User-Defined Procedures
A user defined subroutine or function (called procedures) is a program within your program that you write yourself. From any point you can branch off to a procedure to get some work done, and be automatically returned to the point in the program where you called it. Procedures can reduce the number of lines and commands in your program, and make it more efficient and easier to understand. See the "User Defined Procedures" section of the Sesame Programming Guide for more information.
Comment Your Programming See Good Programming Practices for more on this - as well as additional programming tips.