External media evaluation: 11 tips for improving programs _ PHP Tutorial

Source: Internet
Author: User
Tags coding standards
External media evaluation: 11 tips for improving programs. There are many reasons to explain why we should write clear and readable programs. The most important thing is that you only write the program once, but will read it countless times in the future. When you go back the next day, there are many reasons to explain why we should write clear and readable programs. The most important thing is that you only write the program once, but will read it countless times in the future. When you look back at your code the next day, you will begin to read it. When you show your code to others, you must read your code. Therefore, it takes a little more time to write and you will spend a lot of time reading it.

Let's look at some basic programming skills:

  Keep the method as short as possible

Never use the same variable for multiple purposes

Use the self-described variable name and method name

Define variables as close as possible to use them.

Reject mysterious numbers

Treat your language with friendliness

Do not line against the regular

Be alert for premature optimization

Actively refactor tested programs

Do not over-indulge in skills

Learn new knowledge through examples

Now, let's show each small point in detail.

1. keep the method as short as possible

Although many people follow this rule, it is still very important. The method you write must always be able to put in a screen. If you need to scroll the screen, this will distract you and you will not see the entire context. The optimal length is 5-20 rows, depending on your situation. Of course, getters/setters are usually a line of code, but they are not so much a real method as an access tool.

2. never use the same variable for multiple purposes

A variable should always serve only one purpose. By making the variable constant (const in C ++ and final in Java), the compiler can optimize the compilation and make your code clearly express that the variable cannot be changed, the readability of your program will become better.

3. use the self-described variable name and method name

Your code should, for anyone, just take a look at it to know what it is. Do not use abbreviations unless you have special habits, as shown in the following:

Src-source pos-position prev-previous

If you think descriptive names are not that valuable, compare them with n, ns, nsisd, numTeamMembers, seatCount, and numSeatsInStadium.

4. define the variable as close as possible to use it.

When building a house, you don't want to put a hammer in someone else's yard. You want to keep them as close as possible. The same is true for defining variables.

Int foo = 3; int bar = 5; // A large segment uses the "bar" code, // but it does not use "foo" //... baz (foo );

This code can be simply re-constructed

Int bar = 5; // A large part of code that uses "bar", // but does not use "foo" // ...int foo = 3; baz (foo );

When you declare a variable and use it for the first time at a long distance (beyond a screen), this is indeed a problem. It is difficult to remember the context. you need to scroll the screen to find the variable.

5. reject mysterious numbers

When you compare something with a constant value, remember to define this value as a constant. Nothing is more troublesome than guessing that your colleagues have written such code:

Il & lt; 4384

How do I feel about changing the form?

InputLength <MAX_INPUT_LENGTH

6. Treat your language with friendliness

Learning a new language is a pleasure. you can learn a new way to complete a task. When a person who is already very professional in one language learns another language, there will be a great negative effect. For example, you are a Java developer trying to learn Ruby. You should learn to solve problems in Ruby instead of using Java's solution to problems.

When you need to repeat "Hello world! ", In Java, you may do this:

For (int I = 0; I <5; I ++) {System. out. println ("Hello world! ");}

In Ruby, you may not be able to write like this:

For I in (0 .. 5) puts "Hello world! "End

This seems okay, but there is a better way:

5. times {puts "Hello world! "}

7. do not line against the regular

Each language has its own different conventions. Generally, most people listen to Java coding standards. Let's take a look at some of the customs rules:

The method name should start with a lower-case letter, followed by veryLongVariableName)

Class names should all be connected by uppercase words

Constant names should all be in uppercase and connected with underscores (MY_CONSTANT)

The left braces should be the same line as the if statement.

Break these rules only when necessary, and do not violate them easily because you are not happy. If you just change some of these habits in the team, it's okay, but when you share your code with other programmers who are not prepared for these ideas, the problem will come.

8. be cautious about premature optimization

Early optimization is the root cause of all problems, at least on TV... The first thing you should care about is writing easy-to-understand code. Programs written at first should not be fast. It is too early to talk about optimization unless your program is slow. If you want to optimize something, you first need to know where the problem is. This is why we need the profilers tool.

If you try to optimize the program without knowing where the problem is, the result must be that the program can be broken, at least your code will lose readability. If you think it is slow in some places and do not blindly rewrite the code, you should first find evidence of slowness.

Don't be dumb to solve a non-existent problem.

9. actively rebuild tested programs

Nothing will be perfect. Even if you feel that you have actually written a perfect piece of code, you may be surprised to look back a few months later. "How can it be so silly? "

A good way to improve the program is to refactor, but after the program passes the test. First, make sure that the program runs properly. you can do this through automated testing or manual testing.

At the beginning, what you need is program availability. Don't expect to write a perfect program for the first time. you just need to write it out and use it. Then refactor it to make it perfect. Those of you who know Test-Driven Development (TDD) will be familiar with this. The key here is that you have to get used to refactoring such a thing. If you are using a powerful integrated development tool such as IntelliJ IDEA, refactoring will become much simpler.

After reconstruction, you may encounter some bugs, which may cause some functional problems. This is why write automation testing is used. After reconstruction, you only need to run all the test cases, and you will be able to know exactly what went wrong.

10. do not over-indulge in skills

When I read about design patterns for the first time, I thought I had found the Holy Grail. These well-designed ideas play a significant role, making your design easy to understand, because you can simply say, "I am using the 'Observer mode'", instead of explaining it from start to end. So, is there a problem? Everything looks so natural and simple that you start to use the design pattern wherever you are. Why not make this class singleton? Why not create some factory classes?

As a script that can be written in 80 rows, you finally use 10 classes and 15 interfaces, plus a lot of paradigms and tokens. 97% of the code does not do anything. Design patterns are very useful tools to simplify your design, but it doesn't mean you should use them wherever you can. You should use them, but you cannot abuse them.

11. learn new knowledge through examples

Programming is a process of learning new knowledge. When you learn a new library or language, you may be impatient to drop the old code and re-write it with what you learned. There are many reasons why you shouldn't do this.

This is the case when a new class library or framework is added to an existing application. That is to say, you wrote a Javascript web application, during which you discovered jQuery. Now you are eager to drop your Javascript program and write it again with jQuery, even though you have never used it.

The best way is to use jQuery to write some simple examples. in this way, you can learn the knowledge you will use in your application. Need AJAX? Make some small examples outside of your project. when you fully understand it, discard the example and apply it to your product.

If you are very concerned about programming technology, I strongly recommend that you read the book code Daquan written by Steve McConnell. It will change your understanding of programming forever. :)

Bytes. The most important thing is that you only write the program once, but will read it countless times in the future. When you go back the next day...

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.