11 tips for improving programs

Source: Internet
Author: User
Tags coding standards

There are many reasons to explain why we should be clear and readable.Program. 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 you the next dayCodeYou are about 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:

    1. Keep the method as short as possible
    2. Never use the same variable for multiple purposes
    3. Use the self-described variable name and method name
    4. Define variables as close as possible to use them.
    5. Reject Mysterious Numbers
    6. Treat your language with friendliness
    7. Do not line against the regular
    8. Be alert for premature optimization
    9. Actively refactor tested programs
    10. Do not over-indulge in skills
    11. 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 (in C ++Const, JavaFinal), So that the compiler can optimize compilation and make your code eye-catching.This variable cannot be changed.The readability of your program will be 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, please compare them.N, NS, nsisdAndNumteammembers, seatcount, 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 of code that uses bar,
// But "foo" is not used"
//...

Baz (FOO );

This code can be simply re-constructed

 
Int bar = 5;
// A large segment of code that uses bar,
// But "foo" is not used"
//...

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, rememberDefine 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. WhenProfessionalWhen people learn 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 a capitalized word (Verylongvariablename)
    • Class names should all be connected by uppercase words
    • The constant names should 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 what we needProfilersThe reason for this 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, 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. The design pattern is a very useful tool to simplify your design, but thisNoYou should use it 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. :)

 

Post: http://www.oschina.net/news/14326/11-tips-for-better-code? From = 20110109

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.