How to be polyglot programmer? - Part 1
People ask me every time that how I know so many programming languages or how much does it take to learn a new programming language and build something with it or get a job in it. I face this cause where ever I apply or see a job opening I say I can apply in that be it Node.js, Python, Java etc.
This article is first in series where I explain what I learned from years of coding (since class 5 - 2005) and till date now (Jan 2020) and how I manage to learn so many programming languages, got a job, worked on 2 to 3 programming languages in industry and still learning.
Let's start with basics with brief definition of a programming languages
Set of instructions for computers in human readable format
These instructions have a syntax, special keywords and have a particular set of ways of writing your own logic in it that a computer program understands (compilers, interpreters or transpilers) and convert them to a format a computer understands maybe machine code, assembler code, bytecode etc.
So, what a programming constitutes from a developer perspective. Here I am not talking about different parts of whole toolset like compilers etc. I am talking about only language and I find following:
- Primitive types
- User Defined Functions
- User Defined Types
- Standard Library
I will show similarities between multiple languages. This article will be divided into multiple parts as I will discussing all above one by one. So, let's start
When writing program you need to store values in memory. Variables are containers for those values. For example if you want to add two numbers you need to store them somewhere so that CPU can perform operation on it and store the result. A language compiler can have different implementation for storing those variables in memory but for higher level they seems all same.
Variables can be of basic two types
- Variable - you can change value of this container during program is running
- Constants - as name implies you cannot change value during program is running
Why do we need something which will not change during program running?
There are many reasons for it specially in large projects. A small example of constant can be
pi whose value will never change as it's a universal constant and defining it makes easy for developers who are working in same project, so that everyone uses same value when calculating result and have no discrepancy. Also there are benefits regarding performance also.
Let's have some examples
int a = 1; char b = '2';
As you can see three languages here have same syntax for declaring variables.
val a = 1; val a: Int = 1;
var a = 1 var a: Int = 1
var := 1 var a int = 1
let a = 1 let a: number = 1
let a = 10 let a: i32 = 10
var a = 1; int a = 1;
a = 1 //or a :: Int a = 1
In all above programming languages you see that there are mainly two ways of declaring variables, first let compiler/interpreter/transpiler decide which type of value it is or explicitly assign a variable type during declaration. Syntax are different but concepts are same which can be interoperable between languages.
var a = 1 let a = 2
a = 1
a = 1
// python a = 1 a = 'a'
It's completely valid where as
//c int a = 1 char a = a
Is invalid as first compiler will throw error that variable has been declared and if then also if you can
char a to
char b it will throw error that int type cannot be assigned to char type and here is basic difference between statically typed and dynamically typed languages.
But statically typed languages where type is determined by compiler it is possible (when you don't explicitly declare variable type)
//kotlin var a = 1 var a = a
Most languages have common constant syntax like adding
final keyword before declaring variable. Whereas
final can have different meaning in different languages.
const int a = 1;
final int a = 1;
const var a = 1;
In python you can't declare constant. Max to max there is
Final in Python 3.8 but it doesn't stop you from reassignment just warning if using mypy.
let a = 1
Swift uses let to assign constants.
const a = 1
final a = 1; const a = 1;
final keyword refers that value can't be assigned where is
const refers to value which cannot changed during compile time. For example,
5 is a compile time constant where as
DateTime.now() is not.
Haskell and Elixir being functional languages so there is not keyword for constants but it can be done, we are developers and we find solution but I don't want to discuss it in this article as it would need it's own article being a functional programming language.
As you can see there are many similarities when it comes to variable declaration in languages, most of them uses similar syntax just throwing keywords before or after or not all when declaring. Those keywords are called data types when it comes to variable declaration and this is our next topic.
Primitive Data Types
Every programming language have some basic data types defined by compiler, interpreter or transpilers, in code above you have seen
int those are data types.
Data types are important in programming cause while running program has to know how much space should be allocated in memory to store that data as each data type takes up different amount of storage in memory like in C++ int takes up 4 bytes in 64 bit processor but when this same compiler compiles for 32 bit will make it 2 bytes to optimise it according to processor so that it can be easily stored in processor register and next one is available fast.
Basic data types
- Integer (Non decimal numbers)
- Floating point (Decimal numbers)
- Boolean (True or False)
Some programming languages provide more primitive data types such as
byte in Java and Golang.
short in Java is used to store as name implies long numbers while short for small numbers both of them having specific range, whereas in C/C++ these keywords are modifiers for data types works similarly.
double to store big floating numbers in Java and C++ where
double is a type in Java and modifier C/C++.
There are programming languages where there is subtype of integer or float. Like in Rust or Golang, provide data types of different sizes with signed or unsigned specified. Example,
i8 are same to store 8 bit integer and
uint8 are same to store 8 bit unsigned integer. This eliminates using modifiers like
unsigned keywords before variable declaration in C/C++.
float to numeric type which is similar in Python also.
Some might be saying I didn't include
char name = "hashnode";
Creating array of characters and storing a string. Learning like this is useful as it helps you to learn concepts of programming like reversing a string, finding index of character of long string, checking two strings are similar or not.
Now there are similarities in all these programming languages, learn one and use can use it in other, you have to do extra step to look documentation what are different data types available in that particular programming language.
Hope you found this article/post helpful, in next article we will discuss about keywords and how they are similar in different programming languages and small differences between them.
Happy Learning, Happy Coding.