# Learning how to Program if you already know how to Code

_How do you begin writing code in a programming language you just picked up? how do you start? what is the first line of code in a new project?_

By Yonatan Vega · November 2, 2024

> Source: https://fullstack.rocks/article/new-programming-language

![Women in futuristic environment working on a computer](https://storage.googleapis.com/fullstack-rocks-media/futurism-min-fbd95df1f92e.png)
_AI Image by freepik_

Learning how to write code in a new programming language is kind of like learning how to draw with a new type of tool, you first have to take a look at how other artists use it, and what they use it for. Then, you pick up your new tool and begin by making mistakes, this is natural, confusing and a critical point in your studies, you can either give up, or you can try to understand what went wrong and try again.

This cycle is the fundamental method of learning how to program, or how to pick up a new programming language to add to your tool-set.

So how do we apply this method for learning how to program?

### Step 1: Investigate

Lets take the `Go programming language` as an example, Go is relatively new in the sense that it is in the process of being more widely adopted, and is very different from other programming languages, it might scare programmers that are used to OOP.

So instead of diving head first into Golang, we first have to investigate it.

Luckily, open-source code exists in spades, all we have to do is choose what kind of project we would like to investigate, this is a perfect opportunity to read code from a project we are interested in like a framework, embedded program or a library that we might need in the future.

Initially we might want to stick to something as simple as a library that reads environment variables from a file, such as [**godotenv**](https://github.com/joho/godotenv), this kind of library is perfect for new Go programmers, it's simple, easy to read, has a bunch of comments in its source code and has a great documentation.

#### Choose a project with clear documentation

The library offers clear and minimal usage documentation.

This makes the library easy to understand for programmers that already write code in another language, as we can easily pick up on the syntax while reading the docs.

For example, here is a code snippet directly from the [README.md](http://readme.md/) file for [godotenv](https://github.com/joho/godotenv)

```go
package main

import (
  "github.com/joho/godotenv"
  "log"
  "os"
)

func main() {
  err := godotenv.Load()
  if err != nil {
    log.Fatal("Error loading .env file")
  }

  s3Bucket := os.Getenv("S3_BUCKET")
  secretKey := os.Getenv("SECRET_KEY")
  
  // now do something with s3 or whatever
}
```

Before I dive into analyzing the snippet, here's a tip about reading code; **Always begin by reading the last line / lines of code,** the reason why is that this will give you an indication of what to expect from the function.

If the function returns a primitive such as an integer, you might expect some math, if it returns a structure, you might expect an instantiation of a structure or some data retrieval from an external source.

#### For the analysis, we have to understand what the library does

Basically, environment variables are variables being stored in memory by the underlying operating system, and are available to programming languages that can interface with these variables.

If you don't understand what the project you picked to investigate does, either choose a different one, or read up about it (I hear Google is *pretty good* for this kind of thing).

#### Now we have to find some recognizable landmarks

![Eiffel Tower, Paris, France](https://storage.googleapis.com/fullstack-rocks-media/eifel-ad21cbcfbeca.jpg)
_We have to find some recognizable landmarks — Credit: [Chris Karidis](https://unsplash.com/@chriskaridis)_

The following is an example of how I would approach an analysis of a programming language, my method involves finding some keywords that I can relate to another language, later I look up these keywords and try to read some articles or read the docs if they're clear enough to understand.

I recommend that you try to follow the analysis using the code snippet above.

Lets begin.

#### package

So we can understand that Golang "packages" code by using the "package" keyword, if we know what namespaces are or have worked with any programming language where we had to import / export code, than we can figure out what this keyword does fundamentally.

#### import

This one is more interesting, we recognize that Golang has a couple of built-in packages that are named "log" and "os", but what really catches the eye is the first import, the one where we seem to importing a URI, but not just any URI, specifically the one pointing to the Git repository hosting the snippet that we are currently reading! Well, we can now surmise that when we import public repositories into our Golang project, internally the files are being organized by their origin, which is important to know when we begin writing some code of our own.

#### func and the first variable assignment:

There is no need to explain the `func main()` method to any programmer, but what might seem odd is the variable assignment `err :=` (or "Short variable declaration" as they are referred to by the documentation / "shlong" variables as referred to by me), this assignment should prompt you to investigate this even further and find out about the type of variable and constant declarations available in Go (if you haven't done so already).

**If you are already with familiar with** `:=`  from other programming languages, then it makes sense to look into the usage of the way go handles variable assignment using this operator, as opposed to what you are already know from other programming languages.

#### godotenv.Load()

Pretty self-explanatory, right? not if you write PHP code, where instead of the dot notation you have to type out `→` for accessing properties, while the `.` notation is used for string concatenation! or the "Scope resolution operator" (also called "paamayim nekudotayim") for accessing static properties and methods (typed out like so: `::`).

This is pretty elementary but important to keep track of, especially in more complex languages were there might be a few ways of accessing properties and methods.

For example, `c++` uses both the `.` notation and `->` for accessing properties from objects vs pointers to objects, while the `::` is used for accessing global variables, defining class methods out of header files **and** accessing a class's static properties!

Sometimes you might have to investigate a simple property access notation to find out about a feature of a programming language.

#### err check

If there is one thing that you will do over and over again when writing Go code is error checking (hopefully), and 12 lines into the code and we can already see a very basic error check in Golang, even though we can assume there are better ways of error checking, here is a perfect example by another programmer.

#### **Finally** we can recognize a simple comment's syntax.

If you read the above, you might believe this kind of analysis is overkill, if you do, you might already be fluent in a couple of programming languages and can do this kind of thinking on the fly.

For most programmers that can only read / write in a single programming language, this snippet of code should take a bit of time to analyze, and this method is the one I use when reading code in a programming language that I'm not fluent in.

As a dare, I challenge you to try and read a simple Erlang program (as long as you don't know it already), my method basically prompts you to read code slowly and thoughtfully, some programming languages will be harder to read, and require more research, but for most, you should be able to at-least get a rough idea of what a program does, and how it's structured. 

#### Now that we read our first snippet of code, we need to dive into the file structure

![Man jumping off of Hawaii’s spitting cave](https://storage.googleapis.com/fullstack-rocks-media/dive-in-659681cbd229.jpg)
_Diving into into the file structure — Credit: [Kekai AhSam](https://unsplash.com/@kekaiahsam)_

The #1 question I get from programmers new to a programming language is "where do I start?", as an experienced programmer this may seem obvious, every program has an entry point, and they all need an initial setup. Recognize that entry point, recognize the setup required and GO.

Not so fast, if you're reading this article to teach, know that you cant just throw someone into the water and expect them to swim, especially in a work environment. If you're reading this article on behalf of yourself, know you will be thrown into the water and be expected to swim, especially in a work environment.

Either way, teaching and learning by example is usually best, if you can analyze the example like I did in the above "scroll", you will be able to at-least write your first few lines of code and maybe even load a couple of files.

#### Learning by example

In the snippet above we had a single call to the library that we are currently investigating and that call was `godotenv.Load()` so if we want to understand where the author of the library expects the entry point of this project to be, than we have to follow the function call.

Let’s first take a look at the file structure, Golang is known for its simplicity and so you might be surprised to know that many Golang project authors don't even bother with creating folders for their code.

The reason why should be obvious (not) if you remember that Golang organizes downloaded packages by their URI, and to load them you have to import what seems to be a repository, and so many Golang projects are broken down into different repositories and are then loaded by the author into the main project.

Because Go projects are usually broken down into pieces and are then being loaded, there is usually little to no need for complex file structures. At-least, thats the point, in the real world results may vary.

Now we are ready to follow the function call, this step is not required for learning how to write code but instead will introduce you to the file structure that the programming language of your choosing will require.

Now you should know where to place your first file, and on to the next step we Go!

### Step 2: Not ready, set, GO!

![Relay runner](https://storage.googleapis.com/fullstack-rocks-media/runner-36cd1f8a3882.jpg)
_Not ready, set, GO! — Credit: [Braden Collum](https://unsplash.com/@bradencollum)_

Can you ever be ready for starting anything? When are we ready to start working out? When are we ready to call a friend we haven't spoken to in a while? When are we ready to write the first line of code for our **BILLION DOLLAR** software idea?

We have to start somewhere, and as long as we can put on our running shoes, know our friend's phone number or have a keyboard, **we can start.**

I might not be able to help you get super fit, or fix your relationships, but I sure can write a mean study schedule for a new programmer, in fact I did just that for multiple programmers over the course of roughly 3 years, and coming up is my method for creating a programming syllabus.

## First, what am I trying to achieve?

Learning is about asking questions, and so the first step is asking yourself "what am I trying to achieve?", If you want to build a website, first understand what kind of foundation a website has to be built on, for example:

- any web service has to be hosted
- has to have a running daemon that directs user requests to the correct piece of code
- has to have input and output
- and so on.

Once we recognize some of the obvious (not) obstacles that we have to overcome, we can also recognize some of the things we have yet to learn, and so we can already create a reading list for ourselves before we approach the project, reading up beforehand will provide us with the confidence that we need in order to start, and most importantly, will provide us with some specific questions to ask Google regarding our project.

An example reading list for this kind of project might look like this:

- [Back-End Web Architecture - codecademy.com](https://www.codecademy.com/articles/back-end-architecture)
- [Go by example - gobyexample.com](https://gobyexample.com/)
- [Creating a web server with Golang - blog.logrocket.com](https://blog.logrocket.com/creating-a-web-server-with-golang/)
- [Why use Port 8080 for web hosting - Gibson Research Corporation](https://www.grc.com/port_8080.htm)
- [Connecting to a PostgreSQL database with Go's database/sql package - calhoun.io](https://www.calhoun.io/connecting-to-a-postgresql-database-with-gos-database-sql-package/)
- [A beginners guide to Packages in Golang - callicoder.com](https://www.callicoder.com/golang-packages/)
- And so on...

While researching a topic, we will usually begin by filling up some holes in our understanding

In this example, I started by looking up "backend anatomy of a website", I found out that the results are pretty technical and would require me to experiment, and so I went on to learn some "Go by example", luckily enough, one of the best resources for learning Go is called [gobyexample.com](http://gobyexample.com/), and so I began reading up and writing some of my first lines of Go code.

I then felt comfortable enough to try and build a basic website and so, I went on and looked up "running a server with golang", Google returned 8,440,000 results and so I picked the first one, which was perfect, because the tutorial didn't force me to download any packages.

"*Why does every tutorial make me use port 8080 for my server??"* I had to look it up and learned a couple of things, a perfect couple of facts to tout over lunch with the peeps from work.

I would now like to store some of the POST requests I've been making to my newly created server but I need a way of connecting to a database, I like PostgreSQL and so I looked up how to connect a database.

Alright, so to connect to a database I now have to learn how to download packages and how to manage them, seems like a perfect opportunity to learn about package management in Golang...

I had to stop there as I think the point is clear, *keep asking questions, and you will keep progressing.*

# Step 3: *There is no Step 3*

![Image on a spoon on a dark table](https://storage.googleapis.com/fullstack-rocks-media/spoon-9f4bd03bcbbf.jpg)
_"There is no spoon" — Credit: [Raul Angel](https://unsplash.com/@raulangel)_

The last line of the Step 2 is all you need to keep learning, *keep asking questions, and you will keep progressing.*

It may seem like an obvious (here's that word again..) general statement, but in my experience, programmers who consistently ask questions eventually become better programmers than their co-workers who are afraid of looking dumb.

One reason could just be the fact that asking questions is the most important step of learning, but theres another reason, I found that personally I tend to share new things that I've come across in my work or research with people who consistently show interest in their (or my) work. 

Asking questions shows you care and show that you're thoughtful, making a mistake because you didn't want to look foolish for asking a question will in-turn make you seem foolish.

# Finishing thoughts

Bjarne Stroustrup, the creator of C++ has an FAQ section on his website with the following very standard question: [*"What is the best book to learn C++ from?"*](https://www.stroustrup.com/bs_faq.html#best-book)

Even though Bjarne wrote a book about C++, the first line from the answer contains the following:

> There is no one book that is best for every person. There couldn't be one. People are too different in the way they learn, in what they already know, in what they need, in what they want, and in what kind of effort they are willing to make.
>
> — Bjarne Stroustrup, Danish computer scientist,
> the creator of the C++ programming language

and from the question: [*"How long does it take to learn C++?"*](https://www.stroustrup.com/bs_faq.html#How-long)

> ...if you want to be fully comfortable with all the major C++ language constructs, with data abstraction, Object-Oriented programming, generic programming, Object-Oriented design, etc., you can easily spend a year or two - if you aren't already acquainted with those techniques (say, from Java or C#).
>
> Is that then the time it takes to learn C++? Maybe, but then again, that is the timescale we have to consider to become better designers and programmers. If a dramatic change of the way we work and think about building systems isn't our aim, then why bother to learn a new language? Compared to the time required to learn to play the piano well or to become fluent in a foreign (natural) language, learning a new and different programming language and programming style is easy.
>
> — Bjarne Stroustrup, Danish computer scientist,
> the creator of the C++ programming language
