Think Like a Programmer: A Crash Course in How Coding Works

By on April 17, 2017

Steve Jobs said that everyone should learn to program because it would help them to think. This is something I agree with whole-heartedly and in my experience, learning to code has helped me to become more analytical and creative. Not only that, but it has led to countless direct opportunities.

The only problem is that learning to program is a bitch if you’re new. I know this because I am entirely self-taught.

P1020302

The idea of this post then is to show you how programming works and hopefully pique your interest in the topic. I’m not going to teach you Java or C# here; instead I’m going to teach you the ideas behind programming and the logic. That way, you’ll have a better understanding of how it works and you’ll be better prepared to pick up any language subsequently. If nothing else, hopefully you can apply the same structured style of thinking to other problems.


It’s the logic that matters after all; the rest is really just semantics!

My Experiences of Programming and Why You Should Learn

Learning to program is one of the best things I ever did. It started when I was 6 years old and I got my first computer: a ZX Spectrum. I then went on to make friends with another boy at school who was a bit of a coding prodigy and from there I started making little games that mainly featured dots moving around the screen. Later, I would program on my Tatung Einstein – the most retro-looking computer on Earth – and then move on to QBASIC on my first laptop PC (which is where I also got into writing using Word Perfect!).

Screenshot_20170414-075414

There was something truly awesome about staring at the green screen and knowing that I could create pretty much anything my imagination could think up. At Middle School a few friends and I made a little AI called ‘Bob’ and eventually I even got a game called ‘Rushdy Worm’ onto a fairly big website (QB45).

I came up with little tricks to get around my lack of actual knowledge. I created something called a ‘detail engine’ for example in order to make more complex looking graphics. This worked by looking at random coordinates on the screen and then drawing tiny dots and details on top depending on the color that it would find there. A green and brown square could that way become adorned with random blades of grass to give the appearance of a hand-drawn sprite. I even came up with one game that used procedurally generated levels!

I can’t tell you how rewarding that kind of idea is, how good it is for developing your out-the-box creative problem solving it is, or how awesome it feels to program entirely in your head while gazing out the window.

Programming has since led to a ton of opportunities. After I left University for instance, I started creating mobile apps. Eventually, this led to my ‘big breakthrough’ which was Multiscreen Multitasking. I vividly remember being at a friend’s house party and checking my phone to see how many sales I’d had… and then being blown away. I’m fun at parties that way… I haven’t been able to retire to a tropical island (sadly), but it has certainly helped supplement my income and allowed me to invest more in my business. Eventually, I was contacted by an Indian smartphone manufacturer and contracted to build a version of the app to come as pre-loaded software!

multiscreen-multitasking-thd-v19-apk2

My biggest hit – Multiscreen Multitasking THD!

My app development led to the chance to write development articles for Android Authority and eventually to make video reviews for them. And now I’m writing a book on development using Unity! Programming has shaped my career but even without that influence, it has been an incredibly rewarding and worthwhile endeavour that has changed the way I approach problems.

Building apps that other people will use and enjoy is a great feeling (and it’s nice to get paid too!). But being able to make an app to solve a problem that you have yourself is just as rewarding and potentially just as beneficial. I’ve made apps to help me work faster and better, for brain training and just for my own entertainment. Each one challenged me to think resourcefully and to look for better ways to do things. Programming affirms one of my favorite sayings: there is no such thing as a lack of resources, only a lack of resourcefulness!

Now I want to help you understand programming a little better so that you can at least use some of the concepts yourself – if not .

The Basics of Programming

In order to help you understand how programming works in a way that doesn’t melt your brain, I’m going to talk in ‘pseudocode’. This means I’m going to be using English language or paraphrased expressions, but structuring it in a way that’s similar to real code. You’ll see…

For me, the thing that truly helped me to understand the crux of programming was when I grasped ‘IF, THEN’ statements and variables.

So a variable is a letter or a word that can represent a number or another word. You might remember algebra problems from maths, which look like this:

x + 3 = 13 – find x

In this case, x is of course ‘10’.

We use variables like this in programming all the time because it allows us to dynamically change aspects of our code.

So, let’s say that we’ve drawn a dot on the screen. In QBASIC (a super easy, defunct programming language), you would do this with ‘PSET’ – which was short for ‘Pixel Set’. It literally set a pixel somewhere on the screen! You then follow this term with coordinates in brackets. So:

