General Issues in Using Variables. Find the three data types that don’t fit: Elongated Stream ...

17
CHAPTER 10 General Issues in Using Variables

Transcript of General Issues in Using Variables. Find the three data types that don’t fit: Elongated Stream ...

Page 1: General Issues in Using Variables.  Find the three data types that don’t fit:  Elongated Stream  Double Precision  Heap  Retroactive Synapse  Stack.

CHAPTER 10General Issues in Using Variables

Page 2: General Issues in Using Variables.  Find the three data types that don’t fit:  Elongated Stream  Double Precision  Heap  Retroactive Synapse  Stack.

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

Page 3: General Issues in Using Variables.  Find the three data types that don’t fit:  Elongated Stream  Double Precision  Heap  Retroactive Synapse  Stack.

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.

Page 4: General Issues in Using Variables.  Find the three data types that don’t fit:  Elongated Stream  Double Precision  Heap  Retroactive Synapse  Stack.

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.

Page 5: General Issues in Using Variables.  Find the three data types that don’t fit:  Elongated Stream  Double Precision  Heap  Retroactive Synapse  Stack.

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.

Page 6: General Issues in Using Variables.  Find the three data types that don’t fit:  Elongated Stream  Double Precision  Heap  Retroactive Synapse  Stack.

Scope

Scope is the “live time” of a variable. Keep the scope short to make code more

readable. Group variables with similar scopes.

Page 7: General Issues in Using Variables.  Find the three data types that don’t fit:  Elongated Stream  Double Precision  Heap  Retroactive Synapse  Stack.

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.

Page 8: General Issues in Using Variables.  Find the three data types that don’t fit:  Elongated Stream  Double Precision  Heap  Retroactive Synapse  Stack.

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.

Page 9: General Issues in Using Variables.  Find the three data types that don’t fit:  Elongated Stream  Double Precision  Heap  Retroactive Synapse  Stack.

Using Each Variable for Exactly One Purpose

Use one variable for one purpose only.

Page 10: General Issues in Using Variables.  Find the three data types that don’t fit:  Elongated Stream  Double Precision  Heap  Retroactive Synapse  Stack.

CHAPTER 11The Power of Variable Names

Page 11: General Issues in Using Variables.  Find the three data types that don’t fit:  Elongated Stream  Double Precision  Heap  Retroactive Synapse  Stack.

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.

Page 12: General Issues in Using Variables.  Find the three data types that don’t fit:  Elongated Stream  Double Precision  Heap  Retroactive Synapse  Stack.

Naming Specific Types of Data

Status Variables (flag) Temporary Variables (temp) Boolean Variables (true or false)

Page 13: General Issues in Using Variables.  Find the three data types that don’t fit:  Elongated Stream  Double Precision  Heap  Retroactive Synapse  Stack.

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.

Page 14: General Issues in Using Variables.  Find the three data types that don’t fit:  Elongated Stream  Double Precision  Heap  Retroactive Synapse  Stack.

Standard Prefixes

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

Page 15: General Issues in Using Variables.  Find the three data types that don’t fit:  Elongated Stream  Double Precision  Heap  Retroactive Synapse  Stack.

Creating Short Names That Are Readable

Use standard abbreviations Use combinations of numbers and letters Document all code abbreviations

Page 16: General Issues in Using Variables.  Find the three data types that don’t fit:  Elongated Stream  Double Precision  Heap  Retroactive Synapse  Stack.

Kinds of Names to Avoid

Duplicating words Commonly misspelled words Words that cross multiple languages Unrelated names

Page 17: General Issues in Using Variables.  Find the three data types that don’t fit:  Elongated Stream  Double Precision  Heap  Retroactive Synapse  Stack.

Questions?