Six refactoring methods can help you improve code quality by 80%.

Source: Internet
Author: User

Original English:Top 6 refactoring patterns to help you score 80% in code Quality

In the past, I did a lot of code reading and found some common problems in code quality. The following areTop five:

  1. Bloated class:
    The reason why the class is bloated is that developers lack the most basic coding principle, namely the "single Responsibility Principle" (SRP. These classes tend to become very bloated because different methods that lack Association in functions are placed in the same class.

  2. Long Method:
    The main reasons for the method to become long are as follows:

    • Code of many unrelated and functional modules is put in the same method. This is mainly because developers lack the SRP concept.
    • Multiple conditions are placed in the same method, which usually occurs in long methods. This is due to the lack of McCabe Code complexity and the comparison of SRP concepts.
  3. Massive parameter passing:
    I often encounter these situations. Some methods interact with other methods, or a large number of parameters are input when other methods are called. In this case, if you change one of the parameters, you have to change them in multiple methods.

  4. Constant Value is everywhere:
    It is often found that developers (especially new users) use some constants with clear meanings (mainly devil numbers), but do not assign them appropriate constant variables. This reduces code readability and comprehension.

  5. Fuzzy Method Name:
    In many cases, the following method names affect the readability and comprehension of the Code:

    • Fuzzy Method Name that does not have any meaning
    • Technical, but not the names of relevant fields

Six reconstruction methods (methods) to handle the odor of the code above)

The following are six to help youSolve 80% (80-20 principles) of code Quality ProblemsAnd helps you become a better developer.

  1. Extract class/extract method: As mentioned above, it is like a "bloated class" (a class provides functions that should have been provided by several classes) this code odor should move the methods and attributes in the original class to the new classes with appropriate numbers. The methods and attributes of the new class in the old class should be removed. In addition, sometimes some classes are too bloated because they contain Member methods that are used by other classes and should be member methods of other classes. These methods should also be migrated to appropriate classes.
  2. Extraction Method: Code odor like the "too long method" mentioned above can be eliminated by extracting code from the old method to one or more new methods.
  3. Separation condition: in many cases, a method is long because it contains several branch statements (if-else ). These branch conditions can be extracted and moved to several separate methods. This can indeed greatly improve code readability and comprehension.
  4. Introduce parameter objects/retain Global Objects: I found another common situation during code review-several parameters are passed in methods. The problem is mainly caused by adding or removing a method parameter from an existing method. In this scenario, it is recommended that the relevant method parameters form an object (introduce the parameter object), so that the method can pass these objects instead of each individual parameter.
  5. Replace magic numbers with symbolic constants: assign them a naming constant for literal constants that make sense and are used everywhere. This greatly enhances code readability and comprehension.
  6. Rename method: As mentioned above, ambiguous method names will affect the Code's usability. These ambiguous names should be renamed to meaningful names that may be related to business terms to help developers better understand the code through the business context. This requires skill and requires developers and business experts to work together to clarify the business needs that the code needs to meet. Interestingly, This refactoring method seems very easy to understand, but it is often overlooked by many developers, although it is often seen in the refactor menu items of the IDE such as Eclipse.

Http://kb.cnblogs.com/page/199831/

Six refactoring methods can help you increase the code quality by 80% (for conversion)

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.