Illustrated PHP dialogue with the Boss

Source: Internet
Author: User
Tags continue file size include interface php language php file split svn

Mind Mapping

introduce

Call the boss not only because the position is taller than me, also because the technology also lets the human admire!

Today, with the boss to talk about some of our code structure problems, some may be helpful to you. If you have different views, you can bring it up and discuss it together.

Dialogue

1> Single file Large (more than 5000 rows)

Me: Will the file size affect performance? PHP language in the processing of the source file (this is mainly PHP lexical analysis and parsing), will be the source file cut into one of the tags (token). If the file is large, do you want to mark the methods that we don't need at the moment so that it does not significantly affect performance?

Boss: The effect of this on performance is relatively small. We consider the performance of the time, to consider the overall view, such as the Display page, open the page is very slow, then we first consider is not the file size problem, but the loading speed of each module. For example, through your breakpoint settings, you find that a product list reading is relatively slow, it is necessary to consider whether the assembly data is slow, or from the interface (database or middle tier) read data slow? If the assembly data is slow, it is necessary to refactor the algorithm, or to discuss with the product personnel can modify the scheme. If the interface is slow to read data, it is not necessary to add a machine or index to solve the problem. -So, consider the performance problem, do not grasp the small problem, to consider the most impact on the performance of the place to modify.

Me: What's wrong with splitting large file classes into different classes?

Boss: If in a method body, you through many require_once add a lot of class files, then not also affect performance?—— require_once itself also consumes performance!

Draw me a picture (similar to the above picture):

Me: Then I can load the file with include, logic, and load the file conditionally. This will reduce the number of loaded files!

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

Me: For example, I can follow the category to load the file, the movie, I will be the film related to the program files loaded in, television when the TV related program Files loaded in.

Boss: What do you do when TV is going to use the content of the movie in the future? or what do you do when a lot of categories are used in your movie category?

Me: Then I'll put a bunch of "" code (such as if (' movie ' = = = $category ' TV ' = = = $category ' music ' = = = $category) {}). And then I figured, yes, there's a lot of these if statements in one way, and I'm going to look at a bunch of if. It's better to write it in one piece or refactor the code!

2>autoload () method.

Similar to the following code.

<?php

Test::getname ();

function __autoload ($className) {

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

}

Run Result:

We all know that the __autoload () method does not perform well, and generally discourages the use of this method. So, when I call a class, I add this sentence:

Dialogue:

Me: I think the __autoload method is not very good, so I used the include method when I called another module.

Boss: You do, one is that the whole code does not look so normative, and secondly, if you want to modify the framework in the future, we have to look at all such code files, because, for example, your entry file moved to another folder below, then your Test.class.php file in the location, you know?

If we call the __autoload () method, we just need to modify the interface, because all of the class calls go through this method, which is better managed.

3> a method to keep as much as possible within a screen, a line of no more than 80 characters.

  

Me: I think our class inside the method is too long, many more than a few screens, in order to read the current method. I personally prefer to "try to put the method in one screen" and "Let one way do something". Sometimes when you see a long way, you get a big head!

Boss:

One way is to do something, like test (), and do test (). When PHP was not object-oriented, do we often write the code in one file?

We should not "split the method rigidly" in order to dismantle the method. Instead, split the method because the business needs it! ". And function calls, we know, are inherently expensive to perform and memory. If you have some parts of the method that you want to call, then you can make this part of the code a method. If you have a lot of ways to invoke other classes in your method, don't you think it's too much trouble? It's better to write to a method! This is more intuitive.

4> Retrieve the previously deleted code.

Me: If a feature product requirements are removed, but after a long period of time, the product also requires this function. So my original code is deleted, or just comment it!

Boss: Delete it!

Me: Then how do I recover? Do you want to back up the original code?

Boss: You can use version management software to do the recovery. Like SVN.

Example Demo:

(1) Initial code

SVN Submit code:

(2) Product Requirements Code

SVN Submit code:

(3) After a period of time, the product also requires a new on-line module.

SVN operation: Query the log first, then merge for the log


Summary

The above question, I estimate you also met, therefore everybody encourage together under!

Digression: Once I was away from a company that worked for a year! The project manager told me that if you change your job frequently, it will be bad for your future development, but you won't tell me how to do it. Now I understand a little, because I have been to the company a lot of skilled people, are in this company with more than 3 years of people. I find that if you stay in a company for a long time, it is helpful to improve your technology.

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

When we started to enter the company, the company was in urgent need of a shortage of projects. And so on project completion, generally is 1 years or so. If you stay in the company long enough, this project will be more or less the same with you, at this time, you will not stop looking at your own code, you will continue to adjust the code, and constantly refactor your code-with the writing of an article, you keep looking at the article you have written, you will continue to make changes, The more you modify your article, the more it will make you like it.

2 "Business is familiar, can write code faster and better!—— I personally like the" flowing "feeling.

If you stay in a company for a long time, you are very familiar with this field. New requirements, you will soon know how to do the code architecture, such as the above mentioned, you know the method, which parts of the code can be extracted, independent to make a method, you will also know where the future will be frequently modified. --write code, such as flowing like a smooth!



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.