Pergunta

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?

Foi útil?

Solução

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.

Outras dicas

Algorithm and code are different, but related things. The relationship is simple: Code expresses algorithms.

The same algorithm could be expressed in different languages, including natural language, some form of pseudocode, machine language and programming languages. Barring natural language and pseudocode, we refer to these representations as code (source code if it is not machine language).

In fact, translating from some languages to others is possible and done routinely from programming languages to machine language, or from a programming language to another. Sometimes from machine language back to a programming language. This evidences that the same algorithm can be expressed in multiple languages, which results in different but equivalent code.

I'm saying that code is more concrete that algorithms. Algorithms are ideas, and we can use code to portray them, study them, compare them, talk about them. Or have a computer execute them.


when something goes from being an algorithm to being code?

Algorithms can exist before being portrayed (in code, for example). That is to say, you can figure out how to do something before writing it down. It is the expression that makes it code.

There are already many good answers to your question, so I will not rehash what they have said. They all basically say the same thing: an algorithm is conceptual and code is the manifestation of the concept in a particular programming language. I think the difference here is the context of the conversation, which you have not included in your question.

If you are having a general discussion about the bubble sort algorithm without reference to any particular language, then the subject of the conversation is the concept of a bubble sort. Therefore "algorithm" is the proper term.

If you are talking about the implementation of the bubble sort algorithm in a particular language, then "code" is the correct term.

I rarely use algorithm by itself in a conversation. I typically use both terms in the course of a conversation, depending on whether or not I'm talking about the concept or the implementation.

To say one term is never used and incorrect is just plain silly-talk.

Other answers are good but I'll boil it down for you: 'somebody' is flat out wrong and confused.

Algorithms are conceptual. In a nutshell: they are a processes for solving problems. They exist independent of software and computers. Long-division is an algorithm. The normal way of finding an entry in a (physical) dictionary is an algorithm.

You are writing algorithms in your code but the same algorithm can be implemented in code in many distinct forms. In that way, algorithms and code are related but different.

It makes no sense to call your program an algorithm. You can be fairly certain that 'somebody' knows little to nothing about programming and probably doesn't understand what an algorithm is either. Now you just have to decide how to handle this 'somebody'.

I will answer from a "craftsperson" perspective (as someone who has implemented a lot of algorithms through code)

An algorithm is a recipe. You can write it in plain English. It is a method to achieve a result. It can be more or less efficient.

For instance, if you try to guess a number between 0 and 100 (based on clues such as "too high", or "too low"), you could start from 0 and go up by one step at a time. That would give you the correct result, but require many steps. Or you could start at 50, and always aim for the middle of the valid range (e.g. if the answer is "too high", say 25 next), and you would (generally) get the correct result in less steps.

The efficiency of an algorithm can be determined mathematically. You can review an algorithm (does it give the correct result? is it efficient?) with just pen and paper, or even by discussing it aloud.

Then there's code. Code is always written in a specific programming language. Unless it's completely random and serves no purpose, code will implement various algorithms (usually more than one, for any sizeable program), in order to perform various tasks.

Implement just means: translate the algorithm into computer language. But there is more than one way to perform this translation. For instance, you could use different variable names. You could "package" the code differently (e.g. define various reusable functions, divide the functions into modules, etc.). There is a lot of craftsmanship involved at that level, in order to keep the code readable and maintainable.

If I review someone's code, I will check that the algorithms are correct (and correctly translated into code: should that index start at 1, or start at 0? should this be a "<" or a "<=", etc.), but I will also check that coding standards were followed (not just that the code compiles, but that it conforms to the way code is meant to be written within my organisation)

So as you can see, code and algorithm are related, but also very different things. And it's probable that what you were discussing was code, not algorithms.

The difference between algorithm and code is like the difference between plot and story.

Algorithms refer to how something works. Code is how it is implemented.

The "hero's journey" is a plot. You can write several stories based on this plot. One example is Star Wars (A New Hope).

When referring to a Python program it is code. How the code works is the algorithm.

So basically you are right.

To explain it to the Civil Engineer you can tell him that the statics and dynamics equations he uses to design bridges is the "algorithm". On the other hand the drawing he produces when designing the bridges is the "code". The algorithm is the "theory" and code is the result of trying to implement the theory.

A quick but loose argument

A shorter argument is clearer and simpler, and might help you convince your "somebody":

  • "Much code" - works
  • "Much text" - works
  • "Much algorithm" - doesn't work
  • "Much algorithms" - doesn't work
  • "Many algorithms" - does work
  • "Many functions" - does work
  • "Many code" - doesn't work

In your situation:

When I was talking, I said "code", to refer to a Python program

