What creates good code?

zhaozj2021-02-12  206

Paul Dilascia (James506)

When MSDN magazine let me write a special page, I said: "You are like abolishing taxes, car calls, or suvs?" Of course, their finger is some about programming. After some thinking, I realized that the problem I care is to write a good code. From REDMOND to ZWAZILAND, all books and magazines tend to promote how to use the latest API or some advanced objects, but there is no code about how to write, what is a good code. A good program is very good, there is no flaw. But what is inherent causes so perfect? This is not mysterious, we only need to remind yourself, whether you are using C / C , C #, Java, Basic, Perl, Cobol, or ASM, all good codes do not show the same feature: simple, Easy to read, modular, hierarchical, design, efficiency, elegant and clarity. Simple means you can use the code to complete with the five elements, don't use ten lines. This requires your extra effort to simplify, but don't excessively so that the code is embarrassed. The organization, implementation, and design make it more reliable, away from BUG, ​​not wrong. Easy reading is to let others read your code. Need to write a note, follow the regular naming, do not name your job. For example, "TaxRate" is better than "TR". The modularity is that your program is like the universe. This world is composed of molecules, while molecules are consisting of atoms, electronics, knots, quarks, and strings (if you believe). Similarly, the large system in the program consists of some small parts, and these small parts are composed of smaller modules. You can write a very simple text editor that contains only three parts: move, insert, and delete. As the atom can be combined in a variety of wonderful ways, the components can be reused. The layering allows your program to see a sandwich cake from the interior. The application is built on the framework, and the framework is based on the operating system, and the operating system is built over the hardware. Even in the application inside the application, it still needs to be hierarchical, such as File-Document-View-Frame. The high layer will call the low layer (call down, respond to up). The low level will never know what the top level above is. If DOC calls Frame directly, things will become very bad. The modules and layers are determined by the API, and the API limits their boundaries. From this point of view, it is very important. The design means that it takes time to take a good design before your program is established. After all, early serious ideas should be more cost-effective than the labor force debugging in the later period. A truly trust is designed with half of the time. You need to have a functional manual (what to describe the program) and a development plan. All APIs must write a file. Efficiency means that your program is running fast and small cost. Not caused by files, data links, etc. It only does it should do. Orderly load and unload. On the function level, you can always optimize in the test. But at a higher level, you must have a good plan for performance. Elegant is like beauty, you are difficult to describe, but it is easy to identify. Elegance is a feeling of simplicity, efficient and gorgeous, and created. As follows: int FACT (int N) {RETURN N == 0? 1: N * FACT (N-1);} Clearly the most important one in all other characteristics. Computers provide possibilities for creating multi-software systems that are more complex than physical hardware. The biggest challenge of programming is to manage complexity.

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

New Post(0)