Huawei software programming specification Learning (5)-variables and structures

Source: Internet
Author: User

Huawei software programming specification Learning (5)-variables and structures

5-1: Remove unnecessary public variables

Note: public variables increase.Module couplingTo reduce the coupling between modules.

5-2: carefully define and clarify the meaning, function, value range, and relationship between public variables

Note: When declaring a variable, you should comment out its meaning, function, and value range, and describe its relationship with other variables if necessary.

5-3: determine the relationship between a public variable and a function or process that operates the public variable, such as access, modification, and creation.

Note: clarifying the relationship between process operation variables will facilitate further program optimization, unit testing, system joint debugging, and code maintenance. Description of this relationship can be described in comments or documents.

Example: In the source file, it can be described in the following annotations.

Relation system_init input_rec print_rec stat_score

Student create modify access

Score create modify access, modify

Note: relation is the operational relationship. system_init, input_rec, print_rec, and stat_score are four different functions. Student and score are two global variables. Create indicates creation, modify indicates modification, and access indicates access.

Among them, the input_rec and stat_score functions can modify the variable score, so the variable will cause a large coupling between functions and may increase the difficulty of code testing and maintenance.

5-4: When transferring data to public variables, be very careful to prevent improper value or out-of-range phenomena.

Note: When assigning values to public variables, check validity if necessary to improve code reliability and stability.

5-5: prevent local variables from having the same name as public variables

NOTE: If good naming rules are used, this issue can be automatically eliminated.

5-6: Do not use uninitialized variables as the right value

Note: Referencing unassigned pointers in C/C ++ often causes system crashes.

Others

5-1: construct public variables that only one module or function can modify and create, while other related modules or functions can only access, prevents multiple different modules or functions from modifying or creating the same public variable.

Description: reduces the Coupling Degree of public variables.

5-2: use strictly-defined and portable data types. Do not use variables that are closely related to specific hardware or software environments.

Note: The use of standard data types is conducive to program transplantation.

Example: The following example (in the bc3.1 environment under DOS) may cause problems during transplantation.

