Beginning with Programming!

Riya Jain
Geek Culture
Published in
10 min readOct 17, 2020

--

Programming is an art that cannot be learned in a year or so. You need to practice it continuously and regularly to be pro at it. But, before becoming a pro, some basic fundamentals and foundations are important. This guide will tell you about some of the efficient practices to use while programming.

Sounds exciting? Let’s get right into it!

1. What is programming?

I won’t spend too much time here since if you’re reading this, you likely have a pretty solid idea of what programming and coding is.

It’s just a formalized version of the logic, in the vocabulary and syntax of the language that a machine can interpret. “I want to see all the rows in this database table” becomes SELECT * FROM table. It’s the implementation level of tech. So, to get better at programming, you’ll be writing a lot of code!

2. Why to learn Programming?

If you came up with the answer simply by reading this section’s headline, that’s a really good sign. One of the biggest factors why people don’t follow through on learning any skill is because their “why” is either non-existent or ambiguous.

Know what’s your why. Major ideas for learning programming are: Get a better job, be happy, have an impact, become financially secure, discover new hobbies or passions, etc. Whatever you come up with, drill down on the details. Remember, the clearer your answer, the higher your motivation will be for learning programming or any other skill.

3. How to practice Programming?

Some skills can have a higher ratio of knowledge v/s application. This is NOT the case with programming. Programming is one of the skills where you need to practice a lot more than to focus on the theory.

I will recommend you to spend at least 80% of your time writing code and 20% learning the theory. As you learn more and more, I’d suggest increasing the practice ratio. In short, every minute you spend trying to understand the concepts, you should spend about 5x more time on putting it into practice. That’s how the concepts will sink into your brain and start making sense; not by re-reading it multiple times.

Remember: It’s not just about typing out code. It’s also about fixing bugs, maintenance, security, testing, user experience, architecture, systems, databases, and all sorts of other things. It gets complicated. Learning to code is just the tip of the iceberg.

It’s not an easy ride, to begin with, programming, especially if you haven’t had much practice with other logical skills in the past. That’s why being good at math is often a requirement to get into a computer programming program. The math you’ll use in programming is rarely that complex, but the logic you learned by applying math greatly accelerates your programming abilities.

4. How to break the sessions?

This greatly depends on your situation. Some of you will be able to do proficient enough with a full month of practice others can’t dedicate much time at all. For most skills, I recommend practicing for at least 15 minutes per day. But for programming, that’s not enough. For programming, if you practice for 30 minutes every day, that’s 3.5 hours per week or 14 hours per month.

While everything is fresh in your mind, things will seem easy, but if you stop practicing for even just a month, you’ll lose around 60 percent of what you previously learned. The golden rule is spaced learning and repetition. And you should recall what you learned regularly enough.

5. When to practice Programming?

Just one-word answer — “Every day

The more you can make your skill practice a habit, the easier it will be to follow through and get results. It’s always more motivating to focus for 30 minutes, for example, than to focus on writing a complex piece of code. But since programming is a complex skill, make sure to practice when your mind is sharpest.

While most programmers proclaim to be night owls, research shows that most people have their minds sharpest shortly after waking up. If you’re unsure when’s the right time for you, I’d start with the early morning. If it doesn’t work right away, please don’t give up just try a different time. New habits take time to form.

Practice using different languages, frameworks and IDEs. Try pair programming. Do functional programming, OOP, component-based programming. Try scripting or ML. Code in your room, kitchen, at cafes, school, outside, etc. Try with different types of music. There’s an infinite combination of things you can do!

6. Basic concepts of Programming

Truth be told, the skill of programming is too broad. If you want to learn “programming”, you’ll lose motivation quickly. It’s overwhelming beyond imagination to view it as a single entity! As such, we’ll break it apart into the following sub-skills:

6.1 Boolean and conditional logic

A boolean expression is something that evaluates to a binary value of true or false. In most languages, these are evaluated by using these equality signs. Suppose, var a = 3then print(var)will give 3 as the output. But print(var == 3)will print true.This is because var == 3is a boolean condition that compares the value of var with 3 and then returns the output.

Conditional logic is the evaluation of a boolean expression. Basically, if this is not donethen that. A conditional expression has multiple parts defined by and/or. In most languages, “and” is defined by && and “or” is defined by ||.

You can also group boolean expressions with ( and ). if (a || (b && c)) then d. if (kick the imposter || (task completed && crew members are alive)) then crew will win.

