What is a good programmer? (Reproduced)

xiaoxiao2021-03-06  34

What is a good programmer? Is it a lot of technical details? Or do you understand the underlying programming? Or is the programming speed is faster? I don't think it is. For some technical details and the underlying technology, as long as you see help, check the information can be found. For speed fast, as long as you make more, you will be able to happen.

I think a good programmer should have the following qualities:

1. There is a special spirit, diligent good, and one anticipate.

2, positive attitude, creative thinking.

3. The ability to actively communicate with people, there is team spirit.

4, modesty and cautious, arrogance and arrogance.

5, the code written is high. Including: Code stability, easy reading, standardization, easy maintenance, professional.

These are the cultivation of programmers. Here I want to talk about "programming cultivation", which is the 5th point in the above. I think, if I want to know a author, I will see what he wrote. If I want to know a painter, I will see the drawing he draws. If I want to know a worker, I will see what he makes it. Similarly, if I want to understand a programmer, I want to first think that his program code, the program code can see a programmer's quality and cultivation, the program is like a work, qualified procedures The work must be a beautiful picture, a wonderful song, a pleasing novel.

I have seen many programs, there is no comment, there is no narrow, the name of the variable name, etc., wait, I will collect this person as there is no cultivation, this programmer is doing creative work? No, it is completely destroyed. They are said to be programs, but it is better to "encrypt" on the source program. This kind of programmer should be expelled, because he edited the value, It is far less than the value that needs to be maintained above.

Programmers should have programmers, so afraid of tired, no time, but also to be responsible for their own procedures. I would rather want the programmer that is slow, technical, but there is a good writing program style, or the programmer of "destroying" skills. There is a saying that "the word is like a person", I want to see a programmer from the program. Because the program is a programmer's work, the quality of the work is related to the reputation and quality of the programmer. The "cultivation" good programmer must make good procedures and software.

There is a idiom called "alone", meaning what you have to do very professional, very careful, if you have to be a "craft", it is a deep person, then, from a very simple work. It seems that you have the characteristics of "craftsman", I think it is not difficult to do a programmer, but it is not easy to do a "programmer". The programming is very simple, but it is difficult to compose quality procedures.

I don't discuss excessive technology here, I just want to say something that is easy to ignore, although these things may be fine, if you don't pay attention to these subtle, then he will be very big. The impact of your entire software quality, as well as the implementation of the entire software, the so-called "Thousand Miles of Bius, Destroyed in Ant Accent".

"Seeing the truth in the wealth", I can really embody the bottom of a program is just in these subtle.

This is the programmer - programming cultivation. I summed up 32 "cultivation" on the program writing with C / C language (mainly C language). Through these, you can write a high quality program, and you will also let the human stains you have Stains, those who have seen your procedure will definitely say: "This person's programming is not bad."

---------------------------------------------------------------------------------------------------------------------------------------

01, copyright and version

02, indent, space, wrap, blank line, alignment

03, program comment

04, function [in] [out] parameters

05, the return to the system call is determined

06, the IF statement handles the error

07, #ifndef in the header file

08, allocate memory on the heap

09, the initialization of variables

10, H and C files

11, the processing of error information

12. Commonly used functions and cyclic statements

13, naming of functions and variable names

14, function of the function and the pointer

15, modify the cultivation of others procedures

16, form the same or nearly the same code form function and macro

17, brackets in expressions

18, constant in the function parameters

19. Number of parameters of functions

20, the return type of the function, do not omit

21, the use of GOTO statement

22, macro use

23, Static's use

24. Code size in the function

25, TypeDef's use

26, declare the macro for constant

27, don't define the semicolon for macro

28, || and &&'s statement execution order

29, try to use for for a loop with for while

30, please sizeof type instead of variables

31, don't ignore WARNING

32, writing Debug and Release version

---------------------------------------------------------------------------------------------------------------------------------------

1, copyright and version

-------

Good programmers will pay a copyright and version of each file, each file.

For C / C files, the file head should have a comment like this:

/ ************************************************** **********************

*

* File name: network.c

*

* File Description: Network Communication Function Set

*

* Created: hao chen, February 3, 2003

*

* Version number: 1.0

*

* Modify record:

*

*********************************************************** ********************* /

For functions, there should also be similar to this note:

/ * ================================================================================================================================================================ ================

*

* Function name: XXX

*

* Parameters:

*

* TYPE NAME [in]: descripts

*

* Function Description:

*

* ..............

*

* Return value: Success True, failed False

*

* Throw an exception:

*

* Servers: chenhao 2003/4/2

*

============================================================================================================================================================================================================= ============== * / Such a description can make people have a function, one file has a general understanding, which has a great advantage for the readability of the code and the easy maintenance. This is a good start.

