Home > Back-end >  How to write beautiful C code
How to write beautiful C code

Time:09-27

Object-oriented language is closer to people's ways of thinking, and reduces the complexity of the code to a great extent, improve the readability and maintainability of the code at the same time, the traditional C code can also be designed more readable, easy maintenance, low complexity of beautiful code, this paper will be sent to you by a practical example to illustrate this point,

Basic knowledge of
Structure
In addition to providing basic data types, C language is also provided to the user's own custom data types, structures, in C, you can use the structure to represent any entity, structure is the concept of object-oriented language classes in the prototype, such as:
Typedef struct {
float x;
float y;
} Point;
Defines a plane coordinates of a point, point, there are two domains, coordinates x and y coordinates,
Domain of structure are called members of the structure, the structure of the data type can be a simple data types, also can be other structures, and even can also be nested structure itself, for example, a standard chain table structure can be defined as follows:
Typedef struct node {
Void * data;//data pointer
Int dataLength;//data length
Struct node * next;//points to the next node
} the Node;
As you can see, the structure of the next node pointer types and node type,

function pointer

Pointer is the soul of the C language, C is more flexible than other languages, more powerful, so learning C language must have a good master pointer to a function pointer, the pointer to the function in the first address of memory mapping, through a function pointer, the function can be passed as a parameter to another function, and invoke at the appropriate time, so as to realize the function such as asynchronous communication,
For example, in the UNIX/Linux system signal registration function, its prototype is as follows:
Void (* signal (int signo, void func (*) (int))) (int)
When use, the need to externally define a signal handler (signal handler), then use the signal (sigNo, handler) the handler registered on the process, when the signal occurs, the process can be correction signal processing function,

will be a function pointer as members of the structure of
As mentioned earlier, a member of the structure can be a simple data structure, also can be other structures, and, of course, also can be a pointer, when the function pointer as members of the structure, and these functions are used to manipulate the data in this structure, can form an independent entity, the entity of existing data, also have to the operation of the data, so that nature can the concept of class (class),

object oriented language feature
In general, inheritance, encapsulation and polymorphism is considered a must support the three characteristics of object-oriented language, it is through these three characteristics can be also reflected in what way is superior to the object-oriented process oriented, due to the language developers publicity or other reasons, make on the surface of the object-oriented thought to be realized by language as a carrier, but in fact, is a kind of object-oriented software design thought, completely can be has nothing to do with the specific implementation,
However, there is no denying the fact that these so-called pure object-oriented language, in its the readability of the code and match with human nature thinking, much better than the process oriented language,

object oriented language level
We tend to describe an object, usually need to describe this some of the properties of an object, such as box (box) is an entity, it has six surface, color, weight, whether attributes, such as empty and can put things into, can take things out, in object-oriented languages, usually such abstract objects into a class (class)
The class Box {
Clolr color;
Int weight;
Boolean empty;

To put (something);
Something the get ();
}


Operation was carried out on the box, you can do the actions:
Box. The put (cake);
Box. The get ();//to get something from the box,


And procedure-oriented languages, usually pass entities to a throughout the global function, also in the Box, for example, to manipulate, the Box is often like this:

Put (Box, cake);//put a cake in box
Get (Box);//something out of the box to


Obviously, the first code in the form of more accord with common sense, so most object-oriented languages provide details of the language level of support, make the readability of the code, to increase comprehensibility, C language, as a flexible and simple language, we can through C provides simple mechanism, implementing such more beautiful code form,

C object-oriented

As I said, is a kind of object-oriented software design thought, is independent of language, in this section, I'll give you an list (list) examples to illustrate how to design in C language with object oriented style code,

Defines the interfaces

Interface is a more important concept in object-oriented languages, the entity of the interface interface to external promises only can accomplish what kind of function, but is not exposed, the advantage is that the implementer can not contact interface under the condition of the user's code, to achieve adjustment,

Let's look at the list of interface definition:

Listing 1. The list of interface definition
# # ifndef _ILIST_H
# define _ILIST_H

//define a linked list node structure
Typedef struct node {
Void * data;
Struct node * next;
} the Node;

//definition list structure
Typedef struct list {
Struct list * _this;
Node * head;
int size;
Void insert (*) (void * node);//function pointer
Drop void (*) (void * node);
Void (*) the clear ();
Int * getSize () ();
Void * (*) get (int index);
Void print (*) ();
} the List;

Void insert (void * node);
Void drop (void * node);
Void the clear ();
Int getSize ();
Void * get (int index);
Void the print ();

# endif _ILIST_H/* */


IList interface, can see clearly that entities (objects) for a list, you can insert in it, the drop, the clear, getSize, get (index), and the print operation, such as

the implementation of the interface
Listing 2. The structure with Node * Node=NULL;
The List * List=NULL;

Void insert (void * node);
Void drop (void * node);
Void the clear ();
Int getSize ();
Void the print ();
Void * get (int index);

The List * ListConstruction () {
List=(list *) malloc (sizeof (list));
The node=(*) malloc (sizeof (node));
The list - & gt; The head=node;
The list - & gt; Insert=insert;//insert function to registration on the entity list
The list - & gt; The drop=drop;
The list - & gt; The clear=clear;
The list - & gt; Size=0;
The list - & gt; GetSize=getSize;
The list - & gt; The get=get;
The list - & gt; Print=print;
The list - & gt; _this=list;//_this pointer to save the list itself up

Return a List (List *);
}


It is important to note here _this pointer, _this pointer can guarantee external mapping to the operation of the list to _this on the operation of making code is simplified,

Listing 3. Insert and delete

//insert a node to a list object
Void insert (void * node) {
Node * current=(*) malloc (sizeof (Node));

Current - & gt; Data=https://bbs.csdn.net/topics/node;
Current - & gt; Next=list - & gt; _this - & gt; The head - & gt; next;
The list - & gt; _this - & gt; The head - & gt; Next=current;
(the list - & gt; _this - & gt; The size) + +;
}

//delete a specified node node
Void drop (void * node) {
Node * t=list - & gt; _this - & gt; The head;
Node * d=NULL;
Int I=0;
For (I; i D=list - & gt; _this - & gt; The head - & gt; next;
If (d - & gt; Data=https://bbs.csdn.net/topics/=(Node (the Node *)) -> data) {
The list - & gt; _this - & gt; The head - & gt; Next=d - & gt; next;
Free (d);
(the list - & gt; _this - & gt; The size) -;
break;
} else {
The list - & gt; _this - & gt; The head=list - & gt; _this - & gt; The head - & gt; next;
}
}
The list - & gt; _this - & gt; The head=t;
}


Other implementation code to see the download section, there is limited to space is no longer listed,

test

The test code

Well, everything in front of the work is to ensure that we are exposed to the user of the API can be as far as possible concise, beautiful, now it's time to test:
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related