Copyright statement
This article can be freely reproduced, but it must follow the following copyright conventions:
1. retain this agreement and keep it at the beginning of the article.
2. You cannot modify the content of an article, or delete a section to add the content. If you believe that the content of this article is inappropriate and needs to be modified, please
Contact the author.
3. The content of this article cannot be excerpted. It must be published in full or referenced.
4. The author's signature must be kept and the source of the article must be indicated. (This article authorizes www.linuxaid.com.cn)
5. If you do not comply with this provision, you do not have the right to repost this article.
Author
Ariesram
Email Address
Ariesram@linuxaid.com.cn, or ariesram@may10.ca
This article and all my articles are collected in Bambi. may10.ca /~ Ariesram/articles.
This document authorizes www.linuxaid.com.cn.
Body:
If there is a programming method that can overcome all the difficulties and correct all the errors, and avoid rewriting the code, I believe we will use this method. Because there is no perfect programming method, what we can do is to see how the programmers around us do, what is correct, and what is wrong. There are some programming methods I have summarized from my actual work, and I do feel that they can help me develop good programming habits. The most important thing is to remember that when you see these experiences, don't think they are too simple and basic, and think that a method is not worth learning. Many Programmers think that checking errors and writing program comments is a waste of time. In my opinion, these experiences can help us save time and energy. In practice, I learned that I can write code faster and code more efficiently.
Make the worst plan
If you are a super programmer, your code will never be wrong. However, what will happen if your perfect code does not get the perfect data? Your code assumes that a pointer is valid, or does it process a sound file as an image? Basically, a piece of code cannot assume anything. C language is another standard function assert, which can be used to capture errors. Every time your code receives user data, make sure that the data is exactly what you expected. If not, use assert and print the message to explain the error. This is very important, so that you can let anyone who reads your program understand what is correct data and what is wrong. 90% of errors are simple errors. Therefore, do not let such errors affect your program to waste debugging time, but simply give an assert in those places to avoid it. 90%, it can be easily corrected. At another 10 percent, it can be noticed and corrected by the debugging personnel before it becomes a major error. No matter which programming language you use, the first program you write usually prints a message. Use this message printing function as a basic function in your program to print any errors. In this way, the program can identify non-obvious errors, and you can also use this function to print error messages wherever any errors may occur. In this way, you can save the time for searching for errors and shorten the time for correcting errors.
Note
Do not try to remember what your code is. After writing a program for a few months, you won't remember the idea when writing the program, or what code is used. Therefore, writing comments is a good method, especially when you need someone to read your code, or to remember this code after six months. If a colleague tells you that your code has an error, you will have to re-check and correct it. If you can recall the code used for doing something through comments, you can quickly locate and correct the error. This method is also relatively simple. You only need to specify what your code is. This is enough. If you do not, other people who read your program will not understand what it means, do not know what variables are used for, and what complicated computations are used. If you explain their meaning, it's much easier. For example, if "(FRMP> 10)", "(plist [I]. bdown & x03) "," (plist [I]. y> pond. y) "is much more complicated than reading comments. When you write comments, you will get two benefits. Anyone can understand what you want to do with a piece of code, and if this piece of code has errors, the person who reads the code will be able to find out, it does not execute what you want it to do in the comments, so that you can detect errors and correct them as soon as possible. Annotation is one of the most important tools for programmers. All languages Support annotations. So remember to write comments.
Document
When I was writing a document, I remember that I spent a lot of time writing a document about systems and modules. This document is correct, but it is useless. Because no one has read it. Many people forget this document, but ask me when they need it. Let me explain it to them. This method is also good, it is much faster than reading the entire document. There are few chances that someone will spend a long time reading the entire document. Therefore, it was a waste of time to write documents. In addition, if the system and module need to be modified, I must modify the document accordingly. That is to say, this document doubles my work. However, this does not mean that the document is unimportant. On the contrary, it is much easier to record documents with source code and instructions. At the beginning of each function, a comment is used to explain the functions and usage of the function. For a complex piece of code, you need to explain the method you are using. There is no need to use another file to record documents, but to write documents only in the middle of the source code. In this way, you can find them whenever you need documents. Other programmers can easily use your code. Moreover, unlike a dedicated and independent document, other programmers will unconsciously read your document without ignoring it. If someone asks you what a piece of code means, you will understand that the piece of code lacks clear comments. Therefore, you can add it as soon as possible without another programmer asking you the same question.
Tools used
In programming, you may often encounter such heavy physical labor, such as compiling a program, and then waiting. Or you use API functions written by others, and it is a very tiring task to remember the names and parameters of these functions. There is no technology for these jobs. For example, if you compile a program, someone puts the newly added programs in the library every night. Then you need to re-compile them after work the next day, it is often a bit of repetitive work, but because of the large program, the compilation process is very long, and you have to wait. So why not use tools? Or write some such tools? I did that. I wrote a program that started at every morning and re-compiled the program. By AM, the compilation was almost complete. Then capture the error. If yes, send an email to the relevant person. In this way, at work time, everyone can get the latest compiled program. If there is an error in the code, you can get the fastest Error Report. For another example, I need to use API functions written by others. Of course, I can't expect everyone to write functions using the same naming method and Parameter definition method, it is also impossible to view documents every time you need to use them, which is too time-consuming and inefficient. I wrote a tool to check whether my function call is correct and whether the parameters are correct. If an error occurs, find the possible function in the document and give a prompt in the error log. Then I can write code quickly without worrying about the Function spelling and the type and sequence of parameter calls. You can also do this. When someone asks you about the name, type, and sequence of a function, you can tell him what to do and what tools to use.
Reusable code
There is a good example to describe an engine. A friend who is not a programmer asked me a question. What is a program engine? What is the function? Why use the engine? I try to answer it in plain language. A program engine is like a car engine. Without it, cars cannot be started. However, at the same time, an engine without tires is useless.
I think this is a good example. This friend said, when your engine is not available, you can change one. If the drive belt breaks down, you
It can be changed to a new one, and the engine performance can be adjusted to the best. Similarly, an engine has the same part, such as a drive device. They are important to an engine. If a part is broken, you must break it apart.
My friend concluded that if you write a program engine from the ground up, you will have to write all the transmission belts and all the parts from the ground up. If you can use some previously used parts, you can simply assemble them together without re-writing them.
Yes. He doesn't know how to write a program, but he tells the true meaning of programming. This is also the difference between a common programmer and a Senior Programmer. Reusable code makes work simple.
The key to writing General code is not to make your subroutine longer than a screen. The key is to break down what you want to do into small, reusable functions, either to complete a function or to call another function to complete a complete function.
For example, vectoradd () may contain a piece of code that combines the elements of two vectors, while scenedisplay may contain code that calls preprender (), render2dobjects (), and renderhud. Each function may have only a few rows.
When you break down the code into a small part, you will be able to focus on some functions that can be reused elsewhere. In the previous example, renderhud () and renderdebugtext () may share the same function call because they all draw some objects on the screen.
Many people think that you must have a good design when making a common code. Design is an important step when writing modules and systems. However, it is not enough to do this. What I want to say is to develop a habit of turning every piece of code you want to write into a common and reusable code. In this way, when you design a new system and module, you will have a large number of functional modules that can be reused. You only need to call some of these functions, instead of writing all the code from the beginning.
Be a good carpenter
The carpenter uses some raw materials to build a Wooden Ware Based on his learning experience and imagination. In the early stages of their work, they may use some simple tools, such as hammers, nails, and slashes, to create wood tools. But after they have accumulated some experience, they can do more advanced things, such as adding a door to a house. When a carpenter gained more experience, he could have the opportunity to build a whole house and lead a group of people to work and tell them who is doing what, for example, someone specialized in planing wood, someone specialized in nailing, and so on.
These advanced carpenter are valuable, not because they know how to use a lot of tools, but because they know how to and when to use them. When you give a carpenter a hammer and let him put a door on the door frame, he will ask you for a screwdriver and a screw. If you give a hammer to an ordinary person, he may think that the door was nailed. If you give a child a hammer, he will take the hammer to knock on everything, as if everything has become a nail.
You must have good judgment capabilities. When you solve a problem, do not always try to solve the problem with the latest technology or the technology you are most familiar with, because it may not be the same. It is important to always use the most suitable tool in a specific scope to solve the problem.
Admit what you don't understand
We are all human beings, and no one knows everything, so we do not understand everything. What should you do when you are asked by the leaders to complete a task that you are not familiar? If you answer, "I won't, sorry, I don't know how to do it ". I believe no one will answer this question. This will make you have no face in front of the leadership, but also lose part of your value. In the above example, many people think that it is best not to say anything, lest others think that you do not understand what you are doing. Then they close their mouths, work out, read more books and online articles, and complete the task on time.
However, you often close your mouth, work out, read any books and articles you can find, but you still cannot finish the task on time, or it takes more than a month to complete the task. You completed the task, but how much did you spend?
If you do not know the task to be completed, and you do not know enough about it, the best way is to acknowledge it. Instead of trying to hide or lie. If you say that you are not familiar with it or do not know how to solve the problem, you can say, "I don't know this problem. It is a new problem for me, is there anything you can tell me in which direction to do it, or is there any similar example for reference? ". Of course you can't say that," I won't do it. I am useless. Fire me. "What you want to express is," I may not understand, but it doesn't matter. I am willing to learn. I am willing to learn and finish my work on time ". Many other programmers may be happy to share their knowledge and experience with you. In this way, you can learn what you want to learn faster and complete tasks on time.