That is correct. The code refers to all of the source code.

He told me that he didn't know what "code" was, that the correct term was "algorithm". That "code" was something else.

I don't agree with your "somebody". An algorithm can refer to a particular function, or a few very tightly coupled functions, but not all of the code in a Python file.

So on a python-file(s) dimension, code is all of the text in the file(s), and algorithm is the instructions within each function.

Beyond files

However, there is a more natural dimension to consider outside of files. Consider the "Binary Search" algorithm. This is an idea before any code text is typed into a text editor. Consider how mathematical notation can describe an algorithm, or animations, or diagrams. The algorithm is an idea. An algorithm can be implemented in different ways in the same language (as code). Fourier Transform is an algorithm, Fast Fourier Transform is a more efficient algorithm, and then there are optimisations for that algorithm and various ways to implement in code.

(Going back to files, when the FFT or Binary Search function is referred to, it might be communicated as "check the Binary Search algorithm". Although that refers to the function code, the speaker is still using the high-level idea's name to nominate the function scope of code)


The nature of code

For deeper insight into the difference, consider: A programming language is just data that the compiler reads to compile the object code (binary).

In an object-oriented language like C#, the term class is more like HTML markup, it tells the compiler about how to organise the algorithms that are defined within functions (or property getter and setters). It becomes more obvious when you explore the compiled CIL (.Net byte code) which is more of a database of functions (algorithms). The Attribute tags are not algorithms that is procedurally run, they are decorations that might lead to some code (algorithms) being run.

The question you actually ask is this:

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

But I think the background you present is rather more relevant.


'Code' also has other meanings than 'program code'. See e.g. the definitions in Merriam-Webster.

The sense that's probably closest to computing is the meaning of "a system of signals or symbols for communication", as in Morse code, or 8b/10b code. Base64 is also a code, though usually called 'encoding', probably to separate from the sense of 'program code'.

Also, 'code' can also mean mean laws, rules and regulations, e.g. quoting Wikipedia: "For the traffic code in the United States, each state has its own traffic code...". Also, building codes, etc. I've never seen the word used in this sense in a programming or computing context, again probably because it would be confusing, but a civil engineer may well meet a number of laws and regulations called 'codes' in their work.

Now, I wouldn't use the word 'algorithm' for program code. Even though program code obviously is "a step-by-step procedure for solving a problem or accomplishing some end" (M-W), I would reserve the word for the more abstract concept. A program can implement an algorithm, though.

In any case, I would at least apply Hanlon's razor here, and refrain from interpreting them as purposefully facetious, and instead assume the mismatch is about differing customs or such. You also mention their background with Pascal and BASIC, and also reflect on how you see the word used nowadays on the Internet. Those don't strike me as the latest popular thing. Anecdotally, I've mostly seen Matlab in engineering studies in recent years. This could hint at a generational gap, which might also influence the accepted jargon.

Now, they probably shouldn't flat out claim to your face that you are wrong. That rather shows they're ignorant of the different usages of the word in different contexts, but that doesn't mean you need to be.

The question is marked as answered, but I'll add my opinion because I think that the accepted answer is too harsh.

An algorithm is a logical sequence of instructions, the code is one possible implementation. So the code itself could be seen as a way to show the algorithm, but it cannot be considered the only possible representation of that algorithm. The technical details of the language often influence the way the algorithm is expressed.

I don't know what was the engineer thinking, but I know that in the mathemetical environment the formal pseudo-code was developed in order to abstract the algorithms from the technical details of the programming languages, so, it may be possible that this is what the engineer was asking for.

Algorithms do not have comments. Code is more all-encompassing and can refer to a whole program or a single comment or part of an expression.

Whether you were talking about an algorithm or not, I can’t say. But you’d never say something like the algorithm is on the file share x or in the directory x on my computer or I haven’t checked in the algorithm yet (although you might say you haven’t got the algorithm working yet).

Algorithm is very specific and not the correct term for most programs/applications. A web browser is not an algorithm, neither is word processor. A sort utility could come close to being called an algorithm, so it might not be totally inappropriate.

An algorithm will almost always refer to a subset of the code in an application. Depending upon language this subset may be referred to as function, procedure, routine, method or some other term that refers to an specific part of the application The code can incorrectly implement the algorithm so even in this case, they aren’t totally interchangeable. But they are loosely interchangeable at that level, you might highlight a portion of the code and say to someone “in this part of the algorithm a lower bound is checked”, the word code could work just as well.

But frankly, it sounds like your civil engineer was jerking you around, trying to bullshit instead of addressing any real concern.

Licenciado em: CC-BY-SA com atribuição
scroll top