Separate Input/Output From Computation
A lot of computer code follows the pattern handed down to us from the ancients:
- Get some data from somewhere.
- Do some computations from the data to produce new data. Equivalently, transform the data.
- Put the new data somewhere.
Separate the code that does Input or Output into its own modules. Do not make an I/O call in the middle of some code that is doing a computation.
An Input code module deals with things like reading a configuration file, reading environment variables from the operating system, loading data from a file, et cetera.
An Output code module will typically handle writing to a file or database, or transferring data through a network call.
Rules of thumb for readable code:
- Try to put the calls to the Input module at the beginning of the program.
- Try storing the input data in some kind of State module, for later use.
- Try to put he calls to the Output module at the end of the program.
Input and Output are major dependencies of the program. For example, a program may depend on the existence of a config file as input. Now suppose the config variables are not needed until half way through the program. An 'efficient' programmer might delay reading the config file until the point at which it is needed. However, it is better to read the config file at the very beginning of the program, because:
- It makes the dependency on the existence of the config file immediately explicit to the reader.
- If the file is missing, the program will error immediately, instead of wasting time on a computation that will ultimately fail.
Make dependencies explicit. Declare them early; your readers will want to know about them. Fail early. Make sure you have everything that you need before starting the job.
🙠