Algorithms Terminology – Difference Between Algorithm and Code

algorithmsterminology

A few days ago I had a conversation with a Civil Engineer with a background in Pascal and BASIC, and we talked about programming in Python. When I was talking, I used the term "code" to refer to a Python program, and he told me that he didn't know what "code" was, that the correct term was "algorithm", and that "code" was something else.

I really didn't know how to refute him because the way I see it is that a program can be an "algorithm". I used that term only when I was starting to program simple programs. The word I use most is "code", and on the internet, almost everyone else uses that term.

When does something go from being an algorithm to being code, if it can change. Maybe it can be both at the same time?

Best Answer

In short, while there are differences in the specific meaning of the words, that civil engineer was being needlessly pedantic and balking at you not using his preferred word. There was no justifiable reason to disrupt the flow of conversation other than them wanting to be a clever know-it-all.


Arguing over the "algorithm" vs "code" moniker is like arguing whether what I'm sitting on right now is "furniture" or a "chair". These are not exact synonyms of one another and in some cases it can be one without being the other, but the specific designation really doesn't matter in scope of the current conversation.

An algorithm is defined as:

In mathematics and computer science, an algorithm is an effective method expressed as a finite list of well-defined instructions for calculating a function. Algorithms are used for calculation, data processing, and automated reasoning.

All code is essentially an algorithm. It's a sequence of well-defined instructions to get the computer to do the thing you want it to do.

Can you have code that is not an algorithm?

Pedants might argue that declarations (e.g. public class Foo {}) are not algorithms and only operations (e.g. int c = b + a; can be considered algorithms.

I don't quite agree, as the declarations are essential to the well-defined nature of the instructions (as they define the data used in the operations). In essence, if your language's native definition of int is acceptable, then my custom definition of class Foo is as well. I see no reason to distinguish between the two in this regard.

Can you have an algorithm that is not code?

Yes. Any set of calculation instructions is an algorithm. This could be a handwritten list of steps on how to e.g. calculate the length of the hypotenuse of a right triangle (i.e. Pythagoras' theorem):

  1. Square the length of each leg.
  2. Add them together.
  3. Take the square root.

This is not code, but it is an algorithm.

The furthest stretch I could give in favor of that civil engineer's argument is that you could argue that a compiled application is still an algorithm but has ceased to be code. But I doubt you were specifically talking about a compiled file, given that Python is an interpreted language, at which point this argument doesn't even apply in the civil engineer's favor.

As an aside, while most definitions tend to restrict algorithms to the fields of mathematics and computer science, I personally see no reason why we couldn't consider e.g. a cooking recipe as an algorithm as well. It's still a sequence of well-defined instructions to achieve a specific predetermined outcome. But this is maybe a subjective argument and you might feel differently.

Related Topic