💾 Archived View for rawtext.club › ~mieum › zine › issue002 › sloum_getting-started-with-programming… captured on 2023-06-14 at 14:29:59. Gemini links have been rewritten to link to archived content

View Raw

More Information

⬅️ Previous capture (2021-12-03)

-=-=-=-=-=-=-

Getting Started With Programming

I am a big believer in making your own tools *when possible*. For example, I do not have the ability to make a computer processor myself (well, not the kind used in modern general purpose computers), but I do have the ability to learn to program software that will run on that chip.

This, of course, can extend beyond computing: making a jig when working with wood, making pottery or baskets, sewing, etc. The way the world is at present, or maybe always, it is hard to control the whole chain. You likely have to acquire something to do the making: a hammer, a needle and thread, etc.

With computers it is comparatively easy to make things once you have a general purpose computer. Every operating system I can think of allows for some type of programming of applications and/or the system itself. Once you have a system you can create an endless array of tools for yourself, or modify ones that already exist. You could even code a bootloader and an operating system if you were so inclined.

I believe just about anyone can do this (write applications for computers), if they are so inclined. This is not something solely available to C programming wizards that have been doing it for decades. There are lots of easy to approach programming languages and environments for people new to the concepts.

Why Build Your Own Tools

At the end of the day the best reason I can think of is for the satisfaction of using something you built and of knowing how a thing works (at least at the level of abstraction needed to be able to mostly control or modify it).

Of course there is also knowledge. The learning that happens when you build a piece of software can be quite valuable. It can help you learn to think and reason in different ways. Solving problems is good brain exercise.

How To Get Started

As stated above: you do not need to have prior programming experience to start building your own tools. The first step is to pick a language to work with. This choice mostly doesn't matter too much, at least from a certain perspective.

Languages

When starting out it is good to choose a language with good documentation and lots of resources online for working with that language, but most languages you will find nowadays will be fine. A few good/popular choices that come to mind:

Python

Python code is generally easy to read and reason about (at least at beginner levels) and there is a lot of information about writing Python programs available online. The built-in library has just about anything you'd need to write most types of programs, even graphical programs.

If you are on Linux or OSX it is likely that Python is already installed on your system. Read ahead to learn a small bit about terminals/shells and then run `python3 --version`. If you get a response that says it is not found, try running `python --version`. If it is still not found then you will need to install it with the instructions on the Python website (linked above) or with your package manager (if you system uses one).

Lua

Lua, like Python, is straight-forward and easy to read and reason about. Lua's built-in library is MUCH smaller. This has a lot of benefits though: Lua is a small enough language that a person can reasonably hold pretty close to all of it in their head at one time. So after a bit of practice you should just be able to write things without constantly looking at the documentation. Some downsides are that the built-in library does not have things like sockets (which let you communicate over the internet as a client or server, among other things). A lot of video game engines allow games to be written in Lua (Love2D, Pico-8, etc.), so if you like video games Lua might be a good choice.

Both of the above are interpreted languages. To run any programs written with them, a person needs to have the Lua or Python program installed on their system. Thus, sharing your creations has an added step of getting someone to get that interpreter on their system.

There are also compiled languages. These languages use a compiler (another program that converts the code you write into lower level code of some sort - it varies from compiler to compiler). Once a program is compiled it can generally be run without needing to have the language installed on the system or invoking and interpreter. Most programs on your system were likely written in the programming language 'C'. This is a fairly old programming language that I do not generally recommend as a first language to people these days. It is VERY powerful, but can be harder to reason about than Python or Lua, especially for a beginner. It can also be tricky to find actionable information on. There are a number of compiler and library implementations, and they may vary in how they are used. The language features and available functions/macros can also vary from OS to OS and architecture to architecture.

Rust is a newer programming language that can work in similar domains to C. But it is a complicated language in comparison the the others being recommended here. The programs it creates, when coded well, can be really solid and safe (from a memory standpoint)... but it can be a lot for someone just starting out.

As such, I tend to recommend learning Go as a first compiled language:

Go

