Writing code is like writing an article, a good article is repeatedly modified, and the code is reconstructed again and again. Today to share with you, how to change from a programmer to a program ape (Cheng)! At least don't let someone look at your code to know that you are a little rookie!
We usually write a code that inevitably passes through a simple-hard-concise process, so what do we need to pay attention to in the process of refactoring?
1, the code can run normally
First of all, it is necessary to ensure that the code can run properly!
Whether you are writing down the logic directly or functional programming, you must first implement your code function and it will work properly.
2, streamline the code, improve readability
Once your code is up and running, you'll need to re-read the code, and here are some things to optimize:
- · Does your code follow the PEP8 principles, such as naming, the length of each line of code, and so on, these details should be handled well
- · function refactoring, return value, default value, etc., to maintain a single principle of functional function
- · Are there too many if else nesting, can I extract
- · Global variable has no uppercase, no write to start
3, code comments complete
After the initial streamlining of the code or in the process, it is a good habit to write the comments in handy
Code is to show people first, and then to the machine to run! So the comment is a very important thing, some classmates do not like to write comments, feel very troublesome, then if this code is very short, then can not write, but if your code is very long, hundreds of lines, do not write comments will make you very confused force!
4. Complete exception handling, code is very robust
Code is timeliness! Especially in the crawler is deep experience, now can run normal code, over a period of time may be due to Web page revision, or cookies, SQL statements and so on changes, may lead to your code error, we have to consider these issues in advance, this requires exception handling mechanism, pay attention to the following points:
- · If it is a crawler, whether it is necessary to write the relevant function of the cookie, if IP proxy is required, consider the timeliness of proxy IP
- · Do you consider errors when looping through, such as getting an empty list
- · If branch is considered complete, there is no else case
- · File read and write whether there is a try statement, whether to consider storage location problems
- · Get a handle, like ssh,sql, so there's no consideration for the validity of the handle.
All exceptions to the code may need to be considered to ensure the robustness of the code!
5. Configuration Files
We have just written the global variables, now we can put these global variables into a configuration file, to implement and interface separation, reduce the coupling degree. For the user, just change the configuration file. For example, the whole global variable can be put into a config.py, and then in the main program with the From config import * This way, the subsequent modification is much easier.
If you can, write a redeme document, the use of your code in the environment, version, configuration profile and so on the information are written inside, for people are very convenient for themselves!
6, each unit function perfect, test various branch circuit
Although the program is written, whether your program is dozens of lines of small program small script, or thousands of tens of thousands of lines of the project, test cases must be designed.
Simple program can be set some assert assert, see some there is no exception, for complex logic, must be targeted design multiple branch loop repeatedly test code.
7. Add Log function
Some students said 6 steps above, I feel the code is very good, so there is optimization! Python code many are running in the service area, you can not always be print bar, especially for large programs, no log how to do, we recommend the logging module log records
8. Performance optimization
Structural optimization and threading, process, scheduling, and distribution are designed in advance.
If you are dealing with a task that is only hundreds of thousand, the performance requirements are not high, the real-time requirements are not high that's fine. If you want to process hundreds of thousands of data! This time must consider the concurrency of processing, in the end is a multi-process, or multi-threading, thread pool, or use the process, need to think!
Of course, performance optimization is not only single-threaded multi-threading, and data structure optimization, such as when to use the list, when to use a tuple, which is less memory consumption, query fast.
9. Function Change Class
To make our code easier to extend and adapt to change! We need to use classes to encapsulate variables and functions, design some interfaces, those that are externally developed, and those that are externally closed.
Which are wrapped with static functions, and which are used as instance methods. It is not necessary to use some adorners to simplify the code.
Functions of the same category, to be consolidated, merged into a class inside.
Multiple functions are represented by multiple classes for ease of maintenance and expansion.
Between classes and classes, consider their intrinsic relationships. Use combination or inheritance, with some simple design patterns, depending on the characteristics of the program such as Factory mode, observer.
Welcome attention and private messages, together to learn progress!
How do you make your code more pythonic? It's not a trick, but you need to see it.