In the past few years, I had the opportunity to participate in some interesting projects and independently complete development, upgrades, refactoring, and development of new features.
This article summarizes some key mistakes that PHP programmers often overlook in web development, especially when dealing with medium and large projects. Typical errors include the inability to distinguish between various development environments and the failure to use cache and backup.
The following uses PHP as an example, but its core ideas are applicable to every web programmer.
Application level errors
1. Error reporting is turned off during the development stage
The only thing I want to ask is: Why? Why should you turn off error reporting while developing?
PHP has many levels of error reporting, and we must turn them all on during the development phase.
If you think errors will not happen, then you are idealizing the program. In the real world, errors are inevitable. error_reporting and display_error are two completely different methods. error_reporting () sets the error level, while display_errors sets whether error information should be output.
During the development phase, the error reporting level should be set to the highest level, such as the following settings: error_reporting (E_ALL); and ini_set ('display_errors', true);
Contrary to the previous point, many programmers like to drown errors. You know that errors will happen, but you choose to hide them, and then you can go home early and sleep. Little did they know that more serious mistakes would occur in the future.
3. There is no usage log anywhere in the code
You must keep the usage log in mind at the beginning of software development. You cannot make up for the logging function until the end of the project. . Many programmers will use one method or another to record logs, but few people can actually use logs to record abnormal information. What is the use of a log system that no one checks?
4. No cache is used
In the application system, we can use cache at multiple system levels, such as on the server side, application side and database side. wait. Like logging, caching should be applied to the system from the beginning. You can disable caching during development and enable caching after product release.
5. Abandoning best practices and design patterns
How many people have you seen using their own password encryption algorithms? Sorry to tell you, there are a lot of them because they think they will know better about it.
The best practices and design patterns have been created by predecessors. This is often easier and more effective than reinventing the wheel yourself. We developers only need to be proficient in these design patterns and use them reasonably. Just apply it in the project, such as some encryption algorithms.
6. No automated testing is used
Tests are used in every Web project, just like logs. If no one manages and uses them, so will the tests. Useless.
Running a test project is a tedious task. Fortunately, there are a series of tools to help us achieve automated testing. In PHP development, there is a good testing tool called Jenkins, which is very convenient to use.
7. No code review
Working in a team is a very big challenge, because each member has his own different working habits and methods. Without good specifications, project development will take many detours.
Every member of the team should review each other's code, just like unit tests, which can help the project become cleaner and more consistent.
8. Programming only considers ideal situations
Have you ever encountered problems or even chaos in your own or other people’s code after it was handed over to the customer? Of course I didn't.
This situation often occurs because developers are lazy and only consider ideal situations, which can lead to database crashes, PHP fatal errors, or even servers being hacked. When writing code, programmers must not only consider the best case scenario, but also the worst case scenario. Only by thinking comprehensively can the code cover all situations.
9. Failure to correctly apply object-oriented programming ideas
Most PHP beginners will not use object-oriented ideas in their code, because this concept is It was difficult to understand at the beginning
Of course, the concept of object-oriented is not simply to organize some classes together
Objects, properties, methods, inheritance and encapsulation, etc. These are the most basic concepts in OOP. After developers correctly use the object-oriented design pattern, they will be able to write cleaner and more scalable code.
10. "On-the-fly" programming
Most developers will encounter this situation: "Quick, the customer needs a "New functions must be able to run ASAP", so you add some new functions to the source code, and then upload them directly to the running server. We call this programming method "On-the-fly" programming.
When we develop software, especially medium and large projects, we must analyze, program and release according to the workflow, which will greatly reduce future software bugs. This "flight mode" is not advisable.
Database-level errors
11. Failure to separate database reading and writing
In order to run a complex system for a long time, each Programmers should consider the scalability of the system. 99% of the time, the system does not need to consider expansion because there is not such a large amount of traffic.
Why do we need to separate database reading and writing?
In every system, the database will be the first bottleneck to appear. Under the impact of large traffic, the database is likely to be the first to die. So in most cases we use multiple databases to spread the traffic, and developers often use Master-Slave mode or Master-Master mode. Master-Slave is the most popular database pressure sharing mode. It will route the specified select statement to each Slave server, which will reduce the pressure on the Master server a lot.
12. The code can only connect to one database
This is very similar to the previous error, but developers sometimes need to connect to multiple databases for some reasons. , for example, you will put high-load data such as user logs, activity information streams, and real-time data analysis into different databases to relieve the pressure on the main database.
13. Failure to detect database vulnerabilities
If you do not detect vulnerabilities in the database, it is equivalent to opening the server's door to most hackers.
Among the many vulnerabilities, database vulnerabilities are the most vulnerable, and the most common one is SQL injection. Therefore, it is still necessary to conduct database vulnerability detection regularly.
14. Data tables do not have indexes
Indexes play a very important role in data tables. Appropriate indexes can improve the performance of each table. Here is an article The article tells how to create an index and when to create an index.
15. No transaction mechanism is used
Data integrity is very important to the Web system. If an error occurs in data consistency, the entire system will collapse and be difficult to repair. Proper use of the database transaction mechanism will effectively solve this problem. For example, if you want to save user data, there are e-mail, username and password in table1, and first name, last name, and gender age in table2. We can use transactions to ensure that the data is updated at the same time or not at the same time when updating two tables.
16. Sensitive data is not encrypted
PHP5.5 provides a hash encryption method, which is used as follows: span>
$hash = password_hash ( $password, PASSWORD_BCRYPT );
17. No backup
Did you see the picture below? If you encounter such a situation and you don’t have a backup, everything will be over.
18. No monitoring
Without monitoring, you will not know what will happen next What happens? For monitoring, you should pay attention to the following questions:
•How many people can directly access this application service?
•Is the server running under high load?
•Do we need to expand the system with another database server?
•Where are the failure points of the application system?
•Is the system currently offline?