Coding Basics 3-Part Series: What is Source Code?
By Tech Elevator Instructor Walt Impellicceiri
[PART 2] In this 3-part series, I’m breaking down the foundation of computer programs. We will focus on what they are and how they are made.
Source code is the basis of a computer program. It’s the written text by which a software developer communicates to or controls a computer. To further explain what source code is, I’d like to break it down into 3 parts:
- What is the text that source code is written in?
- How does the computer understand what I mean by my source code?
- How does one organize their source code?
1. What is the text that source code is written in?
Source code is written within a file. The file is just like others that you’re likely familiar with, image files, word processing files, spreadsheets, etc. The main difference is that a source code file contains a specific type of text. Source code files contain text written in a programming language.
Programming languages have a specific syntax and rules that a software developer must adhere to. This structure helps a computer understand what a developer is asking it to do. I’m not going to delve into the specifics of any one programming language, but allow me to paint a high-level picture of what many programming languages offer.
- Mathematical operations
- Temporary data storage.
- When you are doing something, it’s often advantageous to remember what you did previously.
- Adding two sets of 3 numbers A, B, C and A, B, D can be written like this:
- A + B + C = ABC
- A + B + D = ABD
- OR it can be written like this, where A + B is stored and re-used:
- A + B = AB
- AB + C = ABC
- AB + D = ABD
- Notice that the latter approach requires one fewer addition. I counted 3 plus symbols instead of 4!
- Logic and branching. If this, then do that; otherwise, do this instead.
- If it’s 6am AND the alarm is set for 6am, sound the alarm!
- OR, if the sleep timer has run out, sound the alarm!
- Otherwise, keep quiet.
- Repetition. Do these operations while these conditions are true.
- Step 1: If you are thirsty, go to the next Step. If you are not thirsty, go to Step 5.
- Step 2: If there isn’t water in your glass, go fill it up. Go to the next Step.
- Step 3: Take a drink of water. Go to the next Step.
- Step 4: Go back to Step 1.
- Step 5: Go on with your day.
2. How does the computer understand what I mean by my source code?
A computer understands a set of instructions called machine code. So, as a developer, we are looking to somehow convert our source code into machine code. If you recall from the previous article about computer programs, machine code can be created by compiling source code.
Think of it this way. You speak English and you’re visiting a restaurant with a menu in Japanese. In this scenario, you are the computer and your machine code is English. The source code is the menu, written in Japanese. You would need a translator to convert the menu into the language that you understand. This translator could specifically convert Japanese to English.
Though, sometimes translation to machine code is more involved. In these cases, you may also need a program that further translates the compiled source code into machine code. This is sometimes what we call an interpreter or a runtime.
In this scenario, you may speak English fluently and understand French. Your translator may speak both French and Japanese. The translator could convert the Japanese menu into French so that you can understand and interpret it. From there, you could translate the menu into English, and off you go!
3. How does one organize their source code?
As I mentioned earlier, source code is written in a file. Sometimes, however, source code can be written in many files. As developers, we often distribute our source code across files and folders to better organize our code. I’m sure you have run into the scenario where your computer’s desktop becomes cluttered with files. It’s no different with source code. Using folders can help keep things organized.
Generally, as the complexity of the problem we are working on increases, so do the number of lines of source code, and so do the number of files that make up the source code. For example, if we were to create a calculator, it may be simple enough that we could store it in one file. On the other hand, if we were to build a search engine, it may be spread across many files and folders.
One thing to be aware of is that distributing source code across files poses a problem. Often, code that is in one file needs to reference code that is in another file. Fortunately, this is easily accomplished with most programming languages. References to other files or groupings of files are typically written at the very top of a file to make clear what the current file depends on.
The last thing I’d like to mention about source code organization is that developers often like to share and collaborate on source code. To do this, source code needs to be stored in a centralized repository in which all developers have access. This is accomplished with a version control system like Git. I’ll discuss Git in more depth in a future article.
Thanks for reading this series on computer programs. If you haven’t already, I encourage you to check out part 1. And if you’re ready for my last installment, check out part 3 where I discuss what an algorithm is.
Good luck on your coding journey. If I can help with anything along the way, feel free to drop a comment below.
Walt graduated from the University of Pittsburgh with a degree in Computer Engineering. He has spent the last 10+ years developing applications for companies in the healthcare, retail and education industries. He leads instruction at Tech Elevator Pittsburgh.