There are three main characteristics that make something an algorithm:
- They are sequential. They don’t jump around. They followed a defined process step by step until the algorithm is complete.
- They are conditional. They perform steps based on specific conditions that you have defined and won’t run if those conditions aren’t met.
- They are generalizable. You should be able to easily adapt your algorithm to solve a variety of similar problems. So nothing is hardcoded into the algorithm that would make its purpose too narrow.
I can think of many early projects I coded that were rendered completely useless over time because of hardcoded data. Or worse, projects that dragged on for lines and lines of repeated code that could have been elegantly reduced into a well-thought-out algorithm.
Your algorithm could contain one or more functions. A function is a set of repeatable instructions that takes an input. If you have a clear understanding of the task you want to accomplish, you should have no trouble composing a clean function.
The control flow and logic of your algorithm determine which steps happen and in what order. If this, then that.
Algorithms can help you optimize your projects. Not only will they make it easy to transport your code into other projects, they will also make your code shorter, easier to understand, and often save on processing time and resources.