2, indent, space, wrap, blank line, alignment

----------------

i) indentation should be done every program, as long as the program is programs, you should know this, but I still have a procedure that does not indent, or the procedure that is indent, if your company also has a write program The programmer who does not indent, please open him without hesitation, and check him with the crime of destroying the source, and he will pay attention to the spiritual loss of people who have read his procedure. Indent, this is an incumbent rule, and I will retrieve it, a indentation is generally a Tab key or 4 spaces. (It is best to use Tab keys)

II) space. Does the space will give the program? No, effective use of spaces can make your program read more and pleasing. Not a pile of expressions are squeezed together. Take a look at the code below:

HA = (HA * 128 * Key )% tabptr-> size;

HA = (HA * 128 * Key )% tabptr-> size;

It feels different from space and no space. In general, the statement is equipped between the individual operations, and when the function is called, it is equipped with each parameter. Such a binding and inclusive:

IF ((HPROC = OpenProcess, false, pid) == null) {

}

IF ((HPROC = OpenProcess, false, pid) == null) {

}

Iii) Retained. Don't write the statement on a line, this is very bad. Such as:

For (i = 0; I '9') && (a [i] <'A' || a [i] >'Z')) Break;

I copy, this is no space, no wrap program is writing? Plus spaces and wraps.

For (i = 0; i

IF ((A [i] <'0' || a [i]> '9') &&

(a [i] <'A' || a [i]>'z'))) {

Break;

}

}

How much? Sometimes, when the function parameters are more, it is best to go, such as:

CreateProcess

NULL,

CMDBUF,

NULL,

NULL,

Binhh,

DWCRTFLAGS,

Envbuf,

NULL,

& sistartinfo,

& prinfo

);

The conditional statement should also be wrap when necessary:

IF (CH> = '0' || CH <= '9' || CH> ='A' || CH <='z' ||

CH> ='A' || CH <='z')

IV) blank line. Don't spoil, the blank line can distinguish between different blocks, blocks, and best to add space lines. Such as:

Handle HPROCESS;

PROCESS_T Procinfo;

/ * Open the process handle * /

IF ((hprocess = openprocess) == null) == null) == NULL

{

RETURN LSE_MISC_SYS;

}

MEMSET (& Procinfo, 0, SIZEOF (Procinfo);

Procinfo.idproc = pid;

Procinfo.hdproc = hprocess;

Procinfo.misc | = mscava_proc;

Return (0);

v) Align. Use the Tab keys to the declaration or comment of your variables, you will make your program look good. Such as:

Typedef struct _pt_man_t_ {

Int numProc; / * Number of processes * /

Int maxProc; / * max number of processes * /

INT NUMEVNT; / * NUMBER OF Events * /

Int maxEvnt; / * max number of events * /

Handle * phndevnt; / * array of events * /

DWORD TIMEOUT; / * TIME OUT INTERVAL * /

Handle hpipe; / * namedpipe * /

TCHAR USR [MAXUSR]; / * User Name of the process * /

INT Nummsg; / * Number of Message * /

INT msg [maxmsg]; / * Space for Intro Process Communicate * /

} Pt_man_t;

how about it? It feels good.

Here mainly tells the story, if you write a pleasing code, the good-looking code will make people feel happy, read the code, not tired, work, neat program code, usually more welcome, more people. Now the hard disk space is so big, don't let your code squeeze together so they will complain that you abuse them. Ok, use "indentation, space, wrap, blank, align" to decorate your code, let them get a row of ordinary organs from there is no order.

3, program comment

--------

Develop a habit of writing procedures, this is the work that each programmer must do. I have seen that thousands of lines, but there is no row of comments. This is like driving on the road, but there is no road sign. How long does it take? I don't know my intention, I have to spend a few times, I understand that this kind of people who waste others and their own time are the most compussia.

Yes, you may say, you will write a comment, really? The writing of the comment can also see a programmer's skill. Generally speaking, you need to write a note at least in these places: The annotation of the file, the comment of the function, the comment of the variable, the comment of the algorithm, the program comment of the function block. Mainly to record what is your program? What is your intent? What is your variable used? and many more. Don't think about it, there are some algorithms that it is difficult or written. It can only be aimed. I admit that there is this situation, but you have to write it, just can train your own expression. The ability to express the technicians of the kind of technicians who are stuffy, you have a high technology, if you express your ability, your technology will not be able to get full. Because this is a team's era.

Ok, say the technical details of a few comments:

i) I don't agree with the row bet ("//") than the block annotation ("/ * * /"). Because some old versions of the C compiler do not support row notes, for your program's portability, please try to use block comments.

