Step 10 makes you a better programmer-it's worth keeping your favorites

Source: Internet
Author: User

10 steps to becoming a better programmer

ArticleArticleI want to introduce myself as a professionalProgramWhat students have learned over the years can actually improve me.Code10 things about quality and overall work efficiency.

1. Never copy code

Avoid repeated code at any cost. If a common code snippet appears in several different places in the program, refactor it and put it in a function of its own. Repeated code may lead to confusion when your colleagues are reading your code. If duplicate code is modified in one place and you forget to modify it in another place, a bug will occur everywhere, which will make your code bloated. ModernProgramming LanguageA good solution is provided to solve these problems. For example, the following problem is hard to solve before, but now Lambdas is well implemented:

2. Watch out when you get distracted.

When you discover that you are browsing Facebook or Weibo, rather than solving the problem, it is usually a signal that you need a short rest. Leave your desk, have a cup of coffee, or chat with your colleagues for five minutes. Although this seems a bit intuitive, it will improve your work efficiency for a long time.

3. Do not give up the principle in a hurry

When you take the pressure to solve a problem or modify a bug, you can easily lose your control, find yourself in a hurry, or even forget the important test process that you have been sticking. This usually leads to more problems that make you very unprofessional in the eyes of your boss or colleagues.

4. Test your complete code

You know what your code can do, and try it, it is really easy to use, but you actually need to fully verify it. Analyze all possible boundary conditions and test that it can work as scheduled under all possible conditions. If there is a parameter, pass some values out of the expected range. Pass a null value. If possible, ask your colleagues to check your code and ask if they can break it. Unit testing is a common method to achieve this purpose.

5. Code Review

Before submitting your code, ask a colleague to sit down and explain to him what changes you have made. In general, you will be able to find errors in the code in the process of doing so without the need for a colleague to say a word. This is much more effective than reviewing your own code.

6. Reduce code

If you find that you have written a lot of code to solve a simple problem, you may have done something wrong. The following Boolean usage is a good example:

 
If(Nummines>0) {Enabled=True;}Else{Enabled=False;}

In this case, you should write as follows:

 
Enabled = nummines>0;

The less code, the better. This reduces the number of bugs, less possibility of refactoring, and less chance of errors. Moderate. Readability is equally important. You cannot do this to make the code unreadable.

7. Strive for elegant code

The elegant code is very easy to read. It can solve the problem with only a small amount of code at hand and few operations on the machine. It is difficult to achieve code elegance in various environments, but after some time of programming, you will have a preliminary feeling about what the elegant code looks like. Elegant code won't be obtained through refactoring. It will be nice when you see elegant code. You will be proud of it. For example, here is a method that I think is an elegant method to calculate the Polygon Area:

  static   Public   double   getconvexpolygonarea (vector2 [] vertices) {  double  area =  0  ;  for  ( int  I =  0 ; I 
  
   ) {vector2 P0  = 
    vertices [I]; vector2 P1  = vertices [(I + 
    1 ) % 
    vertices. length]; area  + = 
    wide wedge (P1) ;} 
    return  area/
    2  
   ;} 
  

8. Write self-explanatory code

Without a doubt, annotation is a very important part of programming, but it can be self-evident because it allows you to understand it when you look at the code. Exercise caution when selecting a function name or variable name. When a good variable or method name is placed in a language semantic environment, anyone who does not know programming can understand it. For example:

 
VoidDamageplayer (player,IntDamageamount ){If(! Player. m_isinvincible &&!Player. m_isdead) {player. inflictdamage (damageamount );}}

Self-explanatory Code cannot replace comments. Annotations are used to explain "why", and self-explanatory Code describes "what.

9. Do not use pure numbers

Embedding numbers directly into code is a bad habit, because it cannot tell what they represent. Worse when there are duplicates-the same number appears in multiple places of the Code. If you modify only one, but forget the other. This leads to a bug. Be sure to use a named constant to represent the number you want to express, even if it appears only once in the code.

10. do not perform manual work.

As a series of actions, humans always like to make mistakes. If you are doing a deployment task that is not completed in one step, you are doing something wrong. Try to automate the work and reduce human errors. This is especially important when it is used as a task with a large workload.

11. Avoid Premature Optimization

When you want to optimize a usable functional code, you may break it down. Optimization can only occur when performance analysis reports indicate optimization is required, usually at the final stage of a project development. The optimization activities prior to performance analysis are a waste of time and may cause bugs.

Okay, I said 10, but you get an extra one!

These are what I want to talk about. I hope they can help you improve the programming and development process.

Goodbye next time! Happy!

Cheers, Paul.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.