It seems that I have never studied a specific language, C, C ++, or VC, all of which are in practice. However, Java has read Thinking in Java. in actual development, excellent programming styles are very important. When we look at source code and some are open source code in the community, sometimes it is really painful. I have an open course "programming methodology" on Netease. Let's take a look.
Stanford gave a Karel class to teach programming on this basis. Record some notes.
Lesson 1st: course Introduction (2011.5)
It is much more interesting than the previous teachers in the school. Learning is really different from being learned.
Many people may write programs, but they will be different if they want to become masters. It is like writing a text, but it is very difficult to become a master.
Lesson 2nd: Welcome to Karel's world)
Programming should focus on readability. Programs should not only be written to machines, but also to people. This is the first condition for excellent programmers.
The program should be universal, not for single-case processing, the reusability of methods in dealing with complex situations. This depends on how we break down functions and combine functions. For example, in Java, we define what functions a method implements.
Lesson 3rd: Karel and Java (2011.7.8)
Error Handling: ensure that the loop exits to avoid infinite loops, especially when the first and the last exit cycles of the body is executed, that is, for the loop, you need to determine whether the first and last steps of the loop have completed the required actions, for example, whether to execute another action after exiting the loop, or execute the action once before the loop is executed to avoid the off by one bug. And whether this last step can effectively ensure that the loop is exited in the condition judgment, and whether various situations are taken into account.
Comment: to develop good programming habits, add comments before files and methods for easy reading. The precondition and result of the method are given. For example, Karel moves Beeper from one point to another and returns the result. Return karel to the original state, for example, in which direction is the original direction. If the original status is not completely restored, the subsequent operations will be affected. If the status changes, the post condition in the comment must be clearly stated. For example, if there is a method for walking sways and there is a row and a row, do you need to specify whether the post condition is, and the method has been switched.
Stepwise refinement: top down design ). Except for some specific situations, this method is usually used in the design. The bottom-up approach is a mistake many programmers make before getting started. It is a good choice to restore the initial state of decomposed actions and prepare to execute the next action. The current CPU does not care about more computing, but needs to write the program concise and clear. For example, karel, a small robot, moves and operates at a certain position, and finally returns to the original position, or faces a certain direction (not necessarily an absolute direction, such as a non-wall direction, this is to restore the original state so that anyone can get a good state after you finish processing it.
General principles of decomposition:
- Each method should be able to solve a problem with multiple layers;
- A method may be between 1 and 15 rows.
- Good method names, easy to read functions, and comment should be added (with prefix and Postfix conditions)
Appendix: Let's talk about Guo MM, what is the day for poverty alleviation, and I am notified by email that the amount of money each level of employee should donate is clearly written, even if you have a burst of love, there is also a limit, but if you don't want it, it generally doesn't work, because the Secretary collects the data one by one and basically implements it to the human head. In the past, our aunt had to accept the money, and then sent an email to the last day, usually falsely saying that the donation was voluntary, or that ordinary employees volunteered. The problem is that it was previously donated, and the corresponding part can be tax-free, but only the money is actively collected and never seen as tax-free. Guo MM is at the right time. The only result of the incident is that we are willing to do so this time, because everyone has decided not to do so ...... At that time, I was injured at home and thought someone had handed it over for me. I went back and heard that I didn't have to do it for the first time.
There is also Li Na's business, reward 0.6 million, tax 0.12 million, will donate 0.48 million (Supplement 20 thousand to 50 ). There is a big problem here. If the donation does not deduct the tax, that is to say, if Li Na wants to donate, it will be able to donate 0.6 million yuan in total, without the need to deduct the tax.