It is a good idea to create a set of programming standards when writing CSPro code. If you work in an organization, you will want to discuss these standards with the other programmers in your organization. Code is more readable when multiple people use the same coding guidelines.
This is an example of a set of programming standards adopted by an organization. Your standards may differ, but the point is to at least have a set of standards.
- Backup your code, and all components of the application, frequently. Create a system of version control (such as by using Git).
- Compile code frequently while working on it as this will help you identify syntax errors early.
- Use the help system (by pressing F1) and the reference window while writing logic.
- Use // for line comments and { } for multiline comments. Comment the code while writing it, not afterwards, as it will probably never get done in that case. Remember that it is likely that you will not be the only person who looks at your code.
- Maintain indentations for code within conditional statements (if), loops (do/for), and functions.
- Write CSPro statements in lowercase.
- Write variable names from the dictionary in UPPERCASE.
- Write globally declared variable names in mixed case, starting with a lowercase word (this is called camelCase). Make all global variables at least three characters long.
- Declare all constants in PROC GLOBAL, not in the application-level preproc.
- Use locally declared variables for loop counters and when only one procedure or function uses the value of the variable. Remember that the value of the local variable will be reset (to 0) each time the procedure is executed.
- Use functions when an operation is generalizable or if the same operation will be called repeatedly from different procedures.
- Use standard prefixes for variable names. For example:
- cnt: counts of an item (such as cntPersons and cntHouseholds)
- ptr: pointer to a record (for instance ptrEldest is the occurrence number of the eldest resident)
- For batch applications, maintain an error message numbering scheme so that both subject matter specialists and programmers can understand where error messages occur. For example:
- (1 digit) record type number
- (2 digits) question number of the primary variable
- (2 digits) error number for this variable
- Do not use deprecated features as they may not be supported in a future version of CSPro.
- Take advantage of free support from the U.S. Census Bureau at: cspro@lists.census.gov