How to Refactor code in beginners

Source: Internet
Author: User

As long as you want, even beginners can refactor the code.

The first thing to understand is that refactoring is the process of optimizing and improving the code based on how it can be run, and what we want to do is make the code that meets the most basic requirements easier to read, maintainable, and more efficient.

1. Easy to read:

The code we write is for others to read, the finished code you can not be maintained forever, so it is necessary to make your own format code easier to be read by others.

(1) enough to break the line, space, anyway parsing the code will not parse these blank parts, not white, ah, can also show the number of lines of code ^ ^

(2) To achieve this, the simplest is to add enough annotations to explain the function of the class and method, the meaning and format of the parameters of the method, and the logic of the key statements in the method;

(3) Naming method: With the explicit class name, method name, variable name, with the hump type (or other variable format specified by your project group), with $a,$123 such variable name even add a comment is enough.

2. Do not repeat:

(1) The function of the class should not be repeated: if you repeat the instructions you should consider using the inherited class

(2) The function of the method should not be duplicated: even in different classes, the same method of accomplishing the same function is used, and the property of the method is set to public and can be called by another class. If there are many such common methods, it may be necessary to create a special class to store such a method, not in the time of the call to disorderly hands and feet;

(3) Reusing variables: Using a variable frequently in a class, consider creating a global variable to replace it and reduce memory consumption;

3. Use proven models and frameworks:

Popularly said, the pattern is a lot of people in front of the same needs development, found that their Red to make things basically are similar, then why do not create a template, later encountered similar requirements for development, the direct apply is not good.

"In-depth PHP objects, patterns and practices," said the book of such a chestnut (I changed into people are more accustomed to the chestnut):

Suppose there is such a base class lesson represents the new Oriental Curriculum, the lesson class has two sub-categories: Chineselesson class represents the language course, the Englishlesson class expresses the English course, according to the class form divides into the day shift and the late night two kinds, the class lesson and the charge are different;

As a result, 5 classes are established according to the traditional method: Lesson Class-Chineselesson Class-Daychineselesson class () and Nightchineselesson class

-Englishlesson class-Dayenglishlesson class and Nightenglishlesson class

The 4 sub-classes in the bottom include the method of course arrangement lesson () and the method of charging charge ()

If there are more classes or more common methods behind it (which is bound to be the case), the code duplication is more serious and repetition means high coupling, which is a bad phenomenon.

A better solution is to use the strategy model, which simplifies the lesson class-Chineselesson + Englishlesson by eliminating the lowest-level method, creating a policy class strategy class-Daystrategy + Nightstrategy.

Combine the lesson class and the Strategy class to add a new course schedule or charge pattern by creating subclasses of the strategy class, without affecting the lesson class

Common design Patterns: Singleton mode, strategy mode, observer mode, Factory mode ...

Common frame: Yii,thinkphp,ci ...

How to Refactor code in beginners

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.