Presentation is loading. Please wait.

Presentation is loading. Please wait.

General Issues in Using Variables

Similar presentations


Presentation on theme: "General Issues in Using Variables"— Presentation transcript:

1 General Issues in Using Variables
Chapter 10 General Issues in Using Variables

2 Data Literacy Find the three data types that don’t fit:
Elongated Stream Double Precision Heap Retroactive Synapse Stack Value Chain Array Local Variable Typedef

3 Making Variable Declarations Easy
If a programming language doesn’t require that you implicitly define the variable, define it anyway. Declare all variables before you use them. Make a list of all variables declared. Check to make sure that you have declared a variable before it’s put to use.

4 Guidelines for Initializing Variables
Improper data initialization is one of the most problematic sources of error in computer programming. Guidelines for avoiding initialization problems: Initialize and define each variable as its declared. Initialize each variable close to where it’s used. Use final or const when possible. Pay attention to counters. Check if re-initialization is necessary. Actually read the warnings from the compiler.

5 Guidelines for Initializing Variables
More guidelines for avoiding initialization problems: Check input parameters for validity. Check for bad pointers. Initialize working memory at the start of your program.

6 Scope Scope is the “live time” of a variable.
Keep the scope short to make code more readable. Group variables with similar scopes.

7 Persistence The lifespan of a piece of data.
Main problem: assuming a variable has a longer lifespan than it actually has. Ways to avoid persistence problems: Use debug code to check for improper initialization. Set variables to null when finished with them.

8 Relationship Between Data Types and Control Structures
Sequential data translates to sequential statements in a program. Selective data translates to if and case statements. Iterative data translates to for, repeat, and while looping structures in a program.

9 Using Each Variable for Exactly One Purpose
Use one variable for one purpose only.

10 The Power of Variable Names
Chapter 11 The Power of Variable Names

11 Considerations in Choosing Good Names
Choose good names for variables. Make them easy to read. Make them understandable. Don’t make them too long or too short.

12 Naming Specific Types of Data
Status Variables (flag) Temporary Variables (temp) Boolean Variables (true or false)

13 The Power of Naming Conventions
Why there are standards: Global decisions not local ones. Transfer knowledge across processes. Lean code more quickly on a project. Compensate for language weakness. Emphasize relationships between variables.

14 Standard Prefixes For a Word Document: Ch (character) Doc (document)
Pa (paragraph) Scr (screen region) Sel (selection) Wn (window)

15 Creating Short Names That Are Readable
Use standard abbreviations Use combinations of numbers and letters Document all code abbreviations

16 Kinds of Names to Avoid Duplicating words Commonly misspelled words
Words that cross multiple languages Unrelated names

17 Questions?


Download ppt "General Issues in Using Variables"

Similar presentations


Ads by Google