Coding Cultivation Series---A must learn to write portable code


Programmer Nick

1. The file name does not exceed 14 characters

  • The operating system derived from UNIX System V stipulates that the file name should be within 14 characters
  • The operating system of the BSD series of UNIX systems stipulates that the file name should be within 15 characters
  • Windows NT has almost no restrictions
  • Considering the portability of the program, the file name should be limited to 14 characters

2. Do not use special characters in file names

  • Don't be fancy, don't use special characters or control characters in file names
  • If you want me to say it, just use English characters and underscores.

Third, use conditional compilation to improve portability

  • When writing a program, you often need to modify the code when porting to a new computer environment or compile-time environment, which is too troublesome
  • Use a variety of conditional compilation instructions to write an independent header file and add it to all programs to get through the difficulties:
/*    条件编译     */
#ifdef XENIX
#define NEWAPI 600
#define NEWTYPE iNUM
#else
#define NEWAPI 300
#define NEWTYPE liNUM
#dedif
....
  • Select conditional compilation. The rewards far exceed the investment in learning time.

Four, understand the limitations of the compiler

  • Different compilers have different restrictions on nesting depth, module size, and allowed identifier length
  • When considering porting, it is necessary to set the boundary value of the program in advance.
    Examples of relevant coding guidelines for compiler restrictions:
  1. The size of a module should be limited to 60KB or less
  2. The length of a module should be less than XXXX rows
  3. It is forbidden to use arrays whose overall size exceeds 640KB
  4. Nesting depth must not exceed 5 levels
  5. Identifier length should be limited to 32 characters

Five, need to consider the possible changes in the size of the data type

  • To give the simplest example, some compilers think that the char type can hold negative numbers, but some cannot. Some compilers think that the size of int and long variables are both 32 bits.
  • The size of the data type and the ability to handle negative numbers all depend on the manufacturer.
  • Experienced programmers will use the sizeof() function to write the code to confirm the size of the data type at the beginning of the program
  • And novice programmers will only abandon the cumbersome sizeof() function
  • If it arbitrarily leads to data overflow and system paralysis, who will bear it? ?

6. Choose one of portability and high efficiency

  • You can not have it both ways
Focus Dependence on the system Representative means of realization
Focus on efficiency high Bit fields, pointers, binary files, shift operations, reference calls
Value portability low Integer processing, arrays, text files, multiply 2 / divide 2 operations, call by value

Seven, use arrays instead of pointers to improve portability

  • After using the pointer, you have to write a machine program
  • To improve portability, you should use arrays instead of pointers to process data

8. Choose a programming language with better portability

  • Portability and readability are usually proportional
  • As long as it does not cause serious problems, it is best to choose a language with better portability

Nine, do not insert code written in low-level languages

  • Insert assembly and machine language code into the C language inportable code, which may cause problems when transplanting.
  • If you have to use a low-level language to implement a specific function, you should write a library in the corresponding language, compile it in advance, and then call it in the form of a function.

10. Summary

This chapter introduces portability issues from several aspects such as file name, compiler, data type, programming language, etc., so that I have a good reference and understanding when writing large-scale projects in the future!

Guess you like

Origin blog.csdn.net/weixin_43722052/article/details/111027446