Go is a more modern compiled language. It differs from C in a number of ways, that mostly make it easier for you to program... but make it less suitable for certain types of applications (anything where garbage collection would be a problem - but that won't be an issue for 99% of people learning to program). The documentation is fantastic and the code is easy to reason about. The built-in library is almost as complete as Python's (though there is no direct help with GUI programming built in).

All of the links provided above are to "Getting Started" instructions. Again, the language doesn't matter so much. If you see something out there other than one of these three that tickles your fancy and have found readable documentation that makes some level of sense to you: go for it!! I just recommend these three to give novices somewhere to start.

You may have noticed I did not include JavaScript. This is intentional. I do not think it is a good place to start. The ecosystem is complicated and the tooling is difficult. To effectively use JavaScript in a modern way in a browser you will likely need to learn HTML and CSS along with JavaScript. Additionally, it has become trendy to use various frameworks and libraries rather than just writing JavaScript code (using React, Vue, Angular, Svelt, jQuery, etc). These extra things to learn can make approaching JavaScript as a beginner confusing and difficult and may create long term confusion about some basic programming concepts. It is also harder to look up documentation due to the wide variety of frameworks that people work in. While you could use nodeJS to build console applications like you would when starting out with Python or Lua, I just feel like nodeJS is not as good as Lua or Python and that its ecosystem is terrible (opinions on this will vary, and I am just giving you mine).

You may also notice, if you have experience in such things, that I have not included any sort of shell programming (bash, fish, zsh, etc. scripting). While these options can be very useful for system automation and can even be used to make larger programs... their syntax is often not clear and the ability to just call as if in the shell is unlike most of the rest of the programming world. They might be something good for people to look into, but not a great choice as a first language as I think they would create some misunderstandings and possibly bad habits.

There will likely be many opinions that disagree with me and encourage starting with Rust, C, or some functional language (Haskell, Clojure, Elixir, Erlang) or other. All of these languages are powerful languages that can be really great to work with. I have made my recommendations based on my experience and a desire to allow someone to "dip a toe in". Not everyone needs to become a systems programmer, and my advice is really geared toward people that just want to try out coding to see what it is all about. In earlier times BASIC or Pascal would have been recommended for this purpose, but those days are largely past us. I think Python, Lua, and Go are sensible choices for someone to try things out.

Terminals, Shells, and Text Editors

Once you have followed the installation instructions for the language you have chosen, you will likely need to open a terminal window.

Many folks that do not program may not know much about terminals (on any modern system it will likely be a terminal emulator... but you don't need to worry about that). Each of the major operating systems includes some textual interface to the system. On OSX the application is `Terminal.app`. On Linux or BSD there will be a terminal of some form, but its name will vary by distribution/OS (`gnome-terminal` will be present on Ubuntu, for example). On Windows, if memory serves correctly, it is called `Cmd.exe`, but don't quote me on that (I have not used a Windows machine, even in passing, for over a decade).

Once the terminal is open you will be presented with a prompt of some sort (likely ending in ` gemini - kennedy.gemi.dev , `#`, or `>`, but that is not a rule). You can type commands at this prompt and get output. On Linux/BSD/OSX type `ls` and press Enter; on Windows type `dir` and press Enter. You should see a list of files in the current folder. You entered the name of a program: `ls`/`dir`. This program takes your current directory and loops over all of the files in the directory and outputs their names. It then exits, returning control to the terminal (actually the shell, which is running inside of the terminal, but lets not get ahead of ourselves).

Your first programs will likely be similar: you will call them from the terminal, they will do their thing, and then exit. Many programs can take arguments, additional pieces of information being passed into the program. For example, the `ls` program can be entered as `ls -l`. The `-l` is an argument that tells the `ls` program to output extended information about each file... its owner, access settings, size, etc. So, the `ls` program has code to look at the arguments that were sent into it and do things with them. Your early programs will likely not include many arguments, but it is good to know about them since passing in paths to files or other input will quickly become useful.

I do not know much about the Windows command line, so from here on I will be talking about Linux/BSD/OSX. If you enter:

cd ~
mkdir programming_practice
cd programming_practice

You will move directories to your home directory. The program `cd` takes a file path as an argument. In this case the `~` is something the shell (the actual program running inside the terminal that lets you input commands) knows to expand to meaning a user's home directory. We then create a directory in your home folder called `programming_practice` and then `cd` into it.

You can always go directly to this folder with:

cd ~/programming_practice

Then you can run `ls` to see any files or subfolders you may want to work on/in.

You now have a place to work on programs. You can navigate to this folder in a graphical file browser as well.

Now you are ready to write a program... but you need something to write it with. Using 'MS Word' or OSX's 'Pages' will not work. Those are word processors that use their own document formats as output. You want to work in so-called 'plain text'.

There are editors for your terminal as well as graphical editors. For simple things using Windows' 'Notepad' or OSX's 'TextEdit' can work fine for this purpose in a pinch, but using an actual code editor will provide many useful things to you, including automatic coloring of various words and structures in your given programming language to make the code easier to read.

Here are some editors to look into (all of them are free to use):

For those that are used to terminal environments and have used them before, or feel like a challenge (one that will, in my opinion, have a great payoff)... the following are great options:

Both of them offer graphical versions as well as terminal versions. But neither are as simple as just opening it up and typing. They require you to learn lots of commands and key combinations to use them. They offer very powerful editing capabilities for coding and other writing, but may not be a good place to start if you are not familiar with them. Vim is likely installed on your system and can be invoked by typing 'vim', followed by an optional filename, at the shell. You will immediately notice that it does not respond like you are used to when keys are pressed. It is a common joke that vim, or its predecessor 'vi' (also likely on your system), traps people in it if they don't already know how to use it. If you end up in one of them and can't get out, press Escape, then type: ':q!', then press Enter. I have less experience with emacs, but it is not without a steep learning curve as well.

Once you pick an editor and a language and get them both installed you may want to try running a program. Here are some very basic example programs that you can type into a file for each of the three languages I recommended above:

Python Example File

Create a file named `hello.py` in the current directory (`nano hello.py`, for example) and enter the following text:

def say_hello():
	name = input("What is your name? ")
	print("Hello " + name)

say_hello()

Then run the following in your terminal/shell:

python3 ./hello.py

If it doesn't work, make sure you have Python 3 installed (type `python3 --version` at the shell and see if you get a Python version number printed. It is possible that the program may be called `python` on your system, in which case replace `python3` in any of the above with `python`).

If it is installed and still not working, make sure you are in the folder containing your file. Run `ls` to see the folder contents. If you don't see your file, run `pwd` to see what file-path you are at and navigate as needed to where the file is using the `cd` command. Then try running the program again.

Lua Example

Create a file named `hello.lua` and enter the following text:

function say_hello()
	io.write("What is your name? ")
	io.flush()
	local name = io.read()
	print("Hello " .. name)
end

say_hello()

Then run the following in your terminal/shell:

lua ./hello.lua

If it doesn't work, make sure you have Lua installed (type `lua -v` at the shell, if you get a version number printed then you are good to go, if not then you should check your install instructions for Lua).

If it is installed and still not working, make sure you are in the folder containing your file. Run `ls` to see the folder contents. If you don't see your file, run `pwd` to see what file-path you are at and navigate as needed to where the file is using the `cd` command. Then try running the program again.

Go Example

You may notice that this is very different from the Python and Lua. Go has some rules for how file structure should be set up, they aren't too complicated.

Create a directory for the program by running `mkdir go_hello && cd go_hello` at your shell/terminal. Then create a file named `main.go` and enter the following text:

package main

import (
	"fmt"
)

func main() {
	var name string
	fmt.Print("What is your name? ")
	fmt.Scanln(&name)
	fmt.Printf("Hello %s", name)
}

Then run the following in your terminal/shell:

go build
./go_hello

You may notice that running the program is a little different here. We call `go build` first. This compiles the program into an executable file. We then run that file by giving the shell the path to the file (`./` just means "inside the current directory").

That file will run on any system with the same OS and Processor architecture (there are maybe two or three in common consumer usage at present, though retrocomputing enthusiasts or people on the cutting edge may have others). This is a cool benefit of using Go: you could just share that file (or output one that works on a friends machine) and they do not need to have Go installed on their system at all.

An Actual Application

Your first programs will likely be simple things that print output to a terminal screen, or maybe take a bit of user input and modify it or print it out. Most languages will have a "getting started" guide that will walk you through these introductory steps. Once you get past the basic concepts and structures you should be able to pick an actual project that creates something useful for you.

You will often hear the refrain "don't reinvent the wheel". I could not disagree more. Trying to either replicate the wheel or straight up reinvent it may not produce a better wheel... but it will almost certainly teach you a lot about wheels.

I, for example, am writing this in a text editor I wrote. That editor is being run by a shell that I wrote. I am listening to music acquired with a web scraper (similar to youtube-dl) that I wrote. I did not code the audio player, but am working on that as a project as well. When searching for music I usually use an internet client that I wrote.

Not everyone needs to go to the trouble of all of that, but I can certainly say I have enjoyed it. I know how these programs work (again, at a reasonable abstraction level) and can change them when they do something I don't want them to. They are sometimes buggy, but they get better each time I add to the code.

For first projects I recommend thinking about the tasks that you do on computers or in life a lot. Think about what could benefit from automation or data storage/retrieval. Perhaps you read a lot and want to keep track of what you have read? A simple system to store and recall books by genre, author, title, or rating should be fairly easy to construct (with or without a database). Perhaps you write on gopher or gemini a lot and want to create a tool that converts your documents to HTML? Or maybe you like games? Converting a board game to a terminal game is a great way to start learning to code. Creating a game for two human players is easier, since you don't need to program a "player" for the game.

Here are some steps for thinking about how to code your first bigger application:

If you find you are stuck, there are resources online. Stack Overflow is a code help site where you can post questions and get answers. Your question may have already been asked, so you could search for it.

Stack Overflow

There are lots of developers using public nix systems. If you are on one, it may be a good idea to ask around for help. I think people generally want to support their communities on these systems and would likely be able to point you in the right direction.

If you want, feel free to reach out to me: <sloum AT THE HOST rawtext.club>.

Wrapping Up

This started out as a writeup about creating the tools you use, and turned into more of a "how to get started coding". I know it is cursory at best, but hopefully has some information useful to someone. I know there are a lot of people out there that want to learn how to do these things and may feel overwhelmed by the sheer volume of information and all of the options. We are living in a time of plenty in many respects with regard to languages, libraries, frameworks, tutorials, opinions, etc. Though there is less variety now in the systems themselves (a shame).

The key thing is to try and have fun, but expect there to be some hurdles and roadblocks. I think programming appeals a lot to people who like to solve puzzles and ponder different ways of doing things. If this describes you, then you may enjoy programming. It can be a useful skill, but it can also just be a fun skill that could serve the same role as solving sudoku puzzles for someone, but with the added benefit of creating something cool or interesting. However you come to it, you may be starting down what could become a lifelong obsession. It has been for me (I started coding with BASIC as a kid in the late 80s).