Chapter X GENERAL variables used

The term "variable" refers to both objects by chapter and built-in data types, such as integers and arrays. "Data Type" generally refers to the built-in data types, and word "data" is likely to represent objects, may also represent built-in data types.

Cognitive data

The first step in creating an effective data is the data that you want to create the kind of understanding. The accumulation of large amounts of data types is essential for programmers.

For more resources about data types

Cormen《Introduction to Algorithm》

Sedge wick《Algorithm in C++》

Easy to master variable definitions

Implicit declaration

  • Close implicit declaration
  • Declare all variables
  • Follow certain naming conventions
  • Check the variable name

The principle of variable initialization

Avoid initialization error suggestions:

  • When you declare a variable initialization;
  • Initializes variables at a position near the first use;
  • Ideally, the first variable is used to declare and define a position close to the variable;
  • In the final use or possible const;
  • Special attention counters and accumulators;
  • Initialize the data members of the class in the class's constructor function;
  • Check whether the need to re-initialize;
  • One-time initialization named variables, executable code to initialize the variable;
  • Compiler arranged to automatically initialize all variables;
  • Use warnings compiler;
  • Check the input parameters of legality;
  • Use memory access checking tool to check for errors pointer;
  • Initialization working memory when the program starts.

Scope

Or scope of visibility means is visible within the program and variables can be referenced range. Here are some rules to use scopes:

  • The variable reference localization;
  • As much as possible to shorten the variable "survival" time;
  • Reducing the scope general principles;
    • Before the cycle begins again initialize a variable loop in use, and these variables are not initialized at the beginning of the cycle in the subroutine belongs;
    • Until the variable is about to be re-assigned to their use;
    • The related statements are put together;
    • The relevant statements extract into separate program;
    • Most stringent visibility at the start, then according to the need to expand the scope of variables.
  • About the narrow scope of the explanatory variables;
    • What kind of narrow variable domain using methods programmer, depending on how he sees "convenience" and "manageability intellectual."

Persistent

Continuity is another description of a data declaration period, continuing with a variety of forms:

  • Lifecycle of specific code segments or subroutines.
  • As long as you allow, it will continue.
  • The life cycle of the program.
  • Last forever.

Binding Time

Binding time is the time variable and its value to bind together. It's time to occur when writing code or the compiler? Is the time when the program is loaded or running? Or is it another time?

  • Encoding (using Mystery value)
  • Compile time (using named constants)
  • Loaded
  • When an object is instantiated
  • immediate

The relationship between data types and control structures

Between data types and control structures in a well-defined manner relative binding. Jackson describes the relationship between the three types of data and corresponding control structure:

  • Data Translation as a sequential order of the program structure;
  • Selective translation of data into the program if and else statements;
  • Iterative data translated into the program for, repeat, while other loop structure.

Single-purpose specified variable

By using some clever way, you can give a variable is assigned multiple duties. But you'd better stay away from these clever but useless.

  • Each variable is used only for a single use;
  • Avoid the code have hidden meanings;
  • Be sure to use a variable to all declared.

Checklist: GENERAL usage data

Initialize variables

  • [] Every word programs to check the correctness of input parameters it?
  • [] Variable declaration location close to the first use of locations?
  • [] Possible to initialize variables while the variable declaration?
  • [] If you can not simultaneously declare and initialize variables, there is no use for the first time in close to a variable position to declare variables?
  • [] Counter and accumulator through the proper initialization it? If you need to use again, before re-initialize it?
  • [] Re-initialize properly "in the code you need to repeat the variable" yet?
  • [] The code at compile time by the compiler is not without a warning message?
  • [] If you use the language allows implicit declaration, compensation measures do you question thus may lead to yet?

Other matters use of data

  • [] If possible, all variables are defined as having a minimum scope yet?
  • [] Point of reference variables are concentrated at one point as much as possible it? Again adjacent to the same variable reference, or variable throughout the life cycle have done this yet?
  • [] Control structure in line with data type?
  • [] All variable declarations are used yet?
  • [] Variable bindings at the right time yet? - In other words, you consciously between flexibility and late binding brought about by the increased complexity of the balance made yet?
  • [] Each variable has one and only one use it?
  • [] The meaning of each variable is very clear and there is no implied meaning of it?

Points

  • Data initialization process is prone to error, so please use the initialization method described in this chapter to avoid errors due to the unexpected result of the initial value;
  • Minimize the scope of each variable. The same variable reference point together. The variable defined in the range of a subroutine or class. Avoid the use of global data;
  • The statement uses the same variable together as much as possible;
  • Early binding will reduce flexibility, but helps to reduce complexity. Late binding can increase flexibility, while increasing complexity;
  • It only uses for each variable.

Guess you like

Origin www.cnblogs.com/liam-ji/p/11516600.html