I learned programming relatively late compared to most of my peers. It was only in college that I dabbled in it seriously and decided this is what I wanted to do for life.
Personally, I think everyone should learn a little about computer programming. Not because you could build an app on your own, although that's a great skill to have, but to also learn some useful concepts that is beneficial to your everyday life.
Let’s take a look at some basic concepts in the Ruby programming language and how they could be adapted to improve your life.
What’s in a name?
In programming, we have the concept of variable that allow us to store value and act as a reference to that value for future usage. The variable is both a name for the value and the location of where the value is stored in the computer’s memory.
Let’s say we want to calculate the circumference of a circle and if you remember your math (I don’t, I just Googled), the formula is '2 x pi x radius' of the circle. The value of pi is roughly 3.142 and for this case, our circle radius is 5. This is what it could look like in code.
pi = 3.142 radius = 5 2 * pi * radius => 31.419999999999998
We start by assigning the variables pi and radius the value of 3.142 and 5 respectively. This will allow us to reuse them for future calculations and to also convey their meaning. Now if I wanted to calculate the area of the circle, I can reuse both pi and radius.
Note: Formula to calculate area of a circle is 'pi x radius squared'.
pi * radius * radius => 78.55
Naming “things” is a big part of human culture. By assigning names to certain ideas, concepts or things, we are able to communicate more efficiently and effectively with each other. Names also makes it easier for us to recall and remember complex ideas and concepts. (e.g. Toothbrush Theory)
Build a functional life
Variables allow us to reuse values, but what if we want to reuse a certain computation instruction? For example, if I wanted to calculate the circumference for a circle with a different radius, I have to pretty much repeat what I did before.
pi = 3.142 radius = 7 2 * pi * radius => 43.988
Functions or methods in programming allow us to build reusable computation routines. By moving the computation into a function, I’m able to reuse it later with different inputs. Think of a function as a machine that is programmed in a certain way. Given the same inputs, it will always produce the same result. Tweak the inputs and you’ll get a different output.
Let’s define a function that takes a single input (radius of the circle) and then return the circumference of the circle.
def circle_circumference(radius) pi = 3.142 2 * pi * radius end circle_circumference(5) => 31.419999999999998 circle_circumference(7) => 43.988
With the function circle_circumference, I’m now able to reuse the formula over and over again. All I had to do is pass in a different input (circle’s radius) and it will return the desired output.
Computer programs are basically made of functions that does all sort of things. Functions can also call or execute other functions to further improve reusability.
In our daily lives, there are many actions and routines that we perform regularly and repeatedly. Like functions, our brain has turn them into habits so we can execute them efficiently and frequently. (sometimes without us realising). It may be wise to look into the various functions you have programmed into your life.
Adapt to changing conditions
By changing the inputs of a function, we are able to modify its output. But what if we only want to accept certain inputs? For example, the radius of the circle must be a positive number. In our previous function, if we passed in a negative radius, it will return us a negative circumference, which doesn’t make sense.
circle_circumference(-5) => -31.419999999999998
To solve this problem, we use the if-else conditional construct to handle the different types of inputs. Our new function will first check the value of radius. If it’s smaller than 0 which means it’s a negative number, we will return an error message. Else, we will perform the calculation as before and return the result.
def circle_circumference(radius) pi = 3.142 if radius < 0 "Error: radius should be a positive number" else 2 * pi * radius end end circle_circumference(5) => 31.419999999999998 circle_circumference(-5) => "Error: radius should be a positive number"
With this new change, the function is now able to adapt to the inputs given and return the correct result. The if-else construct allows our programs and functions to make decision based on conditions we specified.
Knowing change is the only constant, all of us should consider ever-changing circumstances and adapt our plans accordingly. If you don’t want to be caught in an unexpected situation, it is best to prepare for it. Else, you may not get your desired result.
Calculating a single circle’s circumference may seem useful but the real power of the computer is to be able to perform calculations repeatedly with ease.
Let’s say we want to calculate the circumference for 3 circles with different radiuses. Using our previous function, we can repeatedly call the functions with different inputs.
circle_circumference(5) => 31.42 circle_circumference(7) => 43.988 circle_circumference(9) => 56.556
When writing code, we should aim to reduce repetition and let the computer do that instead. So let’s rewrite the code by employing a loop construct. In programming, the concept of loop enable us to execute one or more instructions repeatedly.
We start with assigning the variable radiuses with a list of circle radius. Then we use each (a type of loop) to iterate through the list and execute the circle_circumference function with the different radiuses.
radiuses = [5,7,9] radiuses.each do |radius| puts circle_circumference(radius) end => 31.42 => 43.988 => 56.556
Both code may look similar in length but if we were to increase the list to 10 radiuses, the first example will be much longer while the second one remains the same. Loops help reduce repetition in our code which in turn reduces errors.
By identifying repetitions in your life and replacing them with systems and processes, you will be able to free up precious time. Personally, I chose to outsource a bunch of decisions in my life to others so I can spend time on things that matters to me.
Start programming your life
Armed with these concepts, you can now start improving your life by automating repetitive tasks and installing good habits. Leverage the power of names as shortcut to complex ideas and always consider different situations in your plan.
I hope this brief introduction have sparked your interest in computer programming and if you are looking for resources online to get started, here are sites you can begin with.