As you can probably imagine, this can get really complex. In this article, I’ll show you how good programmers keep this easy to understand for anyone reading their code.

6.2 Variables

To keep it really simple, variables are something named that holds a value. Example:

int numImposter = 1;
string imposterColor = "Black";
bool isImposter = true;
string[] crewColor = {"Cyan", "Brown", "Black", "Green", "Blue"};
float deadCrewmates = 4.0;
int[] kickVotes = {2, 1, 3, 0, 1};

Good programmers will use variables in a descriptive way instead of “hard-coding” values. Here’s an example of “hard-coding” a value:

if (imposterKicked < 1) {
// Continue the game
}

What does 1 mean here to someone not familiar with the running game or code? Nothing. Now, if we use variables instead, it’s a lot clearer:

if (imposterKicked < numImposter) {
// Continue the game
}

We could similarly write code withcrewColor array:

if (deadCrewmates < (crewColor.length - numImposter + 1)) {
// Continue the game
}

6.3 Loops

What is the sum of the following numbers: 2, 6, and 1? Fairly simple, right? How did you do it? Something like this:

2 + 6 = 8
8 + 1 = 9

In your brain, this required two iterations. In this very simple example, we know how many numbers we have and what their values are. That is rarely the case in programming.

As briefly mentioned in the previous section, you have the concept of an array. You don’t know how many values there are and what the values are. Let’s say you want to write an algorithm (method) that will work in any scenario to count the sum of votes that have been polled to kick out someone? The answer is with a concept called loops. It’s ultimately not different at all from what you did in your head above. Here’s what the code would look like:

int[] kickVotes = {2, 1, 3, 0, 1};
var totalVotes = 0;
for (var i = 0; i < kickVotes.length; i++) {
totalVotes = totalVotes + kickVotes[i];
}
print totalVotes;

That last line would print 7 on your screen.

A lot is going on in the code above. A for loop goes through each value of an array one by one, from one index to another.

In the example above, we started from the index 0 (see var i = 0;). In most programming languages, an array’s first index is not 1, it’s 0. Our range of indices in the above case is from 0 to the number of values present in the array, indicated by kickVotes.length. Essentially, var i = 0; i < kickVotes.length; i++ means that the algorithm is going through each index one by one (i++), starting from the first index until the last.

So, there are three components of a for loop: starting index; ending index and number of steps per iteration i.e. for (startIndex; endIndex; numSteps)

kickVotes[i] means the value at the index the loop is currently at. If you replace i here, by kickVotes[3], the value you’ll obtain is 0. The for loop above basically performs this: var totalVotes = kickVotes[0] + kickVotes[1] + kickVotes[2] + kickVotes[3] + kickVotes[4]

for is the most common loop. You’ll also regularly find the while loop. It means “do something until I’m done”. Example: Count the votes until there are no more votes left that have been polled. Let’s say we want to print the votes each mate got in the game:

int memberIndex = 0;
while (memberIndex < kickVotes.length) {
print kickVotes[memberIndex];
memberIndex++;
}

What do you think will happen here? You’ll see this printed on your screen:

2
1
3
0
1

You’ll see all the 5 values printed. The code in the while loop will keep executing until the condition is met: memberIndex < kickVotes.length, i.e. — we reach the end of array!

6.4 Functions

Functions make code pieces reusable. Remember the for loop above to calculate the sum of total votes polled? How do you make it work for ANY game? Let’s make a few adjustments:

function countVotes(kickVotes) {
var totalVotes = 0;
for (var i = 0; i < kickVotes.length; i++) {
totalVotes = totalVotes + kickVotes[i];
}
return totalVotes;
}

You’ve created your first function/method! This, on its own, actually does nothing. Here’s how you would use the function:

int[] kickVotes1 = {2, 1, 3, 0, 1};
int[] kickVotes2 = {0, 3, 1, 5, 0, 0};
print countVotes(kickVotes1);
print countVotes(kickVotes2);

This would print 7and 9 on your screen

7. Good programming practices

The basics above help you get started, but you won’t be a very good programmer yet with those. Below, I’ll explain some ways to go from noob to good in a short while. This section is the most complex one, so if you can’t follow along currently, don’t forget to bookmark to revisit it later when your mind is sharpest!

7.1 Clean code

Let’s say you’re working on a 2D platformer game in Unity. You want to add simple range detection to decide when an enemy should attack the player. A regular programmer may write code that looks like this (if not using colliders):

