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

Source: Internet
Author: User
Top 6 refactoring patterns to help you score 80% in code Quality

Posted by ajitesh Kumar/in code review, software quality/January 31,201 4

Have done several code reviews in past and found followingTop 5 code smellsCommon Cause SS most of these Code having code quality issues:

  1. Large Class: The classes were found larger enough due to lack of developers' understanding on one of the primary coding principles which is "single responsibility principle" (SRP ). these classes use to get larger due to various methods serving unrelated functionality in the same class.
  2. Long Method: The methods have been found longer due to several reasons such as following:
    1. Several Block of Code serving unrelated/multiple functionality within the same method. This is primarily due to lack of SRP concepts with the developers.
    2. Several conditionals within the same method. this is found very often within the method which are long enough. this may be attributed to the lack of knowledge on McCabe Code complexity vis-a-vis SRP concepts.
  3. Several method parameters: Then, there are several instances I Came authentication SS where it was seen that method is passing several parameters to each other to interact/call each other. and, a change to one of the parameters in the parameter list use to change several method signature.
  4. Literal constants used everywhere: Several times, it is seen that developers (primarily newbies) use the literal constants (mostly numbers) with definite meanings using them as it is, without assigning a proper named constant variable it. this degrades the Code read-ability and understand-ability.
  5. Vague method names: Always A times, method names have been found to look like following which impacts on Code read-ability and understand-ability:
    1. Vague names without any meaning
    2. Technical names without any reference to problem domain related names.

 

Top 6 refactoring patterns to deal with abve code smellsFollowing are top 6 refactoring patterns which cocould help you Resolve 80% (remember 80-20 rule) of code quality issuesAnd become a better developer:
  1. Extract class/move method: As mentioned above, code smell such as large class serving functionality that shocould be done by two or more classes shocould be split/extracted into appropriate number of classes. in these new classes, the appropriate fields and methods from the old classes shoshould be moved. additionally, the classes are also found to be long at times because of presence of methods which serves functionality which are used by other class and tends to be the member method of that class. such methods shoshould also be moved to such type of appropriate class.
  2. Extract Method: Code smell such as long method as mentioned above cocould be dealt by extracting code from old method to one or more new methods.
  3. Decompose conditional: returns a times, method found to be long is seen to consist of several conditional statements (if-else ). these conditionals shocould be extracted and moved into separate methods. this does increase the Code read-ability & understand-ability to a great extent.
  4. Introduce parameter object/preserve whole object: several parameters passed to methods has been one another common observations I have come into processing SS while doing code review. the problem has been primarily related to change in code if there has been a need to add or remove one of the method parameters from the involved methods. in this scenario, it is suggested to group related parameters as object (introduce parameter object) and let Methods pass these objects rather than each of the parameters.
  5. Replace magic number with symbolic constants: for literal constants that have meanings and that are used everywhere, shocould be assigned a named constant variable. this enhances the Code read-ability and understand-ability to a great extent.
  6. Rename method: As mentioned above under the Code smells, the vague method names impacts the usability of the Code. these vague names shoshould rather be given meaningful names which may relate to business domain related terminologies and help developer understand the code in the business context. this is quite a skill and requires developers to collaborate with business analysts to properly understand the business requirements that is met with the code. interestingly, This refactoring pattern looks pretty simple to understand, but however, gets ignored quite often by includevelopers given the fact that its mention is made in ides such as Eclipse under refactor menu link.
  • About
  •  
  • Latest posts
Ajitesh Kumar ajitesh is passionate about various different technologies including programming versions such as Java/Jee, JavaScript, PHP ,. net, C/C ++, mobile programming versions ages etc and, computing fundamentals such as application security, cloud computing, API, mobile apps, Google Glass, big data etc.

Follow him on Twitter and Google +. powered by starboxrelated posts:
  1. Refactoring 3000 lines of code
  2. Top 5 code smells newbies developers cocould easily identify & Avoid
  3. PHP code smells and best practices
  4. Stay single and get admired for your code
 

Tags:Refactoring

1 comment
  1. Top 5 code smells newbies developers cocould easily identify & Avoid | software developer cravingsFebruary 1, 2014 at AM

    [...] May want to check the refactoring patterns that cocould Help You With tips to avoid these smells and write neat [...]

    Reply
  2. EyalFebruary 2, 2014 at 2: 53 AM

    Hi,
    Nicely written blog!
    I just wrote a blog about the single responsibility principle.
    I talk there on some of the stuff you mention.
    Wocould appreciate your comments

    Http://eyalgo.com/2014/02/01/the-single-responsibility-principle/

    Thanks

    Reply
  3. The 7 Habits of highly valid developers-software developer cravingsFebruary 11,201 4 at AM

    […] Very well about code smells and varous different code refactoring patterns which deals with the coding […]

    Reply
  4. The 7 Habits of highly valid developersFebruary 11,201 4 at pm

    […] Very well about code smells and varous different code refactoring patterns which deals with the coding […]

    Reply
  5. Six refactoring methods can help you improve code quality by 80%-isureJune 26,201 4 at pm

    […] Kumar is a widely used software engineer and has great enthusiasm for many technical fields, such as Java/Jee, PHP, and ,. net, C/C ++, and other programming languages, mobile programming languages, application security, cloud computing, API, mobile applications, Google Glass, and big data. The Twitter account is @ eajitesh. Recently, Kumar wrote an article about the common bad taste of code and six reconstruction modes to improve code quality, the application scenarios of each reconstruction mode are discussed and discussed in detail.

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

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.