II) You may not be nested by the block annotation, then you can use the precompilation to complete this feature. Use the code enclosed in "#IF 0" and "#ndif", will not be compiled, and it can be nested.

4, function [in] [out] parameters

-----------

I often see such procedures:

FuncName (Char * STR)

{

INT LEN = Strlen (STR);

.....

}

Char *

GetUsername (Struct User * Puse)

{

Return Puser-> Name;

}

Do not! Please don't do this.

You should first judge that the pointer to come in is empty. If the pointer passed is empty, then a big system will crash because of this small function. A better technology is to use assertions (Assert), here I don't have much to say these technical details. Of course, if it is in C , reference is much better than the pointer, but you also need to check each parameter.

When writing a function of parameters, the primary work is to perform all the parameters that come in and perform legality checks. The parameters of the outgoing parameters should also be checked. This action should of course be external to the function, that is, after the call is completed, the value it will be checked.

Of course, check will be wasted, but for the entire system, "illegal operation" or "Core Dump" error, how many spends this time is still worth it.

5, judgment on the return to the system call

----------------

Continue to the previous, for some system calls, such as opening files, I often see that many programmers do not do any judgment on FOPEN, they are directly used. Then find that the content of the file is not read, or you can't write it. Still judge:

FP = fopen ("log.txt", "a");

IF (fp == NULL) {

Printf ("Error: Open File Error / N");

Return False;

}

There are still many other, such as: Socket number returned by Socket, Malloc returned by Mall. Please judge what these system calls returned.

6, if statement handles the error

-----------

I saw you said, what is good to say. Still first look at a program code.

IF (CH> = '0' && ch <= '9') {/ * Normal processing code * /

} else {

/ * Output error message * /

Printf ("Error ... / N");

Return (False);

}

This structure is very bad, especially if "normal processing code" is long, it is best not to use ELSE for this situation. Judgment the error first, such as:

IF (CH <'0' || CH> '9') {

/ * Output error message * /

Printf ("Error ... / N");

Return (False);

}

/ * Normal processing code * /

......

Is this structure not very clear? Highlight the wrong condition, let others use your function, you can see unhanectation conditions, so you will be more conscious.

7, #ifndef in header files

------------

Don't ignore the #ifndef in the first part, this is a very critical thing. For example, you have two C files, both C files include the same header file. When compiling, these two C files should be compiled into a run file, so there is a problem, a large number of statements conflicts.

Still put the contents of the header files in #1ndef and #ndif. Whether your header will be referenced by multiple files, you have to add this. The general format is like this:

#ifndef

#define <标>>

......

......

#ENDIF

In theory, it can be freely named, but this "identification" of each header file should be unique. Named rules identified are generally the header file name, and the line is added to the file, and "" in the file name "also becomes underline, such as: stdio.h

#ifndef _STDIO_H_

#define _STDIO_H_

......

#ENDIF

(BTW: More useful features for pre-compilation. Do you use pre-compiled?)

8, allocate memory on the heap

---------

Many people may not understand "Stack Stack" and "Heap" on memory allocation. People who include some barborses are not understanding these two concepts. I don't want to say more about these two things. Simply put, the memory system allocated on the Stack is automatically released, the memory allocated on the HEAP is not released, even if the program exits, the memory is still there. Stack is typically static allocation of memory, and HEAP is generally dynamically allocated.

The memory allocated by the Malloc system is allocated from the heap. The memory allocated from the pile must be released by himself. Use free release, or the term - "Memory Leak" (or "Memory Vulnerability") - Memory Leak. Thus, the system's allocated memory will be less and less Mallo, until the system crashes. Let's take a look at the difference between "stack memory" and "stack memory".

Stack memory allocation

-----

Char *

Allocstrfromstack ()

{

Char PSTR [100];

Return PSTR;

}

Stack memory allocation

-----

Char *

AllocstrFromHeap (int LEN)

{

CHAR * PSTR;

IF (len <= 0) Return NULL;

Return (Char *) Malloc (LEN);

}

For the first function, the internal presence of the PSTR is released by the system when the function returns. So the return of the char * is nothing. For the second function, it is allocated from the heap, so even if the program is exited, it is not released, so the memory returned by the second function is no problem, which can be used. But must call free release, otherwise Memory Leak! The distribution of memory is easy to cause memory leaks, which is the largest "gith" of C / C . If your program is stable, then Do not appear Memory Leak. So, I still have to pay a thousands of people here, be careful when using the Malloc system function (including Calloc, Realloc).

Remember to have a service application on UNIX, there are a few hundred C files compiled, run a good test, etc., the system is Down every three months, and many people can't find the problem. . Only,% 8

转载请注明原文地址:https://www.9cbs.com/read-54244.html

New Post(0)