void Update() {
if (Mathf.Abs(transform.position.x * 100.0 - player.transform.position.x * 100.0) <= 10.0) {
isAttacking = true;
animation.SetAnimation(0, "attack", false);
}
}

Did this terrify you? Don’t worry, it should. Writing clean code is about making it so easy to understand that you’re simply reading English. The above example is extremely simple, yet we can do much better. Here’s one way a more advanced programmer may write it:

void Update() {
if (IsCloseToPlayer())
Attack();
}

bool IsCloseToPlayer() {
return (PositionDifference(transform, player.transform) <= ATTACK_DETECTION_RANGE);
}
float PositionDifference(Transform t1, Transform t2) {
return Mathf.Abs(GetHorizontalPosition(t1) - GetHorizontalPosition(t2));
}
float GetHorizontalPosition(Transform t) {
return t.position.x * PIXELS_PER_UNIT;
}
void Attack() {
if (isAttacking)
return;
isAttacking = true;
int animationTrack = 0;
bool loop = false;
animation.SetAnimation(animationTrack, "attack", loop);
}
const float PIXELS_PER_UNIT = 100.0;
const float ATTACK_DETECTION_RANGE = 10.0;

What’s one of the first things you’re noticing? “There’s a lot more code!!”, right? Did it take me more time to write? You bet! But here’s why it’s better than the first block of code.

7.2 Abstraction and Encapsulation

Hide implementation details: When someone else (or future-you) reads your code, they want to understand what’s going on first before jumping into the details. Sometimes, the details are not that important.

If you look at the Update() function of the second code block again:

void Update() {
if (IsCloseToPlayer())
Attack();
}

It’s simple to read, right? Especially next to the first version you previously saw. If you read the previous code block, it will take you a lot more time to understand that it means: “if I’m close to the player, I should start attacking”. Creating nicely named bite-size methods makes your code a lot more readable.

7.3 Name each fragment

The thumb rule while writing clean code is naming. The programmer spends 90% of the time while thinking of a suitable name for the code, be it any boolean condition, function name, etc.

For example:IsCloseToPlayer() is a lot easier to read than Mathf.Abs(transform.position.x * 100.0 — player.transform.position.x * 100.0) <= 10.0

7.4 Stop using comments

Most good code doesn’t need comments to explain the logic because variables and functions are all named properly. If you need a comment to explain a block of code, chances are you can create a well-named method instead. So, think before you are writing a comment.

7.5 Function decomposition

Before actually attempting a problem, try to decompose it into several small problems and create a function for each. This will enhance readability as well as clarity of the code.

7.6 Rule of 7

How many lines of code should a function/method have? Here’s a simple answer: 7 ± 2. The reason for this is that our brain only can work on about that many instructions at the same time. I cringe whenever I see methods that have more than 9 lines of code.

7.7 Generalisation

Try not to hard code the values. The more the code is generalized, the more it is re-usable. Good programmers practice making their code more readable by making it generalized.

For example, in the code we used above for finding out the total number of votes, we used the length() method to find the sum. Instead, if we have used a constant value like 5, then it won’t be re-usable for some other array of different sizes.

8. Getting stuck

Being stuck somewhere is a skill in itself because without committing any error, you won’t learn it. As you keep learning, you’ll frequently come across stackoverflow.com or similar websites. When you Google how to do something in programming, chances are someone else had the same issue as you did and someone else gave a clear answer and how to fix it.

There’s no shame in not knowing every answer. There’s simply too much to programming to know everything. In the programming world, you’re always expected to be resourceful and figure out answers by yourself first — which is a great way to learn anything. If you can’t figure it out after trying, then is the time to seek out help from others, either in person or by asking on StackOverflow.

9. Final Tips

Compare different codes. Try to understand which one is more efficient in terms of complexity, readability, and generalization. Nothing is final. Always, try to make a more efficient code than your earlier version. Don’t just learn to code, learn how to become a developer. Learn to tackle the bugs.

Relax. It’s not a race, there’s no rush. You’re not “falling behind” anyone else by not knowing things if you’ve only just started. You wouldn’t write a novel when you’ve only just finished learning the alphabet. So, just enjoy it.

Have fun!

--

--

Riya Jain
Geek Culture

I am a B.Tech. Undergrad and loves programming. Scholar at Google Women Techmakers and currently exploring different tech fields.