Home Web Front-end JS Tutorial How to separate configuration data from code

How to separate configuration data from code

Jun 04, 2018 am 10:22 AM
code Configuration

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

Jscss basic operation summary

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What to do if the blue screen code 0x0000001 occurs What to do if the blue screen code 0x0000001 occurs Feb 23, 2024 am 08:09 AM

What to do if the blue screen code 0x0000001 occurs

The working principle and configuration method of GDM in Linux system The working principle and configuration method of GDM in Linux system Mar 01, 2024 pm 06:36 PM

The working principle and configuration method of GDM in Linux system

Understand Linux Bashrc: functions, configuration and usage Understand Linux Bashrc: functions, configuration and usage Mar 20, 2024 pm 03:30 PM

Understand Linux Bashrc: functions, configuration and usage

GE universal remote codes program on any device GE universal remote codes program on any device Mar 02, 2024 pm 01:58 PM

GE universal remote codes program on any device

MyBatis Generator configuration parameter interpretation and best practices MyBatis Generator configuration parameter interpretation and best practices Feb 23, 2024 am 09:51 AM

MyBatis Generator configuration parameter interpretation and best practices

How to configure workgroup in win11 system How to configure workgroup in win11 system Feb 22, 2024 pm 09:50 PM

How to configure workgroup in win11 system

Tsinghua University and Zhipu AI open source GLM-4: launching a new revolution in natural language processing Tsinghua University and Zhipu AI open source GLM-4: launching a new revolution in natural language processing Jun 12, 2024 pm 08:38 PM

Tsinghua University and Zhipu AI open source GLM-4: launching a new revolution in natural language processing

Guide you to set up a Maven local repository to speed up project construction Guide you to set up a Maven local repository to speed up project construction Feb 24, 2024 pm 02:12 PM

Guide you to set up a Maven local repository to speed up project construction

See all articles