Yii-how to make self-written code easy to maintain?

Source: Internet
Author: User
The code is easy to maintain and can be divided into two aspects: easy to read and understand; easy to modify and expand.

The code is easy to maintain and can be divided into two aspects: easy to read and understand; easy to modify and expand.

I. how to write code that is easy to read and understand
  1. The most fundamental one is to learn from writing articles, including directories, outlines, titles, paragraphs, and appropriate prompts.

    1.1 First is the directory structure. in some good practices, there are conventions, such as Rails applications. The app directory must be divided into four directories: controllers, models, views, and helpers. Add config, lib, and vender. you don't have to guess where the code is. The more common the project types are, the more conventional the project directory structure should be built. For projects that do not have any reference in the industry, the directory structure should adopt words that are simple, easy to understand, and have fixed meanings, such as core, config, and test.
    1.2 package name and file name. in this regard, the java Language Specification is worthy of reference and reference from other languages. it is organized in layers and named properly. Is the most important.
    1.3 is there any comment in a source code file? In my opinion, try not to write an article as much as you can. For example, file name, class name, method/function name. If you fold all the actual code and read these names in sequence, can you give readers a rough understanding of the content and purpose of this source file? Once again, there are many functions/methods in a source program file. The order of these methods makes sense. Only adjust the order, such as constructor, extended constructor, simple read/write function, and business-related function. In this order, it is easier to read. Do not write too much or describe the key points in a concise manner where you must write comments. use 1.2.3 to clarify the points.
    1.4 variable name, constant name, we must repeatedly emphasize the importance of naming. it can be said that naming is a top priority in software development. It should be concise, clear, and full-English (it is decided not to use Chinese pinyin or any abbreviations) and should not contain numbers as much as possible. variable names such as var1 and var2 are the worst.

  2. Readme. during the project development process, regularly sort out a readme file and put it in the root directory of the project. it mainly contains two parts: what did our code do? How to find the code we wrote.

  3. Wiki. it is very easy for a team to develop and maintain a wiki as much as possible. You can also create an integrated project management tool such as redmine. Management and Maintenance of wiki is a big topic, and it will not be discussed here.

  4. Unit test. @ Li Nan and @ KevinWei have already been mentioned. This method makes it easy to read and understand the code and modify the code. Very important.

  5. Code Review. @ KevinWei has also been mentioned.

II. how to write code that is easy to rewrite and expand
  1. For unit testing, it is best to use TDD (test-driven development) throughout the entire process so that you can confidently modify your code.

  2. Let's talk about the mature practices and design patterns in the industry. In pursuit of scalability, reusability, or even just to play with the design model, a project will become a victim of over-design and never overhead.

  3. Regular refactoring. it is very dangerous to move closer to the design pattern as soon as possible. it is better to refer to the design pattern for reconstruction. However, most of the time, we don't have time to refactor... Therefore, TDD is the most practical. according to the TDD working model, your project is reconstructed almost every day.

  4. Pair programming. this @ Li Nan has already mentioned. It is important to make knowledge more than just one person in the team.

This is probably the case...

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.