Home > Article > Web Front-end > How to separate configuration data from code
This time I will show you how to separate the configuration data from the code, and what are the precautions for separating the configuration data from the code. The following is a practical case, let's take a look.
Code is nothing more than defining a set of instructions for the computer to execute. We] often pass data into the computer, operate the data by instructions, and finally produce a result. The problem comes when the data has to be modified. Any time you modify the source code, there is a risk of introducing bugs, and just modifying the values of some data will also bring some unnecessary risks, because the data should not affect the normal operation of the instructions. A well-designed application should separate key data from the main source code, so that we can feel more confident when modifying the source code.
The value that is hardcoded in the application when configuring data, for example:
Magic number
URL
Needs to be displayed to User's string (may be internationalized)
Duplicate values
Settings
Any values that may change
We must always remember , the configuration data can be changed, and you don't want someone to suddenly want to modify the information displayed on the page, causing you to modify the JS source code.
For these configuration data, you can extract them into constants, or mount them into an object, or write them into a configuration file (JSON is recommended in JS), and read the data in the configuration file through a program. In this way, even if the data is modified, your program code will not be changed, reducing the possibility of errors.
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
How to avoid null comparison in web development
The above is the detailed content of How to separate configuration data from code. For more information, please follow other related articles on the PHP Chinese website!