Programming and cultivation (four)

zhaozj2021-02-08  323

11, the process of error information --------- Do you handle error information? Oh, it is not a simple output. Look into the example below:

IF (p == null) {Printf ("Err: The Pointer Is Null / N);} Say goodbye to the programming of the student era. This programming is very disadvantageous for maintenance and management, error information, or prompt information, should be unified, not like the above, write into a "hard coded". Article 10 describes a part of this in this regard. If you want to manage the error message, you have the following processing:

/ * Declaration error code * / #define ERR_NO_ERROR 0 / * No error * / #define ERR_OPEN_FILE 1 / * Open file error * / #define ERR_SEND_MESG 2 / * sending a message error * / #define ERR_BAD_ARGS 3 / * Bad arguments * / #define ERR_MEM_NONE 4 / * Memeroy is not enough * / #define ERR_SERV_DOWN 5 / * Service down try later * / #define ERR_UNKNOW_INFO 6 / * Unknow information * / #define ERR_SOCKET_ERR 7 / * Socket operation failed * / #define ERR_PERMISSION 8 / * Permission denied * / #define err_bad_format 9 / * Bad configuration file * / #define err_time_out 10 / * Communication Time Out * / / * declaration error message * / char * errmsg [] = {/ * 0 * / "no error" , / * 1 * / "Open file error", / * 2 * / "failed in sending / receiving a message", / * 3 * / "bad arguments", / * 4 * / "memeroy IS not enough ", / * 5 * /" service is down; try latter ", / * 6 * /" unknow information ", / * 7 * /" a socket Operation Has Failed ", / * 8 * /" Permission Denied ", / * 9 * /" Bad Configuration File Format ", / * 10 * /" Communication Time Out ",}; / * Declaration Error Code Global Variable * / long errno = 0; / * Print error information function * / void PERROR (CHAR * INFO) {IF (info) {Printf ("% s:% s / n", info, errmsg [errno]); return;} printf ("error:% s / n", errmsg [errno] }

This is basically ansi error handling detail, so you can do this when you have an error in your program: BOOL Checkpermission (CHAR * UserName) {IF (Strcpy (UserName, "Root")! = 0) {Errno = Err_Permission_Denied; return (false);} ...} main () {... if (! Checkpermission ("") {PERROR ("main ()");} ...} A common, there is a personality Error message processing, doing advantageous as an error, unifying user interface, without failure, A programmer out, and a programmer has an information. And do this, it is very easy to maintain. The code is also easy to read.

Of course, the object must be reversed, nor is therefore necessary to put all the outputs in Errmsg, extracting more important error messages or prompting information is the key, but even though, this also includes most of the information.

12. Common functions and cyclic statements in the calculated quantity ---------------- Look at the following example:

For (i = 0; i <1000; i ) {getLocalHostName (HostName); ...} getLocalHostname means that it will get the current computer name, in the cyclic body, it will be called 1000 times. How much do this have no efficiency. This function should be taken to the cyclic body, which is only called once, and the efficiency has been greatly improved. Although our compiler will optimize, we will get the unchanged thing in the cycle, but you believe that all compilers will know which constant? I think the compiler is unreliable. It is best to do it yourself.

Similarly, the invariance in common functions, such as:

GetLocalHostName (char * name) {char funcname [] = "getLocalHostName"; sys_log ("% s begin ...", funcname); ... sys_log ("% s end ...", FuncName);

If this is a function that is often called, you have to allocate memory every time you call, this overhead is very big. State this variable into static, when the function is called again, it will save the overhead of the distribution of memory, and the execution efficiency is also very good.

13, function name and variable name ------------ I see many programs to name the variable name and function name, especially the variable name, what A, B, C, AA, BB, CC, what FLAG1, FLAG2, CNT1, CNT2, which are also a behavior without "cultivation". Even good comments. A good variable name or a function name, I think there should be the following rules: 1) intuitively and can be spent, and hope that "decoding" will not be "decoded". 2) The length of the name should be the shortest length, but also to maximize its meaning. 3) Don't write all over, don't lower your lowercase, you should write, such as: getLocalHostName or useeraccount. 4) You can be short-handed, but you have to understand, such as: ErrorCode -> Errcode, ServerListener -> Servlisner, Useraccount -> USRACCT, etc. 5) In order to avoid the full bureau function and variable name conflict, you can add some prefixes, usually the module is referred to as a prefix. 6) The global variable is unified to add a prefix or a suffix, let people see this variable knows that it is global. 7) Name the function parameters in Hungarian nomenclature, local variables. But still have to adhere to the principle of "Wangwen Business". 8) Maintain consistent with standard libraries (such as: STL) or development libraries (such as: MFC). 14, the pass value of the function and the pointer ------------ When the function is transferred, when it is incorporated into a non-const, it means that the pointer to modify this pointer in the function. Refers to data in memory. If it is a pass value, then how to modify this value inside the function, it also affects the value that is not passed, because the pass value is only the memory copy.

what? You said this feature, you understand, ok, let's take a look at the routine below:

Voidgetversion (char * pstr) {pstr = malloc (10); strcpy (PSTR, "2.0");}

Main () {char * ver = null; getVersion (Ver); ... ... free (ver);}

I promise that the problem like this is the most important mistake of a novice. The program is in vain through the function getversion to the pointer VER VER VER, but this method does not work at all, the reason is - this is the pass value, not a pointer. You may argue with me, when I divide the pointer? Take a closer look, in fact, what you are passing is that the pointer is actually in the value.

15, modify the cultivation of others -----------

When you maintain the procedure of others, please don't delete or modify the existing program very residual. I often see that some programmers modify the expression or statement directly on others. When modifying others, please do not delete someone else's programs. If you think that others' programs are not correct, please comment, then add your own handlers, you can't know 100% know the intentions of others, so You can recover, please do not rely on CVS or SourceSafe this version control software, or you want to see you the intentions and steps you modify on the source code. This is when the program maintenance, a cultivated programmer should do.

As shown below, this is a better modification:

/ * * ----- Comment by Haoel 2003/04/12 ------ * * char * p = (char *) malloc (10); * MEMSET (p, 0, 10); * / / * ------ Added by Haoel 2003/04/12 ----- * / char * p = (char *) Calloc (10, SizeOf char); / * ---------- ----------------------------- * / ... Of course, this method is used in software maintenance, so Methods, people who can make repatriator easily know the action and intentions of the previous code changes, and this is also a respect for the original author.

Modify someone else's programs in the "Note - Add" manner, it is better to delete the procedures directly.

<- Previous Next->

(All rights reserved, please indicate the source and author information when reproduced)

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

New Post(0)