PSET(20, 300)

Would be a pixel that’s located 20 pixels along and 300 pixels down. So, in the bottom left of the screen.

If we want that pixel to represent the player though, then we would instead write the code as:

PSET(x, y)

Now, when the player presses ‘Right Arrow’, we can simply say that ‘x = x + 1’ and use the same code again to show where the player is now. If the player presses ‘Up’, we say ‘y = y – 1’ and draw the pixel again.

Rather than having to use hundreds of thousands of lines of code to draw the pixel in every possible location, we can just repeat the same bit of code over and over and it will update itself to show the player in the correct place.

In QBASIC you did this by saying ‘GOTO’ and using a line number:

10 PSET(x, y)
20 If Player Presses Up Then y = y – 1
30 If Player Presses Down Then y = y + 1
40 If Player Presses Right Then x = x + 1
50 If Player Presses Left Then x = x – 1
60 GOTO 10

Coding in this way (using ‘GOTO’) is considered bad practice these days. Instead we would use a ‘loop’. But for the purpose of illustration, this shows how that might work.

This example already shows us what an ‘IF’ IF/THEN statements work just the same as they do in Excel (if you’re familiar with spreadsheets) and test whether a statement is true. If it is, you do one thing. If it is not, you do ignore the code that immediately follows.

Screenshot_20170414-075350

In this case we’re saying ‘IF the player presses down THEN change x’.

Likewise, if we wanted to kill the player if they wandered too far to the right off the edge of the screen, then we could say:

IF x is bigger than 300, Game Over

(We use the symbol ‘>’ to say ‘bigger than’.)

In this way, you can build a complex sequence of events and pretty much make an entire game!

In a real programming language such as Java, an if statement would read like so:

IF (x > 300) {

Game Over;

}

(‘Game Over’ is pseudocode)

It looks more daunting but the logic is the exact same.

More Complex Logic

You can build on this use of variables and ‘IF’ statements in a number of ways.

One is to use ‘nested ifs’, just like in Excel. This way, you can create a scenario where two different conditions need to be met for the following code to execute.

Another option for achieving the same thing is to use ‘AND’. AND does exactly what you would expect:

IF x > 300 AND health = 1 THEN Game Over

Now our player dies if they head off the right of the screen but only if their health is on its last point. Health is another variable just like ‘x’ but this one represents how much health the player has.

We could also ‘show’ the player’s health to them by using code such as:

PRINT “Player Health: “ & health

This is combining a ‘string’ (text) and an ‘integer’ (number). But that’s getting ahead of ourselves.

For now let’s stick to these logic statements. For example, what happens if the player walks off the edge of the screen but they still have lots of health?

In this case, we can use an ‘ELSE’ statement. This lets us test for multiple scenarios and then offer lots of different outcomes.

So for example, we can say:

IF x > 300 AND health = 1 THEN

              Game Over

ELSE IF x > 300 AND health > 1 THEN

              Health = health – 1

              X = 1

END IF

In this example, we are using more than one command after our ‘IF’ but that isn’t what matters. What matters is that we now have a kind of branching flow chart of possible outcomes that the player can follow, resulting in more interesting gameplay.

Screenshot_20170414-075437

If the player walks off the edge of the screen with little health, they die. If they have some health, then their health is reduced by one point and then they are sent back to the ‘start’ point at the far left of the screen.

Similarly, you can use ‘OR’ statements which test whether one of two different statements might be true.

Logic Gates in ‘Real Life’

The thing that I find particularly interesting about these logical statements is that they represent the actual physical circuitry of a piece of technology. More specifically, they represent ‘logic gates’, which are simple circuits that test statements.

Imagine that you have a circuit that is simply a light bulb attached to a battery with a switch somewhere in the circuit. This is essentially an ‘IF’ statement, because you are saying that IF the switch is on, THEN the light should be on.

Logic Gates

A pretty crude diagram of how logic gates work

Now imagine two switches in a row or sequence. This is an ‘AND’ statement because you are saying that IF switch a AND switch b are on, THEN the light turns on. Likewise, parallel switches represent an ‘OR’ statement because either can complete the circuit. Consider the diagram below:

You can see here that programming is a form of ‘abstraction’. That means it is taking a physical, tangible subject and then representing it with language so that we can manipulate the concepts in our mind and so that we can better understand the way that a system works.