Void main () {register int index; // register Variable _ AX = 0X4000; // _ ax is the register "pseudo variable" provided by bc3.1 "... // program code}

5-3: The structure function should be single and abstract for a transaction.

Note: When designing the structure, we should strive to make the structure represent the abstraction of a realistic transaction, rather than representing multiple types at the same time. The elements in the structure should represent different aspects of the same transaction, rather than placing the elements that describe different transactions that are irrelevant or have weak relationships in the same structure.

Example: The following structure is not clear and reasonable.

typedef struct STUDENT_STRU{   unsigned char name[8]; /* student's name */   unsigned char age;     /* student's age */   unsigned char sex;     /* student's sex, as follows */                          /* 0 - FEMALE; 1 - MALE */   unsigned char          teacher_name[8]; /* the student teacher's name */   unisgned char          teacher_sex;     /* his teacher sex */} STUDENT;

If it is changed to the following, it may be more reasonable.

typedef struct TEACHER_STRU{   unsigned char name[8]; /* teacher name */   unisgned char sex;     /* teacher sex, as follows */                          /* 0 - FEMALE; 1 - MALE */} TEACHER;typedef struct STUDENT_STRU{   unsigned char name[8];     /* student's name */   unsigned char age;         /* student'sage */   unsigned char sex;         /* student'ssex, as follows */                              /* 0 - FEMALE; 1 - MALE */   unsigned int  teacher_ind; /* his teacher index */} STUDENT;

5-4: Do not design a comprehensive and flexible data structure

Note: a comprehensive and flexible data structure can easily lead to misunderstandings and operational difficulties.

5-5: The relationship between different structures should not be too complex

Note: If the relationship between the two structures is complex and close, the structure should be merged.

Example: The structure of the following two structures is unreasonable.

typedef struct PERSON_ONE_STRU{   unsigned char name[8];   unsigned char addr[40];   unsigned char sex;   unsigned char city[15];} PERSON_ONE;typedef struct PERSON_TWO_STRU{   unsigned char name[8];   unsigned char age;   unsigned char tel;} PERSON_TWO;

Because both structures describe the same thing, it is better to synthesize a structure.

typedef struct PERSON_STRU{   unsigned char name[8];   unsigned char age;    unsignedchar sex;   unsigned char addr[40];   unsigned char city[15];   unsigned char tel;} PERSON;

5-6: the number of elements in the structure should be moderate. If the number of elements in the structure is too large, you can consider using some principle to form different sub-structures to reduce the number of elements in the original structure.

Note: Increase the comprehensibility, operability, and maintainability of the structure.

Example: If the _ person structure has too many elements, you can divide them as follows.

typedef struct PERSON_BASE_INFO_STRU{   unsigned char name[8];   unsigned char age;   unsigned char sex;} PERSON_BASE_INFO;typedef struct PERSON_ADDRESS_STRU{   unsigned char addr[40];   unsigned char city[15];   unsigned char tel;} PERSON_ADDRESS;typedef struct PERSON_STRU{   PERSON_BASE_INFO person_base;   PERSON_ADDRESS person_addr;} PERSON;

5-7: carefully design the layout and arrangement sequence of elements in the structure to make the structure easy to understand, save space, and reduce misuse.

Note: Reasonably arrange the element sequence in the structure to save space and increase comprehensibility.

Example: The bit fields in the following structure occupy a large space and have a slight readability.

typedef struct EXAMPLE_STRU{   unsigned int valid: 1;   PERSON person;   unsigned int set_flg: 1;} EXAMPLE;

If it is changed to the following format, it not only saves 1 byte space, but also improves readability.

typedef structEXAMPLE_STRU{   unsigned int valid: 1;   unsigned int set_flg: 1;   PERSON person ;} EXAMPLE;

5-8: The structure should be designed with forward compatibility and later version upgrade as much as possible, and leave room for some potential applications in the future (such as reserve some space)

Note: The forward compatibility of software is an important indicator of the success of software products. If you want to make the product better forward compatible, you should leave some room for later version upgrades at the beginning of the product design, and you must consider the features of the previous version when upgrading the product.

5-9: Pay attention to the principles and relevant details of the specific language and compiler for processing different data types

Note: for example, in C, static local variables are generated in the "Data zone" of the memory, rather than static local variables are generated in the "stack. These details are important for ensuring program quality.

 5-10: During programming, pay attention to forced conversion of data types

Note: During forced conversion of data types, the meaning of the data and the values after conversion may change. If these details are not considered weekly, they may leave hidden risks.

5-11: You must have a full understanding of the default data type conversion in the compilation system.

Example: assign values as follows. Most compilers do not generate alerts, but the meanings of values are slightly changed.

Char CHR; unsigned short intexam; CHR =-1; exam = CHR; // The Compiler does not generate an alarm. In this case, the exam value is 0 xFFFF.

5-12: minimize unnecessary data types, default conversion and forced conversion

5-13: properly design and use custom data types to avoid unnecessary type conversion between data

5-14: properly name the custom data type to make it descriptive to improve code readability. Note that the naming method is uniform in the same product.

Note: The use of custom types can make up for the shortcomings of the programming language with few types and insufficient information, and make the program clear and concise.

For example, you can declare a custom data type as follows.

The following statement makes the use of data types concise and clear.

typedef unsigned char  BYTE;typedef unsigned short WORD;typedef unsigned int   DWORD;

The following statement gives a richer meaning to the data type.

typedef float DISTANCE;typedef float SCORE;

5-15: when declaring the data structure for the distributed environment or the communication environment between different CPUs, you must consider the machine's byte sequence, bit domain used, and byte alignment.

Note: For example, when intelcpu and 68360cpu are used to process bit domains and integers, the Order in memory is the opposite.

For example, the following short integer and structure are provided.

unsigned short intexam;typedef struct EXAM_BIT_STRU{                      /* Intel 68360 */   unsigned int A1: 1; /* bit  0      7  */   unsigned int A2: 1; /* bit  1      6  */   unsigned int A3: 1; /* bit  2      5  */} EXAM_BIT;

The following describes how intelcpu generates short integers and bit fields.

Memory: 0 1 2... (in bytes)

Exam exam low byte exam high byte

Memory: 0 bit 1bit 2 bit... (each byte bit ")

Exam_bit A1 A2 A3

The following is how the 68360cpu generates short integers and bit fields.

Memory: 0 1 2... (in bytes)

Exam exam high byte exam low byte

Memory: 7 bit 6 bit 5 bit... (each byte bit ")

Exam_bit A1 A2 A3

Note: In alignment mode, the CPU running efficiency is much faster.

Example: for example, when a long number (medium long1) is in the same position as the word boundary of the memory, the CPU only needs to access the memory once, when a long-type number (long2 in the middle) crosses the word boundary in the memory, the CPU needs to access the memory multiple times, the number of accesses such as i960cx needs to read the memory three times (one byte, one short, and one byte, which is executed by the microcode of the CPU and transparent to the software ), the CPU running efficiency is much faster in all alignment modes.

1 8 16 24 32

----------------------------

| Long1 | long1 | long1 | long1 |

----------------------------

| Long2 |

-----------------------------

| Long2 | long2 | long2 |

-----------------------------

| ....

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.