6 reconstruction method can help you improve code quality 80%

  1. In the past done a lot of commuting the code, the code quality found some relatively common problem, the following are among the top five :
  2. Big classes : Class reason bloated, because the developers lack the most basic coding principles, namely understanding "single responsibility principle" (SRP) of. These classes tends to be very cumbersome, due to the different methods and the lack of correlation functions are placed in the same class inside.
  3. Long methods : Method reason become very long mainly for the following reasons:
    1. Many of no relevance, the complex function code module placed in the same manner. This is mainly developers lack the concept of the SRP.
    2. A variety of conditions are placed within the same method, which often happens in a long process. This is due to the lack McCabe code comparison concept and complexity of the SRP.
  4. A large number of mass participation : I often encounter these types of situations, some of the ways to interact with other methods, or call some other method of passing a large number of parameters. This will occur if one of the parameters change, you have to make changes in multiple methods.
  5. Constant value everywhere : often find developers (especially beginners) will use some constant value with a clear meaning (mainly the magic number), but without assigning them to the appropriate constant variable. This will reduce the readability and understandability of code.
  6. Fuzzy method names : Many times, take the following method name will affect the readability and understandability of code:
    1. Fuzzy method name does not have any meaning
    2. Technical, but did not mention the name of related fields
 
6 treatments above code smell reconstruction method (approach) The following is a 6 can be used to help you solve 80% (80-20 principle) code quality issues reconstruction method, and can help you become a better developer By.
  1. Extracting class / disengagement Method : As mentioned above, like "bloated class" (a class that provides this type has several functions provided) This code smell should the original class methods and properties to move suitable number to the new class. Old corresponding class attributes of the new class and method should be removed. In addition, some of the classes sometimes overly bloated because it contains by other classes using this method should be a member of the other members of the class methods. These methods should also be moved to the appropriate class.
  2. Extraction Method : as "long way" The above-mentioned code smell can be eliminated by extracting the code from the old process to one or more of the novel process.
  3. Separation conditions : Many times, a method is long because it contains several branching statements (if-else). The branch condition may be extracted and moved to several separate methods. This indeed can greatly improve code readability and understandability.
  4. Introduce Parameter Object / retention global object : find another very common situation when I was doing code review - Several parameters are passed to the method. The main problem with the need to add or remove a parameter from a method related to the existing methods. In this scenario, the proposed method parameters related to an object consisting of (incorporated parameter object), so that these passing objects a method rather than each individual parameter.
  5. Replace the magic number with a symbolic constant : for meaningful and literal constants to be used everywhere should be allocated a named constant for them. This can greatly enhance code readability and understandability.
  6. Renaming : As mentioned above, the method name ambiguity affects the usability of the code. These ambiguous names should rename to meaningful names may be associated with the business terms, to help developers better understand the business context through the code. This can be tricky and requires developers and business experts collaborate together to sort out the code required to meet business needs. Interestingly, this reconstruction method seems very easy to understand, but often overlooked by many developers, although this one often appear in the Eclipse IDE, refactor this menu item.

Reproduced in: https: //my.oschina.net/wzlee/blog/262198

Guess you like

Origin blog.csdn.net/weixin_33725515/article/details/91716717