The Basics are Pretty Basic
Coding is all the rage these days, even for children. For kids we have lots of coding camps, on-line courses and the like, to teach total beginners how to code.
But what about teaching smart technical adults, such as PMs, system architects, and sysadmins who have no actual programming experience? How do we do that?
Here is a way we are trying.
First, we chose Go as the language, both because it’s increasingly popular and because it has numerous desirable concepts, such mandatory typed variables and no object-orientation. These make it a good teaching language, as does its ability to build and run anywhere, simply.
So how do we teach programming? What do we tell new people?
Flow & Work
First, all programming (in procedural systems) is really about controlling the flow of the program while simultaneously doing useful work. Everything revolves around this, thus we have numerous constructs to control the flow, such as IF-THEN, Loops, functions, etc.
We also have lots of constructs to do real work, such as formulas, string and data processing, and various others useful functionality.
Inputs & Outputs
All programs need inputs and have outputs, so often a lot of programming is about getting and emitting this data. For simple command-line tools, it’s often user and file inputs and user and file outputs.
More complex systems typically use databases for this, most simply in the form or raw SQL, but increasingly with ORMs and other abstracts.
More modern output is also often generated in HTML, JSON, and/or various template languages that feed today’s websites, apps, APIs, etc.
Entry & Exits
Programs have a single entry point, usually a main() like place, but many exits, often due to errors or other problems. It’s best to plan the flow from the entry point, but consider where and how to exit in various situations, though this depends on the language.
Libraries
Most programs use other programs, typically in the form of libraries we can call. We can write these or get them from other places. Normally we have to declare them at the start of our programs so our code can find and use them.
Variables
All our data is stored in variables, which have names and types. Most names are case-sensitive, and basic types include Integers, Floating-point numbers (with a decimal), and strings of text. You should name and type your variable carefully, which Go will enforce on you more strongly than other languages (which is a good thing).
More complex types include arrays, which are just groups of other variables or data in a list. There are many types of lists and array-like things, but simple arrays are the basic building block for processing blocks or lists of data.
An important variable concept is Scope, which is where and when the code can see the variables. The best and simplest is local scope, where variables are only visible where they are defined, and not anywhere else. This is safest and allows you to re-use variable names.
Flow Control
Once our program starts, we need to control the flow, usually with a mix of IF-THEN and Loops of various kinds. The IF-THEN is quite simple, where we test some variables and logic to see if true or false, then execute some code.
Loops allow us to execute the same code several times, but on different data, usually from an array or list of data elements.
Functions
We can build our entire program in one set of code, in one file, but this gets very large and complex. A single 1,000 line program is very hard to understand, manage, or troubleshoot.
So we usually separate different logic blocks and put them in functions, which we can call from our main program. This allows for nice organization, and also re-use, as we can call functions from several places to do the same things. Functions are often put in separate files to help keep things small and organized.
To call functions, we normally give, or pass, them arguments, which is the data the function will use for its work. This is a complicated area and data can be passed several ways, but generally we call the function with a few variables that hold data we want to give the function.
Many functions also return data back to the caller, either by updating the arguments directly, or actually passing back one or more return variables. Go has both of these, but different languages handle this differently.
In addition, functions may also interact with other parts of the program by calling other functions and/or reading and writing data from files or databases, too.
Debugging
All programs have problems and need debugging or troubleshooting, especially during development. This is done in various ways, but the simplest old-school way for simple programs is printing output messages in various places. This is messy and painful, but common and both easy to do and easy to understand.
More complex systems can be run under debuggers so they can be started and paused while visually showing how and what they are running. Debuggers can also see and change variables to help debug what is going on.
Compiling & Building
The code we write is not directly executable by the computer. For ‘interpreted’ languages like PHP or Python, we need an interpreter which is what the core php or python systems are — you just run those interpreters, give them your code, and it runs.
For compiled languages like C or Go, there is an intermediate step, which is compiling. This takes your code and converts it to the machine language code the computer needs, and produces an executable file called a binary. This is then what you run (without needing the original source code).
Most binaries are very specific to the CPU and OS, such Windows on Intel, or Android on ARM. Some are also version and distribution-specific, such as Windows 10, or Red-Hat Linux 6.x because they depend on various run-time libraries for those operating systems.
Compiling and building can be a complex multi-step process for larger programs, but Go programs keep this simple and produce a single binary that can run without other support or necessary libraries nor run-times. In addition, the Go compilers can cross-compile, so you an build a program on a Mac that can run on Windows or Linux or Android, too.
Conclusion
That’s it. Happy developing.