"Three elements of code" is a major issue, involving not only code quality issues

Source: Internet
Author: User

Under certain conditions, the code should be implemented in an easy-to-understand manner. This topic covers a wide range of content. This article only describes a simple program that has been read in a recent period of time as an example to describe what seems to be related to the "single portal, Single Exit" principle. When talking about this principle, we can also talk about it from different perspectives and different code forms. This article only talks about the forms in the following code model. The following code has three units, each of which has multiple documenttypes of the same type. The Code implements the acquisition of the number of documenttypes of each unit. The Code is as follows:

Import Java. util. arraylist; import Java. util. hashmap; import Java. util. iterator; import Java. util. list; import Java. util. map; public class docsgroup {list 

 

The above program has a lot to be affirmed, for example, it is already very modular, And the next two private methods serve the previous find method, if the content of the last two methods is not merged into the find method, the complexity of the find method will be greatly improved, and the maintenance difficulty will be greatly increased (be clear, every method in this code model actually contains many logical relationships ). If the gof design pattern can be applied, find and searchandmapdata are equivalent to an appearance pattern-a special appearance pattern-which is not only reflected in the Design Pattern of the method, it is also reflected in the invisibility of the searchandmapdata method. For example, the called method is the same and simple, and may actually be different and complex.

Before coding starts, you must analyze the business to be produced, perform business modeling and analysis, and carefully consider the data structure. Writing a program is to do three things-input, processing, and output-which are all related to the data structure. In addition, the analysis of the business is ultimately reflected in the Code as a user input and display information.

According to the above statement, the data structure of the result returned by the above Code is analyzed. There is a list in the map, and each documenttype is stored in this list as a map. In combination with the above business scenarios, the number of documenttypes in the business is displayed. The list contains three times the map of this number. If we look at each unit as a map from another angle, the information to be returned can be put into the map as a DTO and returned. Of course, we can also explore whether the data structure here needs to be so complex, but this article does not discuss which data structure is better, just to elaborate on the business analysis before coding, design the data structure.

Going deep into the code details, we will find that two public variables are defined in the class, which are mainly used to receive the information to be returned. It is this definition method that adds an unconventional input/output operation that makes it difficult to read this very short code. First, the find method cannot clearly tell us where the returned value is obtained. The reason is that although we call a process to obtain the returned result that is worth processing searchandmapdata, its role is not a general process, compared with the function, we will know that this process actually acts as a function, but it is covered by the process (of course, the process can play a considerable role in processing multiple return values, however, we should consider a way that is easier to understand. This topic is another dimension issue and will not be extended here ). In addition, note that these two variables do not need to be public variables at all. In a broad sense, this type of problem is attributed to "single entry, Single Exit". In principle, it cannot be said that this principle is completely correct or necessary, but it does contribute to the clarity and quality of the Code. As mentioned in the encyclopedia, it is to ensure the quality of the development program. It requires that the data flow control in the process must be entered at the entry of a fixed program segment, and the fixed exit is returned. data cannot be used at will in the programming process. This type of problem is caused by the absence of real planning and design of the internal logic, just in the coding ......

Although the above Code seems like the code is not clear because of the unclear single portal and single exit, the essence and the available places are far more than that. This involves business analysis, design analysis, and data model analysis. Simply put, the three elements of "input, processing, and output" must be taken seriously, the three elements involve not only the clarity of the code, but also the quality, toughness, and integrity of the entire business!

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.