Expression: Limit language with good code

zhaozj2021-02-16  41

Expression: Limit language with good code

SpaceSoft [Dark Night Sand]

Programming is essentially an expression. In fact, the process of programming is the process of expressing a work and its completion step using a language to perform the procedure for the machine. That is to say: Software development seems to be a person and machine communication work, just like the hacker Niko dry in black clothes wearing black.

However, the project we have encountered is often too large, too complicated, so we are more in trying to communicate - write documentation, using annotations to describe our design, using charts to represent the structure of the system. That is to say: We have to maintain two sets of descriptions for our ideological maintenance every day: code and documentation. And we must also be careful and ensure they consistent. However, in a relatively large project, the number of documents will sometimes be too terrible, and if the document management of this project is a bit confusing, the document has become a disaster.

When using a large number of documents as the rule of the bar frame, the revolution happened to the revolution. Some people shouted the slogan: "Source code is design". Yes, when documentation and annotation appear in development, they effectively improve the design readability; and when they start to win the maintenance, when engineers are effective working hours, more work is not directly When we produce benefits, we need to start reflection. We don't have to program our daily language because they have bis, they are not accurate, and they are not easy to understand the compiler. So why is our design to use a blurred natural language to make further explanations after using the code description? Maybe there is a problem with our expression?

This article tried to summarize some skills to express design, of course, I still don't plan to fully answer the questions you just proposed. These techniques only include the way the encoding is included, so I call the code "limb language", that is, use code itself to express your design ideas and restrictions.

I. A good name

A good name can clearly describe the meaning of the object, or clearly describe the purpose and method of the operation. This function name inside CType.h is very good:

Int Isalpha (INT C);

Clearly inform the intent of the function. Take your code

(((CH> = 'a') && (ch <= 'z')) || ((CH> = 'a') && (ch <= 'z'))) This code throws away, one What is Isalph to describe the movements you have to do!

The code specification of each development team is often a stricter specification for naming. Naming of variables often uses Hungary rules or similar to it. Hungarian rules use a prefix to bring a simple variable name with more information. For example, a variable such as LPSZFORMAT is familiar with the corresponding coding specification. This is actually a character arrow pointer. So he didn't have to run to the spectrum to see its type, nor need to be built from the context, this variable is CString or a char *. Compliance with code specification also brings the same benefit: I really saved the trouble of finding variable declarations everywhere. When boys often start with two dogs, the girls often called Cuihua, we can often save that Cuihua is a man is a woman.

Of course, get a good name, the most important thing is to choose a good word. Good vocabulary is a very critical thing in expression, which is valid for writing code or writing articles. Because of the mainstream of our development or English programming, it is recommended to name the variable or use Chinese. Int Shibushizifu (int C); this name is really awkward, or can be free from it, I think it is much more difficult than English :)

Second, use language elements to describe restrictions

Programming languages ​​often provide a lot of limitations in order to enhance expression, but we often have no good use. Example of Delphi: Function Myexample (Const NPARAMIN: Integer; var nparamout: integer): Integer;

Such a parameter list is very clearly told the caller: nParamin is input, and a data will be returned in NPARAMOUT.

For example, in C has such a provision: If there is a pure virtual function in a class, this class cannot be instantiated, so you can use this feature to express to the user of the base class: "You must Overload this function" Such a requirement.

The assertion is also a good means of explaining the restriction. For example, we have to implement such a function:

Char * strcpy (char * strdestination, const char * strsource);

We will copy string in the strsource buffer to strDestination, how to express "STRDESTINATION cannot be empty pointer"?

The first thing I thought is to add a sentence if (! StrDestination) return null in the start of the function; however, this is not a good expression. It passes the caller with a conflict information: I have encountered problems. As for the specific problem? I don't tell you. Or may further: store error messages with a global variable method (just like getLastError ()), or use exception. However, this is not a good expression: getLastError and abnormal readers are the ultimate users of the software, not programmers. The more friendly way of programmers is to report the error when compiling or debugging. So we use it as an assertion. An Assert (strDestination); clearly informs the use of this function you need restrictions.

Third, using everyone knows how to understand

When an expression is accepted and familiar with everyone, it is often possible to save a lot of tongues, such as idioms and allusions in natural language, often used to express some of the original big names to speak.

Patterns is a way of expressing this. In my opinion, a model is to summarize a type of problem, as well as the classic solution of these problems, and finally give such a set of things. So when such a way of thinking is recognized by everyone, you can use the model name to refer to some questions and means.

for example. I have an object CService, which provides some services for another object CClient, and two objects communicate locally. Now demand changes, you need to move CMYCLIENT to other machines, but I don't want to rewrite the existing modules. So, I wrote a class to manage CClient's call to CService. How do such design ideas describe? Why do you have a few hundred words on the ocean? No need to! I called this class called CServiceRemoteProxy. Those who are familiar with the Remote Proxy model will understand what I want to do. Of course, the programmer who is unfamiliar with the pattern is troublesome. ^ _ ^

So I think the importance of the programmer is that the model makes the programmer's exchanges more common languages. So understanding the mode and is just as important as in primary school.

Another example is the attitude towards the public library. Many people have untrustworthiness to publicly function libraries, class libraries, and even compilers, or they have been generous, don't think they are not horizontal? So I realize my own version. If it is to optimize or have a bug, it will be, it is said that there is a cow personself made a set from Framework to the compiler. It is completely completed in his own environment, then there is a fire. Into the devil. Such code has no way to communicate, and there is no need to express, and of course, it is not in the discussion category of this article. I think if it is possible, I still try to use public things, everyone is familiar, but it is easier to understand. I am afraid that I am afraid that I have another set of things, the interface is similar, and there are many things and different, and the characteristics are large. In this way, people read such a code, it is inevitable that you feel depressed, it is inevitable to write a document. For example, I don't like String.h's Strcpy, so I realize a version of my own:

Int struct (char * strdestination, const char * strdestination, const char * strsource);

Also give a specified STRCPY successfully returns 1. Failure returns -1. It's finished, this time I didn't get my head, I want to write a comment, who is this code not confused? So I have to write a comment. Why do you say this is?

Finally, I still have to explain, I emphasize the expression of code here, I don't need to need annotations and documents since then, sometimes, a small annotation, the effect of the description is better than anything, then why do we have a little? Notes?

In fact, programming is expressing, exquisite expression can save a lot of tongues, or some of us will rescue us from the ocean of the document. So use the body language of the code :)

Welcome to the author's personal homepage: http://www.mrspace.net/

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

New Post(0)