Object-oriented reconstruction of C Language

Source: Internet
Author: User

ReconstructionRefactoring is to adjust the program code to improve the quality and performance of the software without changing the existing functions of the software, so that the program design mode and architecture become more reasonable, improve software scalability and maintainability.

Today, more and more convenient and practical languages have never been poor,C LanguageAs a programming language, Evergreen still occupies the leading position in terms of the number of users. Of course, this is closely related to the use environment of C language, and many embedded and underlying code still cannot be separated from this classic language.

Recently, my work has been blank. How to Make the C code written by myself easy to read and easy to maintain has become a major issue I have recently considered.

So I found refactoring written by Martin Fowler. This book, based on Martin's experience and the various "stinks" in the code, puts forward the ideas, guiding ideology, and reconstruction steps for reconstruction, so that the Code continues to evolve, as a result, the Code becomes more and more beautiful, more and more suitable for changes, and more convenient for maintenance. Although this book is intended for the Java language, a large number of refactoring ideas are language-independent. We may encounter problems described by Master Martin in any language, he can use Martin's solution to correct the problem. Now, let's record some of the ideas and ideas I have read this book. It's also a reading note.

I. Function Improvement

The Procedural language directly faces the system responsibility for solving the problem, that is, the functional domain of the system. What functions should be implemented and what processes should be used to solve them? The implementation of such content is done by functions. Therefore, rebuilding functions becomes the most important part of C language reconstruction.

1. Rename the Function

The code is first written for humans, and then for machines. The function name should accurately express its purpose. When we maintain the code, it can save a lot of time to guess the purpose of this function by name. Think about some of your experiences, and you can understand it by reading the code! If there is a good name, look at the name and you will probably get the purpose of this function. With this general situation, it is so easy and pleasant to read the code.

Some may think that I am writing a C program, not a Java language. The C language requires a concise name, instead of Java, and the name of the Hate cannot be longer than the implemented code. In my opinion, being concise does not mean being vague. We all have a tacit rule. read stands for reading. If we get a file named rfile (), I guess it is a read file. If it is sfile (), who knows what it is?

2. Extract Functions

A function that is too long may include many complicated processes. When we need to constantly flip pages to understand the functions of a function, we will soon be impatient and lose confidence. Therefore, to make the function easy to understand, you must try to make the function Concise. In this case, you need to refine the functions and refine the functions continuously. Each function has its basic functions, which makes it very likely to reuse functions. complex functions are a combination of these small functions. In this way, complex functions are read like a series of annotations, which are easy to understand.

3. Refine judgment Conditions

Sometimes, when determining a condition, you may encounter a long expression that represents a condition Branch. This expression sometimes not only contains "and", "or", "not", but also query functions, greater than or less. To understand such an expression, the first thing to do is to check the corresponding relationship between brackets, then find out the operation Priority of various operators, and finally consider the meaning of this expression.

To get rid of this "non-" dilemma, We need to extract this judgment condition, assign a value to a temporary variable, or completely extract it into a function, the variable or function name clearly indicates the meaning of the judgment condition, such as isEmpty. In this way, we can see at a glance what the branch is like in the judgment statement. If you really want to consider the efficiency issue, you can use the extracted function as the inline function. In short, it is worth using to write code to humans.

4. Simplified function parameters

Using C language programming, there is a lack of Powerful Auto-completion tools, especially those who like vi. Although there are completed plug-ins below, they are rarely used and not very suitable). If there are too many function parameters, you can't remember the parameters that should be added. When writing code, you must constantly review them to reduce the efficiency, especially when programming with multiple people. However, this is sometimes difficult to avoid in the C language. You cannot set a large number of global variables for numerical transfer.

My solution is to set the struct, merge the parameters with similar meanings into a whole, and then set the value assignment function for the struct. In this way, the number of parameters of the main function is simplified to a certain extent, making it easier to remember.

Ii. Improve the overall code structure

1. Release the nightmare of global variables

You must use global variables in C programming. Global variables bring us great convenience, but they also bring us a nightmare. Sometimes, the global variable is not the expected value, but is quietly modified by the inexplicable Code. It is so painful to find the hacker in so many codes.

A good practice is to set the global variables to static, which can only be seen in this file, and then write the access/set function to control the access entry of the variables. Searching for functions is much easier than searching for a small value assignment statement, at least with breakpoints available. In the next step, if multi-thread or multi-process is used without breakpoints, you can use macro definition to write access control points and add printing to macro definition, print out all the files and functions of this statement. I don't believe it. I can't find it, hahaha.

2. Avoid implicit bugs in function passing.

The parameter transfer in C language is actually a piece of memory passed. During function execution, in this part of memory, the corresponding size of the SWAp block is obtained based on the parameter type. This leads to an invisible bug. For example, if you do not remember the function parameter type and pass a struct to it, the result is that the function is only a few int-type parameters, and the compiler will not report an error to you, what's even worse is that the Code can still run. If the test is insufficient, the Code may still run correctly.

Such an invisible bug will make you miserable. The simplest way to kill it is to add a function declaration. A flexible and convenient c compiler does not require function declaration to be added to function calls. function declaration is important especially when calling functions in other files. If the call is different from the declared type, the compiler reports an error. In this way, this invisible bug will not harass you. Therefore, do not worry about it. In order to live a harmonious life in the future, declare every function you want to call.

3. Adjust the function position

In multi-file programs, the function location is very important. You must adjust the Function Location Based on the functions completed by each file. The phenomenon of disorderly placement is very prominent in the call functions temporarily added. I often do this ). When the code is maintained later, related functions are not found based on the file name. The result shows that the function is in a file with completely unrelated content. In order not to disturb your thinking, just put it temporarily. But after this function is complete, you must reconstruct this function to the position where it should be.

For refactoring, this is what we think of now. In fact, some of them should be noted when writing. This should also be a sentence: refactoring should have been integrated into daily code writing. The highest realm is that there is no reconstruction in hand, and there is no reconstruction in mind.

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.