Mmmm, algorithms…

Tim Rooke
6 min readMar 21, 2018

--

Where to begin.

Algorithms can be scary. A little daunting. They are for me. They provoke nightmares of whiteboarding interviews and complex, theoretical problems with maths that was just way too complicated to understand. Until now, I have passed algorithms off as the work of data scientists, software engineers and practicers of dark magic.

I am a Web Developer at an EdTech startup. Having come out of a General Assembly Web Development bootcamp, and being in my first development role for less than 12 months, the thought of algorithms is a little scary. An unknown quantity to me. Therefore, in an effort to tackle this fear, I decided to try and wrap my head around this mysterious world of algorithms while also documenting my progress. I have no computer science background or any education in Maths or Science beyond school, so this series — if you can call it that — will very much be the ramblings of a complete beginner. Nevertheless, I hope some will find it useful.

What is an algorithm?

If we take to Wikipedia, an algorithm is:

“an effective method that can be expressed within a finite amount of space and time and in a well-defined formal language for calculating a function”

Hmmm… this is a little wordy for me at this stage. Let’s look for another definition… “An algorithm is like a recipe”. Ah! That’s something I can get behind. Relating something to food always makes it better — and algorithms are no exception! Like a recipe, an algorithm is a simple set of instructions, which can be performed to return a specified output.

An algorithm accepts some form of input: a number, a string, a boolean, a combination of the two. Performs a specified set of tasks on these inputs, and returns the required result. If we go back to thinking of algorithms as recipes, then this can be made simpler. For example, to make scrambled egg on toast, we have a list of inputs (or ingredients): eggs, milk, bread, salt, pepper. You perform a set of actions: crack the eggs, toast the bread, beat the eggs, add the milk, add salt and pepper, assemble the ingredients together. And like magic, you get your output: scrambled eggs on toast! When we talk about algorithms, computers are doing exactly this process: accepting inputs, following instructions and returning an output.

Algorithms are as simple as making scramble egg (though finding a picture of scrambled egg is less simple)

If you a web developer, or have written any code, you will have used an algorithm. Algorithms are baked into all programming languages. Helper methods — sort, filter, map, reduce — are all algorithms that are operating under the hood of your respective programming languages. This is why learning about algorithms is really beneficial. Algorithms can be theoretical and abstract. However, they are the bedrock of any programming language. And learning how they work — even at a simple level — will make you a better, more knowledgeable, developer. For example, most developers will have used .sort() in their time. However, many won’t know how this is sorting their array. How it is sorting my array?! Surely, sorting is sorting. Simple as that, right? Well, not quite. There are over 15 sorting algorithms that exist, which work in different ways and for different purposes. For example, Python’s .sort() is done differently to JavaScript’s .sort(). I was completely ignorant of this until last week.

Ask yourself do you know what sorting algorithm your languages .sort() uses? If not, learning a little bit about algorithms may be useful to you. If you do, carry on reading and you may still find some information.

Our First Algorithm

Let’s look at our first algorithm — or second if you count the scrambled egg example above! A simple add_numbers function. It takes two inputs: a and b. Carries out a simple instruction of adding a and b together. And returns the result — simple! This is an algorithm!

This algorithm is powerful. It can handle enormous numbers like 84376857 and 3409576356 without any problem at all and return the correct answer in the blink of an eye. Impressive!

Is that it?!

Algorithms can be that simple. Algorithms are really extremely powerful and, if designed properly (which we’ll come on to in future posts), very efficient are solving a specific problem. Our algorithm above is perfect for handling the addition of two numbers. However, it does that and only that. Our algorithm above can’t do much else. It can’t even add three digits together, so this is a limitation. We could add this functionality if we required it, but we’ll leave it for now.

Furthermore, there is a problem with our algorithm. It is designed to add two numbers together. However, we’re making an assumption that users, application or code, whoever or whatever interacts with our algorithm, will only ever pass it numbers. This is a big assumption.

What happens if I run print(add_numbers(“hello”, “world”)) ?

Given that I am coding in python, I get an output of “helloworld” when I pass it “hello” and “world”. In some other languages, you’d get an error. Python has tried to help me out. However, I designed this algorithm for one purpose: the addition of two numbers. To have a string returned from my function is clearly not right and was not what the algorithm was designed for. Furthermore, Python’s help only extends so far. If I pass my function a number and string such as 4 and “world”, I get an error! Our algorithm has broken. With little bit of validation, we can remedy this and protect against unwanted inputs.

Success! We’ve now improved our simple algorithm. We’re now protecting against instances where inputs are given to the algorithm that it is not design to handle. Here, I am only checking against integers, not floats. This is simple error handling. Error handling will be a familiar concept to most. For example, with a web application you can never be certain that your users will interact with your application in a particular way. They may use funny file formats when uploading their profile pictures, odd accented characters when writing a blog or good, old-fashioned browsers. Therefore, where they do, we as developers try to give them an friendly error page to help them out!

This is for their benefit, but also for ours. If we know that our algorithm only accepts numbers, we can be confident that it will perform in a predictable and stable manner. This makes life easier when scaling up or sophisticating our algorithm in exactly the same way as it would a web application. If you don’t check the inputs, it is much harder to predict what will happen. In our print(add_numbers(“hello”, “world”))example, you may have thought that would have failed. How can you add to strings together?! Well, Python tried to help me out by returning "helloworld" instead of returning an error.

Algorithms are no different to web applications in this respect. They are designed for a specific purpose and do one function really well. However, with algorithms just like for web applications, we have to consider the cases of how the algorithm performs when it is not used as intended.

Conclusion

In this post, we have started to get a better grasp on algorithms. We’ve had a look at how they work through the medium of scrambled egg, and why they are worth learning. We’ve even looked at a simple algorithm, tested it and improved it — using basic best practice principles of error handling, which is common practice in web and most other forms of developments, so after just a couple of paragraphs the mysterious world of algorithms is becoming a little bit clearer!

This is the first post in a series on algorithms designed for newcomers — of which I am one — that aims to lift the lid (just a little!) on algorithms, what they are and how they work.

Get in touch on Twitter or in the comments if you have any thoughts!

--

--

Tim Rooke

Full Stack Web Developer — all things JavaScript. Avid Spurs fan, who runs around a lot.