It took a long time to translate for the first time, and it was inevitable that there would be some bad words. In some cases, I could not think of any good words, so I added the English language, but the code was not changed, welcome!
10 steps to becoming a better programmer. The following is the translated text:
Here, I want to introduce 10 skills that have helped me improve code quality and overall quality in the past few years after I became a professional programmer.
Avoid repeated code at any cost. If you have the same code snippet in several different places, write a function to refactor it. When your colleagues read your code, repeated code will make them puzzled. when only one of them is modified, and the other code is not modified, a bug will certainly occur, it also makes executable code bloated. Modern programming languages have been able to overcome these shortcomings. For example, the example below is difficult to solve in the past, and it will be well implemented after the emergence of Delegate and lambda.
/// <summary>/// Some function with partially duplicated code/// </summary>void OriginalA(){DoThingsA(); // unique code DoThingsB();} /// <summary>/// Another function with partially duplicated code/// </summary>void OriginalB(){DoThingsA(); // unique code DoThingsB();}
Now we can use delegate to refactor and rewrite functions that contain some of the same Code:
/// <summary>/// Encapsulate shared functionality/// </summary>/// <param name="action">User defined action</param>void UniqueWrapper(Action action){DoThingsA();action();DoThingsB();}/// <summary>/// New implmentation of A/// </summary>void NewA(){UniqueWrapper(() =>{// unique code});}/// <summary>/// New implementation of B/// </summary>void NewB(){UniqueWrapper(() =>{// unique code});}
- 2. Pay attention to the times when you are getting distracted.
When you find that you are browsing Facebook or Twitter and are not focused on solving the problem, you should have a short rest. Get up and leave your desk for coffee and talk to colleagues for five minutes or something else. Although this is a bit unreasonable, your work will be more efficient if you stick to it for a long time.
When people are under pressure to solve problems or modify bugs, they will easily lose their minds. You will find yourself in a hurry or even completely forget the usually very important test, this often leads to more problems that make you unprofessional in front of your boss and colleagues.
- 4. Test the code you have written.
You know what your code can do and it works during testing, but you still need to fully verify it: analyze all the boundary conditions, make sure that your code can achieve the expected results when testing under various conditions. If there is a parameter, pass a value out of the expected range, and pass a null value. If possible, present the code to your colleagues and invite them to test it. Unit testing is a better way to achieve this goal.
Before you submit the code, sit down with a colleague and give him a precise description of your modifications to the code. Many times, in this process, you can find out the problem from your code without a single sentence from your colleagues. This is more effective than reviewing your own code.
If you find that you are using a lot of code to solve a simple problem, you may have done something wrong.
if (numMines > 0){ enabled=true;}else{ enabled=false;}
In this case, you should write:
enabled = numMines > 0;
The more concise the code, the fewer bugs, the smaller the possibility of reconstruction, and the less chance of error. However, to make it easier, because readability is equally important, you certainly don't want to make your code less readable because it is simplified.
- 7. Strive for elegant code
The elegant code is easy to understand and can solve the problem with the minimum amount of code and smaller machine runtime. Although it is difficult to write elegant code under any conditions, after a period of programming, you will feel something about its form. Elegant Code cannot be improved through refactoring, but it makes you very comfortable and proud of it. For example, the following is a method that I think is more elegant to calculate the Polygon Area:
static public double GetConvexPolygonArea(Vector2[] vertices){double area = 0;for (int i = 0; i < vertices.Length; i++){Vector2 P0 = vertices[i];Vector2 P1 = vertices[(i + 1) % vertices.Length];area += P0.Wedge(P1);}return area / 2;}
- 8. Write self documenting code)
As we all know, annotation is a very important part of programming. However, the self-recorded code is superior, and people can understand the code through it. Functions and variable names must be flexibly selected so that they can be understood by non-programmers even if they are put in any language environment. For example:
void DamagePlayer(Player player, int damageAmount){if (!player.m_IsInvincible && !player.m_IsDead){player.InflictDamage( damageAmount );}}
Self-recorded Code cannot replace comments. Comments are used to describe "why", while self-recorded Code describes "what ".
- 9. Do not use pure numbers (don't use magic numbers)
Embedding Code directly with numbers is a bad habit, because it cannot explain what they represent. This is especially bad when the same number is used repeatedly in multiple places. A bug may occur if you modify only one address and forget other places. You must use a name constant to represent the number you want to express, even if it is used only once in the code.
- 10. Do not do physical work
People are prone to mistakes when performing a series of actions. If you are building and deploying a project that requires many steps, you may have some problems. Efforts should be made to automate work and reduce human errors. This is especially important when you do a task with a heavy workload.
- 11. Avoid Premature Optimization
When you begin to optimize some of the code that is already running, it is very likely that you will damage its performance. Therefore, the optimization should be performed only in the final stage of project development, after the performance analysis is completed, and it indicates that optimization is required. Optimization before the analysis stage is complete is a waste of time and can cause bugs.
I wanted to talk about 10 tips, but you got an extra one for free!
I hope these tips can help you improve your programming and development processes.