Bad taste of code [1]. Taste of code

Source: Internet
Author: User

Bad taste of code [1]. Taste of code

Total returned directory

I. Duplicated Code (repeated Code)

We should follow the DRY principle when writing code: Don't Repeat Yourself. If you see the same program structure in more than one location, you can be sure: try to combine them into one, and the program will become better.

1. Two Functions of the same class contain the same expression.

At this time, you need to extract the repeated code into a separate function, and then let the two functions call it.

2. Two sibling child classes contain the same expression

To avoid this situation, you only need to extract the duplicate code of the two classes, and then push the extracted code into the base class.

3. Irrelevant classes contain the same code

You should extract the repeated code into an independent class and then call this new class in the two classes.

The same expression only appears in one method of a class for other methods to call.

Ii. Long Method (Long function)

The first rule of the function isShortThe second rule is:It must be shorter.This asserted cannot be proved. No research results show that small functions are better.

However, objects with short functions usually have better and longer life.

How long is the function supposed to be? Let's take a look at the article by the author of "The clean code:

We seem a little away from the topic. Well, let's talk about how to extract it from long functions?

1. Search for comments

If there is a line of comment in front of the code, you can replace the code with a function, and you can name the function based on the comment.

2. conditional expressions and loops

These two functions can be extracted into independent functions.

Each method only does one thing. The method must be well defined and named.

Iii. Large Class (oversized Class)

1. There are too many instance variables in a class

If you want to use a single class to do too many things, there will usually be too many instance variables in it. Once so, repeated code will come soon.

Several variables can be extracted to the new class. When refining, you should select variables related to each other in the class and put them together. Generally, if several variables in the class have the same prefix or suffix, this means they have the opportunity to extract them into a certain component.

2. There is too much code in a class

Some fields and methods are only used by some instances, so these fields and methods are moved to the subclass.

Each class is responsible for a group of internally associated tasks.

 To Be Continued...

 

This sharing ends now. If this article is helpful to you, please comment or recommend it!

Author: NaYoung
Source: http://www.cnblogs.com/liuyoung/p/7819969.html
You are welcome to repost it, but you must keep the complete article for any repost. The original Article and the original article link are displayed in the prominent area. If you have any questions or authorization negotiation, please leave a message for me

 

Related Article

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.