Configuration Files: Separating Code from Settings
A config file separates a program's behavior from its code, allowing you to change settings like database URLs without recompiling. They're used for API keys, feature flags, and environment-specific values. The footgun is committing secrets to version control.
WHY IT EXISTS: Configuration files solve the problem of hardcoding. If you write a database password directly in your source code, you must change the code and re-release the entire application just to update it. This also prevents you from sharing your code publicly without exposing secrets. Config files decouple these volatile settings from the stable application logic.
THE MENTAL MODEL: Think of a config file as the software's control panel, but in text form. Your application's code is the engine and chassis—it's fixed. The config file is the dashboard where you can flip switches (enable features), tune settings (connect to a different database), or define endpoints. You can change the dashboard settings without rebuilding the entire car.
HOW IT WORKS: At startup, an application reads one or more configuration files from the file system. It parses the contents—which can be in formats like JSON, YAML, TOML, or simple key-value pairs—and loads the settings into memory. The application's code then references these settings to make decisions, like which database to connect to or what port to listen on. For example, a web server might read port = 8080 and start listening for traffic on that port.
WHEN TO USE IT: Use config files for any value that might change between environments (development, testing, production) or over time. This includes database connection details, API keys for third-party services, feature flags, logging levels (e.g., DEBUG vs. INFO), and server settings like ports or thread pool sizes.
WHEN NOT TO USE IT: Avoid config files for values that are truly constant and integral to the application's logic, like mathematical constants. Most importantly, never store sensitive secrets like passwords or private keys in a plain text config file that gets committed to version control. Use environment variables or a secrets management service for those.
ONE CANONICAL EXAMPLE: A common example is a .env file for a web application. It might contain lines like DATABASE_URL="postgres://user:pass@host/db" and API_KEY="abc123xyz". The application, using a library like dotenv, reads this file and makes these values available to the code. This allows a developer to run the app with a local database, while the same code in production reads different values to connect to the production database.
Read the original → en.wikipedia.org
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.