C language, a programming tool worth lifetime (3) ---- Use suggestions

zhaozj2021-02-16  47

Over the years, I have accumulated some experience, the total professionals of Lin Lin, and some of the specific techniques, and some of them are related to software engineering, and some things are not only suitable for C language, but also apply to other languages. Mainly want to make some suggestions from different perspectives; for specific programs such as pointers, Printf, Scanf, etc., I will specifically describe in another topic.

I believe that in any case, a senior programmer or beginners have their own understanding and use skills to C, I will throw a few bricks first, I hope to lead everyone's jade.

1. Use of global variables

Use careful use of global variables, especially related to global pointers. Why use global variables? Because global variables can be very convenient to implement data exchange between each function. At the same time, it is also possible that any point in the program can access it. Therefore, although global variables are convenient, it is easy to out of control. In practical cases, the program is often running in accordance with the steps we "vision". If the point of the control program is running is not controlled, the program will often crash or cannot get the correct result. From the start of programming to the beginning, I have experienced experience in the out of control of global variables. The nearest example is:

You need to operate some files in the same format, the length recorded in the file needs to be passed to the BSearch comparison function, and the comparison function required by BSearch has fixed int (* CoID *, const void *). For simplicity, a global pointer is defined to point to the structural address of the process of processing the file while the application is applied. This code has no problem in running for more than half a year. But when I use it to open multiple files, the program sometimes core dump. Carefully check that each file has its own different places, although it applies for a structure for each file, but the global pointer points to the last open file, naturally there will be a problem.

When processing a database or file, I often see the head in many code, declare a bunch of global variables to handle the fields of the record, which should be completely banned.

Reasonable use of global variables can make the program simple. For example, I often use the grading log function: WriteLog (int loglevel, char * fmt, ...); internal use of global variables Struct Slevellog * g_plog. In the program, WRITELO can be easily called to write the log. Of course, the destination file of the log can only be unique.

For some constant strings, the global variable is a good choice. For example, an array of strings corresponding to the return value defined by the HTTP protocol:

Static char * http_reasonphase [] =

{

"Continue",

"SwitchProtocols",

"Ok",

"Created",

"Accepted",

...

}

Summarize some principles using global variables:

a. Global variable is to make the program simply, while maintaining a concise logic, the logic of the program cannot be confusing. In particular, the program needs to determine the process of the program according to some states.

b. The content of global variable processing is preferably unique.

c. When multiple global variables need to be used, it is best to package these global variables in a structure.

d. It is best not to include global variables in a custom library.

2. Library

a. Establish a base library

Because the features provided by the C language standard library are not enough, it is necessary to establish a base library. First, it is possible to handle any type of basic data structure: two-way linked list, Table, Hash, Tree, etc .; second is a grading log, buffer operation, analysis, common coding decoding, and C-not provided string analysis.

With these basic data structures, the entire system has the foundation.

b. Establish a core library

Only the basic library is far less than enough. In the process of writing applications, it should also constantly abstract the core library. For example, database operations, file operations, XML parses, ASN.1 encoding decoding, network operation, etc. After the core library has, the ability to analyze and design is preliminary.

c. Application Library

User management, data management, usual operations that are commonly used in applications, etc. should be packaged in time. Increase the reuse of the code, provide development efficiency and quality.

d. function name

Provide a naming method for reference:

For the same set of functions, a short prefix (2-5 characters) is used, and the _ and the specific operation are used. E.g:

Doubly linked list:

SDBList * DL_CREATE (freefunc myfree);

Inline void * DL_HEAD (SDBList *);

Inline void dl_insert_tail (sdblist *, void *);

...

Hash:

Shashhead * ha_create (u_long hsue, hashfunc hfunc, comparefunc cmp, freefunc myfree);

Inline void * ha_insert (shashhead * head, void * ptr);

...

Analysis of ASN.1

SASN1 * ASN_CREATE ();

SASN1TAG * ASN_BUILD (SASN1 * P, U_CHAR TAG, VOID * BUF, INT LEN);

...

Such methods can reduce naming repetitions, and increase the aggregation between the similar functions, it is not easy to make mistakes.

e. test

The library is based on the basis of the entire system, and the importance of the library test does not need to be said.

f. Questional

In a more complex project, people with the strongest program capabilities are responsible for the basic libraries and core libraries, and those who are more familiar with business and programming library, the general programmers are complex and specific business logic, which is generally guaranteed. The successful implementation of the project and the accumulation of technology

3. Object-oriented thinking and methods of key structures

About these two points have been introduced forebased, it will not be repeated here. As can be seen from the example, I firmly support and implement these two methods.

4. "Try" code

In the case of using new functions, new ideas or other grammar, skills can't hold, I often write a short code to familiarize with the use of functions, verify new ideas, master some methods and skills .

5. Keep a "simple" heart

There is a very famous word in the UNIX world "simple is beautiful". Simple is not simple in simple code, and simply represents simple syntax, clear structure, correct thinking, people. Otherwise, there is only pale, there is beauty there.

转载请注明原文地址:https://www.9cbs.com/read-15440.html

New Post(0)