Last Sunday, during the Happy Hours event of Tech Coach Circle, a few participants expressed the sentiment - “I cannot read/write code. But I want to be able to do that.”

If you are the type who wants to understand software code but see Egyptian hieroglyphs when you see any code, this post will help you to read code, with some practical code examples. This may help you to write code too but, the intention of this post is to help you read code that is already written by someone else.

Outline of this post

Code is a new language

If reading code feels like reading an alien language, that is because code is an alien language.

Code is a language that is not meant to be spoken to humans, not even by humans among themselves. Code is a language meant to be spoken to a computer.

Emotional Response - New Language Vs. Code

Let us say you visit an exotic foreign country, exotic because they don’t speak English :) How would you feel if a local of that country handed over a poem written in their local language and asked you to read it?

  1. You will feel challenged because the task at hand is beyond your skillset.
  2. You will feel anxious about being judged because there is a small chance that the local person may laugh at you.
  3. You will feel (a little bit of) guilt, because that ugly voice in your mind will wake up and say, “I told you! Before reaching there, you need to learn their language, at least the basics! But, yeah, you wasted all your time in the last few months by binging on Netflix.”

These very same emotional states are experienced by people who read code for the first time. These feelings are amplified, when it is not a vacation but your daily job. Being around professional coders who can read and write code fluently does not help either. They are a constant remembrance of your inability to comprehend code.

Keys to overcoming negative emotions

There are a few mindset hacks/facts that can help you overcome these negative emotions.

  1. Native people read the language like a ‘pro’ because they have been practicing it since childhood. Similarly, your team’s engineers read code like a ‘pro’ because they have been practicing it for the majority of their professional careers. You can practice and get there too. Reading code is not a talent that people are born with.
  2. There is very little perceptible difference between the reading skills of a 20-year-old native speaker and a 30-year-old one. Similarly, an engineer with 15 years of experience does not read code any better than an engineer with 5 years of experience. (They may make cleverer engineering choices, but that’s not what we are talking about here.) This means that you just need to get enough code-reading practice to reach the average level.
  3. Professional coders don’t readily understand each others’ code more often than not. They also feel anxious about being judged if they can’t understand the code of their team member. They handle it better than non-coders because they recognize that it is just a matter of spending time and enough practice to know what a particular code does.
  4. It is much easier to learn to read code than a human language. Here are the reasons for it.
    • Human languages have a rich vocabulary. Computer languages don’t have a huge vocabulary. There is only a handful of keywords each computer language has. As long as you understand most of them, your vocabulary is great!
    • In human languages, the same word can have different meanings based on context. However, in computer languages, the keywords mean the same no matter the context. So, there is no need for guessing the meaning based on context either.
    • Human languages have complicated grammar. Computer languages have a simple syntax that defines what is allowed and what is not.

The Fundamentals of Reading Code

All major programming languages have a few common and fundamental core concepts. As long as you understand them, you can read most languages without much effort. Here’s a short list of more important ones.

  1. Constants
  2. Variables
  3. Operators
  4. Conditionals (if, switch)
  5. Loops (while, for)
  6. Functions
  7. Classes
  8. Objects

In this post, I will cover the first 4 concepts, along with some example code. The other concepts will be described in a future blog post.

1. Constants

Constants have a value, that cannot be changed. They can be of multiple data types. For example,

  • 2 - This is a numeric/integer constant
  • 2.123 - This is a floating-point constant, denoting fractional value.
  • true and false are boolean constants. Boolean is something that has 2 possible values like true or false
  • 'a' is a character constant. This has only one letter.
  • "hello" is a string constant. In few programming languages, you can write it even with single quotes like 'hello'

2. Variables

You can think of variables as empty boxes with names. The boxes can hold a constant value.

From now on, I will give you 2 example sets for code. One for Java and another for Javascript, so that you can analyze how similar they are, and also how different they are in specific topics.

// This is Java example
int a = 3;
System.out.println(a); //This will print 3

boolean isMajor = false;
System.out.println(isMajor); //This will print false

String name = "Ram";
System.out.println(name); //This will print Ram

int b = a; // you can assign a variable's value to another variable too 
System.out.println(b); //This will print 3

The above code examples are in Java.

In the case of Javascript, you don’t need to specify the data type of the variables.

// This is Javascript example
var a = 3;
console.log(a); //This will print 3

var isMajor = false;
console.log(isMajor); //This will print false

