A conversation between PHP and the boss

Source: Internet
Author: User
Mind map introduction is called the boss not only because of a higher position than me, but also because of technology! Today, let's talk to the boss about some of our code structure issues, some of which may be helpful to you. If you have different opinions, you can come up and discuss them together. Dialog... "> <LINKhref =" http://www.php100.com//statics/style/headfl

Mind Map

Introduction

It's not only because my job is higher than me, but also because of technology!

Today, let's talk to the boss about some of our code structure issues, some of which may be helpful to you. If you have different opinions, you can come up and discuss them together.

Dialog

1> a single file is huge (more than 5000 rows)

Me: will the file conference not affect performance? When the PHP language processes the source file (this is mainly the php lexical analysis and syntax analysis), it will split the source file into a tag (token ). If the file is large, we will mark all the methods we don't need Currently. Isn't this a significant impact on performance?

Boss: the performance impact is relatively small. When considering the performance, we need to consider the "preview". for example, when displaying a page, opening the page is slow. The first consideration is not the file size, but the loading speed of each module. For example, if you find that reading a product list is slow through your breakpoint settings, consider whether data assembly is slow or from interfaces (databases or intermediate layers) reading data is slow? If it is slow to assemble data, it is necessary to reconstruct this algorithm, or discuss with the product staff whether to modify the solution. If the interface reads data slowly, do you need to add machines or indexes to solve the problem. -- Therefore, when considering performance issues, we cannot grasp small issues. we need to consider the most influential places for modification.

Me: Is there anything wrong with splitting large file classes to different classes?

Boss: If you add many class files through many require_once in a method body, doesn't it affect performance? -- Require_once also consumes performance!

I drew a picture (similar to the figure above ):

Me: I can use include to logically load files and load files based on conditions. This will reduce the number of files loaded!

Boss: So how do you load according to the conditions?

Me: for example, I can load files by category. when a movie is made, I load the program files related to the movie. when a TV is made, I load the program files related to the TV.

Boss: What do you do when TV will use movie content in the future? Or what do you do when many categories use the content in your movie category?

Me: then I put a bunch of "|" code (for example, if ('Cine '===$ category | 'video' ===$ category | 'music' = = $ category) {}). Later, I thought about it. Indeed, in this case, there will be many such if statements in a method. when I want to correspond to a specific category, I want to see a bunch of if. It's really better to write it in one piece or refactor the code!

 

2> autoload () method.

Similar to the following code.

 

  

Test: getName ();

Function _ autoload ($ className ){

Echo $ className, "\ n"; exit ();

}

 

Running result:

We all know that the performance of The _ autoload () method is not very good. we generally do not encourage you to use this method. Therefore, when I call a class, I add the following sentence:

Dialog:

Me: I think the performance of The _ autoload method is not very good. so when I call other modules, I use the include method.

Boss: you do this. First, the entire code looks less standard. Second, if you want to modify the framework in the future, we need to check all such code files, because, for example, your Portal file is moved to another folder, so your Test. class. where is the php file? do you know?

If we call the _ autoload () method, we only need to modify this interface, because all class calls have passed this method, which is better managed.

 

3> one method should be kept in one screen, with a line containing no more than 80 characters.

  

 

Me: I think the method in our class is too long. many of them have more than a few screens to read the current method. I personally advocate "try to put a method in one screen" and "let a method do one thing ". Sometimes we see a very long method with a big head!

Boss:

One method is to do one thing, such as the test () method (). When php was not object-oriented in the past, do we often write code in a file?

We should not "hard split the method to split the method. But it should be because of business needs to split the method !". In addition, we know that function calls consume both performance and memory. If you want to call some other methods in this method, you can make this part of code into a method. If there are many methods in your method that call other classes, isn't it too troublesome? It's better to write it into a method! This is more intuitive.

 

4> retrieve the previously deleted code.

Me: if a feature is required to be removed, but after a long time, the product requires that the feature be removed. So my original code is to delete it? Just make comments!

Boss: delete it!

Me: How can I restore it? Do you want to back up the original code?

Boss: you can use version management software for restoration. For example, svn.

Example:

(1) initial code

Svn code submission:

(2) product requirements offline code

Svn code submission:

(3) after a period of time, the product needs to re-launch the module.

Svn operation: first query logs and then merge logs.


 

Summary

I guess you have encountered the problem above!

Aside: I used to leave a company that worked for a year! The project manager told me that frequent job hopping would be detrimental to your future development, but did not tell me why? Now I understand a little bit, because many of the technically competent people I 've been to are people who have been in this company for more than three years. I found that if you stay in a company for a long time, it will be very helpful for your technical improvement.

1. constantly refactor the code to improve the quality of your code.

When we started to enter the company, it was generally because the company was in urgent need of a project. Generally, it takes about one year to complete the project. If you stay in the company for a long enough time, this project will be more or less involved with you. at this time, you will constantly read your code and adjust your code, rebuild your code constantly-get a glance at writing articles. you constantly read articles you have written and modify them constantly. the more you modify your articles, the more you like them.

2. familiar with the business, able to write code faster and better! -- I personally like the feeling of "flowing cloud.

If you have been in a company for a long time, you are very familiar with this field. As for new requirements, you will soon know how to implement code architecture. as mentioned above, you will know which code parts in the method can be extracted and made into a method independently; you will also know where to change frequently in the future. -- Write code, like a cloud flow!

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.