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.
Interview question
What type of information is explicitly warned against storing directly in a configuration file that is committed to version control?
- a.The specific port number for a network service
- b.Sensitive API keys or database passwordsCorrect
- c.A flag controlling the visibility of a new application feature
- d.The application's current logging verbosity level
Why? this is the answer
The card explicitly states that sensitive secrets like passwords or private keys should never be stored in a plain text config file committed to version control, recommending environment variables or a secrets management service instead. The other options represent appropriate uses for configuration files.
Just read this? Test yourself on what you have been reading.
Read the original → en.wikipedia.org
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on devops — each one lists the topics its interview covers.
See open roles