Object Oriented Programming

Finally, I’m going to share with you one slightly more complex subject from programming – that of ‘object oriented programming’ or ‘OOP’.

This is essentially an approach to coding – a design philosophy – which changes the way that code is structured. Most modern programming languages use an OOP structure and you need to know what this means in order to grasp them. Not only that, but it’s another concept that can help you rethink the way that you approach ideas and concepts that aren’t related to coding.

P1020293

In the previous examples of programming using pseudocode/BASIC, we have been writing commands in sequence so that one line follows the next. This is how all programming used to be and it was known as ‘imperative’ programming. Back then, you would read code just the same that you would read an article – going from top to bottom, one line at a time.

This is nice for beginners trying to learn to program because it’s very straightforward to understand. But the problems arise when you try to make a piece of code that is millions of lines long – which is surprisingly common. Suddenly, making changes to your code becomes incredibly difficult, as does finding a specific line that refers to a specific behaviour. This gets even worse when you consider all the ‘GOTO’ commands that send the code back on itself or jumping around all over the place. This is sometimes now referred to as ‘spaghetti code’.

To solve this, programming moved toward a more modular approach with functional programming. This sectioned off pieces of code that would be used often into stand-alone little programs called ‘functions’ or ‘subroutines’ (‘Subs’ for short).

If you created a ‘sub’ called ‘checkPlayerInput’, then you could enter this into your code like so:

10 PSET(x, y)
20 checkPlayerInput
30 GOTO 10

Now, any time we want to check the input, we just write that bit of code and it will update the x & y coordinates of the player. This is much easier and clearer for us to see what’s going on and it means that we can even reuse that piece of code in another program.

P1020320

Object Oriented Programming simply takes this modular approach to coding one step further. Now, we are using distinct bits of code in order to describe objects rather than functions. And a piece of code that describes an object is called a ‘class’.

An ‘object’ in this sense is anything that has its own variables (properties) and its own methods (commands). In this case, we might have an object called ‘player’ and this can have properties (the variables like x and y) as well as methods (like our checkPlayerInput).

Some of these properties and methods will be public. That means that they can be accessed by other classes as well. For example, if we had a class called ‘badGuy’, it would be able to find out where player was by saying:

IF player.x > bx THEN

Here, ‘bx’ is the x coordinate of the bad guy.

Better yet, if you have a class for a bad guy, then you can create lots of bad guy objects to fill your level. The class acts as a ‘blueprint’ of sorts which tells the computer how to make the data that represents the bad guy. The class is not the object, it simply describes the object. And the bad guys are not the ‘one true object’, they are ‘instances’ of objects. You’ll see them referred to as ‘this’ when you read code.

Suffice to say, that thinking of code in terms of the data and the elements that it is a much more efficient way to structure a piece of code. It looks much more complicated and is far harder to explain… but once you grasp it, you can build a piece of code out of distinct blocks rather than making one long messy piece of text.

P1020308

Hopefully, if you do move on to a programming language such as Java, having read this will provide you with the basic understanding you need to not smash your head against a wall.

If you want a nice easy programming language to get started with, then I recommend Python which makes the concept of object oriented programming nice and easy to understand – as much as it can be anyway. If you’re ready to get started with programming, then check out my introduction to Python over at Android Authority!

Conclusion

Confused yet? Fair enough… but hopefully you already also feel a little bit pleased with yourself at having grasped some of these concepts. If you ever do find yourself having to learn a programming language, hopefully you’ll now be in a much better position to do so.

Screenshot_20170414-075456

And even if you don’t ever pick up Java or Python or C#, I hope this has given you some food for thought. Could you create your own abstract shothand to help you grasp concepts at a higher level? Could you be approaching your problems in a less linear fashion? And how can you use something akin to logic gates to create other systems?

Time to try thinking like a programmer!

Subscribe Below to Stay Up to Date!

Mailing List

About Adam Sinicki

Hi there! My name is Adam Sinicki, I’m an entrepreneur, psychology graduate and amateur bodybuilder interested in fitness, self improvement, technology and transhumanism. I run an online business (NQR Productions) which allows me to live the lifestyle I want: getting time to hit the gym and to work on my projects and apps. Stick around and I’ll be sharing my experiments and adventures in brain training, bodybuilding, productivity, business and technology.