1.1 How to improve code quality
We put aside any specific technology to talk about how to improve the quality of the code. How to improve the quality of the code, I believe that not only the people here are distressed, but also all the software project distress. How to improve the quality of the code, I think we should first understand what is high-quality code.
three elements of high-quality code
We evaluate the three elements of high-quality code: Readability, maintainability, and change. Our code must not be less than the requirements of these three elements to calculate the high-quality code. 1.1.1 Readability
The mention of readability seems to be a bit of a cliché, but it's frustrating to say that while we're again to readability, our code is still doing very poorly in terms of readability. Because of the need for work, I often need to read someone else's code and maintain the modules that others have designed. Every time I see a large paragraph, dense code, and no comments are often feeling unceasingly, deeply realized the importance of this work. Because of the need of division of labor, the code we write inevitably needs others to read and maintain. And for many programmers, they seldom read and maintain code for others. Because of this, they pay little attention to the readability of the code, and to how to improve the readability of the code. Sometimes even if you write a comment for the code, it is often the annotation language is obscure and difficult to understand the same book, so that the reader is still unclear about the meaning of discretion. In response to the above questions, I give you the following suggestions: 1.1.1.1 do not write large sections of code
If you have the experience of reading someone else's code, when you see a large paragraph of code written by someone else, and don't have a comment, how do you feel, is not "Om" a big head. A variety of functions entangled in a method, a variety of variables called back and forth, I believe that no one will think it is a high-quality code, but often appear in the program we write. If you look again at the code you've written, you'll find that with a little bit of complexity, hundreds of lines of code go out. Some of the better way is to segment. The large section of the code through the collation, divided into the function of a relatively independent paragraph after paragraph, and at the front end of each paragraph to write a note. This is a lot more than the messy code, but they are still unsatisfactory in terms of functional independence, reusability, and maintainability. From another comparative professional evaluation standard, it does not realize the low coupling, high cohesion. My advice to you is to encapsulate these relatively separate paragraphs into one function after another.
Many masters in their classic books encourage us to develop the habit of refactoring in the process of writing code . In the process of writing code, we often write complex functions, initially written in a function of a class. With the gradual expansion of functions, we began to summarize the complex functions, sorting out one after another independent function. These standalone functions have input and output data that communicates with other functions. When we analyze this, we will be very natural to separate these functions from the original function, to form a separate function for the original function call. When writing these functions, we should think about them, give them an interpretation name, and write comments for them (which will be discussed in more detail later). Another question to think about is where these functions should be placed. These functions may be placed in the original class or in classes of other corresponding duties, and should be guided by the principle of "responsible design" (which is described in detail later).
Here is a class that I wrote to read data from an XML file and build it into a factory. The most important part of this class is the initial chemical plant, which is summed up in three parts: trying to read files in various ways, parsing XML data streams in the DOM, and generating factories. And these functions were summed up and encapsulated in a different function, and they took the name of the interpretation and wrote a comment:
Java code
/**
* Initial chemical plant. Reads the XML file from the path and loads the data from the XML file into the factory
* @param path of path XML
*/
public void Initfactory (String path) {
if (Findonlyonefilebyclasspath (path)) {return;}
if (Findresourcesbyurl (path)) {return;}
if (findresourcesbyfile (path)) {return;}
this.paths = new String[]{path};
}
/**
* Initial chemical plant. Reads the XML file from the path list, loading the data in the XML file into the factory
* @param paths Path list
*/
public void Initfactory (string[] paths) {
for (int i=0; i<paths.length; i++) {
Initfactory (Paths[i]);
}
This.paths = paths;
}
/**
* Re-initial chemical plant, initialize the required parameters for the last initial chemical plant used in the parameters.
*/
public void Reloadfactory () {
Initfactory (this.paths);
}
/**
* Use ClassLoader to try to find a file and call <code>readxmlstream () </code> parse
* @param path to the path XML file
* @return is successful
*/
Protected Boolean Findonlyonefilebyclasspath (String path) {
Boolean success = false;
try {
Resource Resource = new Classpathresource (path, This.getclass ());
Resource.setfilter (This.getfilter ());
InputStream is = Resource.getinputstream ();
if (Is==null) {return false;}
Readxmlstream (IS);
Success = true;
} catch (Saxexception e) {
Log.debug ("Error when Findonlyonefilebyclasspath:" +path,e);
} catch (IOException e) {
Log.debug ("Error when Findonlyonefilebyclasspath:" +path,e);
} catch (Parserconfigurationexception e) {
Log.debug ("Error when Findonlyonefilebyclasspath:" +path,e);
}
return success;
}
/**
* URL-based attempt to find all XML files in a directory and call <code>readxmlstream () </code> parse
* @param path to the path XML file
* @return is successful
*/
Protected Boolean Findresourcesbyurl (String path) {
Boolean success = false;
try {
ResourcePath ResourcePath = new Pathmatchresource (path, This.getclass ());
Resourcepath.setfilter (This.getfilter ());
resource[] Loaders = resourcepath.getresources ();
for (int i=0; i<loaders.length; i++) {
InputStream is = Loaders[i].getinputstream ();
if (is!=null) {
Readxmlstream (IS);
Success = true;
}
}
} catch (Saxexception e) {
Log.debug ("Error when Findresourcesbyurl:" +path,e);
} catch (IOException e) {
Log.debug ("Error when Findresourcesbyurl:" +path,e);
} catch (Parserconfigurationexception e) {
Log.debug ("Error when Findresourcesbyurl:" +path,e);
}
return success;
}
/**
* Try to find files in file mode and call <code>readxmlstream () </code> parse
* @param path to the path XML file
* @return is successful
*/
Protected Boolean Findresourcesbyfile (String path) {
Boolean success = false;
Fileresource loader = new Fileresource (new File);
Loader.setfilter (This.getfilter ());
try {
resource[] Loaders = loader.getresources ();
if (Loaders==null) {return false;}
for (int i=0; i<loaders.length; i++) {
InputStream is = Loaders[i].getinputstream ();
if (is!=null) {
Readxmlstream (IS);
Success = true;
}
}
} catch (IOException e) {
Log.debug ("Error when Findresourcesbyfile:" +path,e);
} catch (Saxexception e) {
Log.debug ("Error when Findresourcesbyfile:" +path,e);
} catch (Parserconfigurationexception e) {
Log.debug ("Error when Findresourcesbyfile:" +path,e);
}
return success;
}
/**
* Read and parse an XML file input stream to get the root of the XML in the form of element,
* Then call <code>buildfactory (Element) </code> build Factory
* @param inputstream file input stream
* @throws saxexception
* @throws IOException
* @throws parserconfigurationexception
*/
protected void Readxmlstream (InputStream inputstream) throws Saxexception, IOException, parserconfigurationexception{
if (inputstream==null) {
throw new Parserconfigurationexception ("Cann ' t parse source because of InputStream is null!");
}
Documentbuilderfactory factory = Documentbuilderfactory.newinstance ();
Factory.setvalidating (This.isvalidating ());