9 programming habits of successful software developers

Source: Internet
Author: User
Tags exit variables printf
Programming good original program to make good software

Some people think: as long as the program works well, no matter how the original program is compiled. But that's definitely not the case. Software is not done at once, it is necessary to make changes, extensions and other management. So the original program to make as easy as possible to understand, management convenience.

To do this, the first is for the software developers convenience, and secondly, it will affect the performance of the software. Managing inconvenient programs does not make good software.

Hope that through this article can learn good programming habits. To understand the content of this article, you need to know at least 1 development tool languages. Here are the examples of C language, but you do not know the C language and do not worry. This explains the principle, not the specific language.

1. Complete---of the statement (semicolon;)

One of the most common mistakes programmers have is to forget to add a semicolon after the statement ends. Such problems are difficult to spot and sometimes leave programmers at a loss. Always keep in mind when programming that each statement ends with a semicolon, although not all languages end with semicolons. Here is an example of a forgotten-dot semicolon.

int main (void)
{
/* No semicolon, cause problem * *
printf ("Hello world!\n")
return (0);
}

A lot of people commit such a mistake. Less than a few procedures are not difficult to find such a problem, but more than 1000 of the procedures? It won't be easy to find a statement that forgets to write a semicolon. Remember, to end a statement, be sure to write a semicolon, just like the end of a general article.

There is also a semicolon error is not to write a semicolon when writing a semicolon. Experienced programmers will find it amusing to see the following example, but I do see a lot of such mistakes.

/* Main () should not be followed by a semicolon * *
int main (int argc, char *argv[]);
{
printf ("Hello World");
return (0);
}


You should not write a semicolon after a function or method.

2. To be suitable for using spaces and TAB keys

C language is not to distinguish the space, so the program can not need to write down the space, but such a program will be no one can not understand the "very difficult" procedure, please see the following examples:

if (x==0) {A=b=c=d=max; x + +;}

This writing, may save space, but not only others, programming I will be difficult to understand. Program to write easy to understand!

if (x = = 0)
{
A = b = c = d = MAX;
x + +;
}

Does it look very clear to you? The procedure must have the exact space to be easy to understand.

3. Uniform use of curly braces and cutting methods

Each programmer has his or her own habit of using braces ({}) and the way they are diverted, so that when the program is handed over to others, there is confusion. For example, like the following example:

int main ()
{
int x = 1;
int y = 10;
while (x < y) {
printf ("Value of X is%d\n", x);
x + +;
}
}

Some programmers write curly braces like this:

int main ()
{
int x = 1;
int y = 10;
while (x < y)
{
printf ("Value of X is%d\n", x);
x + +;
}
}

The author is fond of the second way. Because the beginning and end of a statement is obvious. We can't ask every programmer to program in a certain way, but a program must be unified. Also, the habit of looking at other people's programming may be different from yours.

4. If statement not in random use

Some people like to use the "if" statement as follows:

if (a = = 0)
{
a++;
return (a);
}

if (a = = 1)
{
A + + 5;
return (a);
}

if (a = = 2)
{
A + + 10;
return (a);
}

if (a = = 3)
{
A + + 20;
return (a);
}

if (a = = 4)
Exit (1);

Is there any better way to do it? else if statement? No. A good way to write a simple program is to use the "switch-case" statement:

Switch (a)
{
Case 0:a++;
return (a);

Case 1:A + + 5;
return (a);

Case 2:a + + 10;
return (a);

Case 3:a + + 20;
return (a);

Default:exit (1);
}

If no value is consistent with a, the job defined in default is executed, and the example above is the end of execution.

5. Not disorderly use the procedure to cut off (block)

Many people often use the program to cut off. Using more than three cuts is a more difficult process to read. Take a look at the following example:

int a = 10;
int B = 20;
int c = 30;
int d = 40;

if (a = = 10)
{
A = a + D;
if (b = = 20)
{
B = B + A;
if (c!= b)
{
c = c + 1;
if (d > (a + B))
printf ("Made It all of the way to the bottom!\n");
}
}
}

This may be exaggerated, but a lot of people really do. So how do you write better? One way to do this is by using functions to write:

void Next (int a, int b, int c, int d)
{
if (c!= b)
{
c = c + 1;
if (d > (a + B))
printf ("Made It all of the way to the bottom!\n");
}
}

int main ()
{
int a = 10;
int B = 20;
int c = 30;
int d = 40;

if (a = = 10)
{
A = a + D;
if (b = = 20)
{
B = B + A;
Next (A, B, C, D);
}
}
return (0);
}

Writing like this may increase the amount of work, but the program is structured, easy to read, and can be reused elsewhere if the function is done better.

6. Write a good note

Develop the habit of writing notes. In particular, other difficult to understand the program or variables must be commented, one months later, perhaps you also need to see the note.

int x = 100;
int y = 1000;

if (x < y)
A = 0;
Else
A = 1;

Can you tell what the above program means? It's hard to understand if you don't know what variable x,y and a means. Let's give it a note and see:

/*
* Procedures for detecting profit and loss
*/

int x = 100;
* * x is the total amount of the book sold/
int y = 1000
* * y is to do the cost of books * *
int A;
* * To confirm whether there is a profit * *

/* Compare x and y: * *
if (x < y)
/* 1 means loss * *
A = 1;
Else
/*0 refers to the profit * *
A = 0;

In this way, people who do not understand the C language will understand what each statement means and what each variable refers to. Writing a note is a good habit, but don't annotate everything. Comments are for good understanding, not for writing Chang articles.

int profit = 1;
* * Profit equals 1?? */
int loss = 0;
/* loss equals 0?? */

* * If the profit is equal to 1 * *
if (Profit = 1)
/* Print out "make a profit"?? */
printf ("We made a profit!\n");
* * If not * *
Else
/* Print out "We lost money" * *
printf ("We made a loss!\n");


Such annotations are a waste of time. It is generally necessary to annotate variables or variable values, and to explain the purpose of the program, using a function, Procedule and so on.

7. The name should be reasonable

Programs, variables, procedure,structure and so on must be associated with its content, variables do not name such as "X", "Y", "Z"

Perhaps you will say the author in the example cited above also used x,y and so on, can I do the reason is that he does not involve other programs, the actual development of software, the author of course will use meaningful variable name. Take a look at the following example:

void x (int a, int b)
{
int z;
Z = a + b;
printf ("Z is%d\n", z);
}

Here, we can know what X does, but we don't know what it means. The same is a,b,z. Let's look at it a little bit:

void sum_of_ages (int jacks_age, int jills_age)
{
int total_age;
Total_age = Jacks_age + jills_age;
Print ("Total_age is%d\n", total_age);
}

Although there is no annotation, it is easy to see what to do. The name is good, no annotation is possible.

8. Confirm Buffer

Always confirm the size of the arrangement or variable you are setting to avoid data clutter or cause system problems. Please see the following user input data example: Char city[10];
/* For the city name of the arrangement * * *

printf ("Enter a city Name:");
scanf ("%s", city);
printf ("City are%s\n", city);

Here, the city name is set to 10 words (English words) within. What if the user entered the city name more than 10 words? You can say that the program fails or overwrites the data inside the buffer. Anyway, don't do anything risky, you have to check the length of the text:

Char city[10];
/* For the city name of the arrangement * * *

printf ("Enter a city Name:");
Fgets (city, sizeof (city), stdin);
printf ("City are%s\n", city);

The modified procedure above is: if the user input more than 10 words of the city name, only accept the length of the buffer can be accepted, it will not accept.

9. Never trust the user

This is a very important rule. Never trust someone who is going to use your software, don't think that the user will do what you want them to do, but instead think of them as someone who specializes in finding bugs. For example, the above procedure is to confirm the length of the text for those users who enter the city name longer.

Like C language needs a clear form of data development tool users, to remember to confirm the input data form and program data format is consistent, otherwise there will be problems.

Cutting the wood without a knife

The above programming habits are for your own convenience. Before you actually program, do more preparation (analysis, design). Although slightly troublesome, but in order to make the process of easy to understand and tidy, do not hesitate to spend time preparing, or later will need more time to modify, expand your program.




Related Article

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.