An algorithm is a set of instructions that an application uses to determine what to do.
The instructions should never change and, for any given input, should always result in the same outcome for that input.
An application should be able to follow an algorithm regardless of its current state, and end in the correct state, according to the algorithm.
A typical algorithm should exist as a series of steps, followed in sequence. Each step may contain a condition which determines how to proceed. Certain conditions may force an immediate end to the procedure, or to skip certain steps.
An example of a simple algorithm might be:
- Go to step 2.
- Go to step 3.
- End
A more complex algorithm might be:
- Get data from local storage
- If no data in local storage, or data is stale:
- Fetch new data
- Store data in local storage
- Display data
It is important that an algorithm be completable for any given input, otherwise it may cause an error in the application. Such an occurence could be caused by an infinite loop such as:
- Go to step 2.
- Go to step 1.
To avoid such situations, algorithms should be thoroughly tested, with all possible inputs. Any input that may cause an issue with an algorithm, such as incompatible data types, should be prevented from being entered into the algorithm at the first instance.