var name = "Ram"; // This can also be written in single quotes in Javascript
console.log(name); // This will print Ram

var b = a; // you can assign a variable's value to another variable too
console.log(b); //This will print 3

3. Operators

Operators are the symbols that do some operation on one or more values. Some commonly used operators are +, -, *, /, %, &&, ||, >, < ,etc.

Here are some code examples in Java.

int a = 4;
int b = 3;
int c = a+b;
System.out.println(c); //This will print 7

//You dont need to always assign the value of operators to variable
System.out.println(a+b); //This will also print 7

System.out.println(a-b); //This will print 1

System.out.println(a*b); //This will print 12

System.out.println(a+b-c); //This will print 0 since 4+3-7=0

boolean isAGreaterThanB = a>b; //evaluates to true
boolean isTodayASunday = false;

System.out.println(isAGreaterThanB && isTodayASunday) //Prints false since True AND False = False
System.out.println(isAGreaterThanB || isTodayASunday) //Prints true as True OR False = True

Now, here are the Javascript equivalents.

var a = 4;
var b = 3;
var c = a+b;
console.log(c); //This will print 7

//You dont need to always assign the value of operators to variable
console.log(a+b); //This will also print 7

console.log(a-b); //This will print 1

console.log(a*b); //This will print 12

console.log(a+b-c); //This will print 0 since 4+3-7=0

var isAGreaterThanB = a>b; //evaluates to true
var isTodayASunday = false;

console.log(isAGreaterThanB && isTodayASunday) //Prints false since True AND False = False
console.log(isAGreaterThanB || isTodayASunday) //Prints true as True OR False = True

4. Conditionals

Conditionals are the keywords that evaluate a condition and change the code’s flow accordingly.

if is a conditional present in every programming language. The way we write may be different in each language but the concept is the same. Here is a Java example with if.

int age = 2;
if (age > 20){
	System.out.println('Take a beer!');
} else {
	System.out.println('Take a soda!');
}

The same code in Javascript looks like this:

var age = 2;
if (age > 20){
	console.log('Take a beer!');
} else {
	console.log('Take a soda!');
}

You can also put one if condition inside another one. For example, in Java,

boolean didTheyVote = true;
int age = 30;

if(age>18){
	if(didTheyVote){
		System.out.println("You are a model citizen");
	} else {
		System.out.println("You need to vote now");
	}
} else {
	if(didTheyVote){
		System.out.println("How did they allow you to vote?");
	} else {
		System.out.println("You need to wait until you are 18 to vote");
	}
}

In Javascript, the same code will look like this:

var didTheyVote = true;
var age = 30;

if(age>18){
	if(didTheyVote){
		console.log("You are a model citizen");
	} else {
		console.log("You need to vote now");
	}
} else {
	if(didTheyVote){
		console.log("How did they allow you to vote?");
	} else {
		console.log("You need to wait until you are 18 to vote");
	}
}

Another conditional keyword is switch. This is used for a predefined set of values. Here is a Java example.

int numberOfSugarPackets = 3;

switch(numberOfSugarPackets){
	case 1:
		System.out.println("Dispensing 1 sugar packet.Sweet!");
		break;

	case 2:
		System.out.println("Little high sugar. But no harm");
		break;

	default:
		System.out.println("Preparing for bad health, eh? No sugar for you");
}

//This program will print - Preparing for bad health, eh? No sugar for you

Here’s a Javascript version of the same

var numberOfSugarPackets = 3;

switch(numberOfSugarPackets){
	case 1:
		console.log("Dispensing 1 sugar packet.Sweet!");
		break;

	case 2:
		console.log("Little high sugar. But no harm");
		break;

	default:
		console.log("Preparing for bad health, eh? No sugar for you");
}

//This program will print - Preparing for bad health, eh? No sugar for you

This brings us to the end of basic topics. In the next post, I will continue on this and explain a few more elements of programming, along with a few mindset hacks too.

Practice Exercises

  1. Try reading this code and make sense of it. Ideally, you will be able to make sense of only few lines. If you come across a line that you don’t understand, ignore it and proceed to next line. You will be surprised how much you understand, if you have the will to ignore and proceed to next line :)
  2. Now, read this code
  3. Still with me? Now read this code.

Hopefully, this was fun! See you in my next blog post about a few more fundamentals.

Hey 👋 liked the stuff here?

Subscribe to updates from Tech Coach Circle here.

Updated: