Spread the love

So you want to know what it takes to become a programmer? Well that is what we will cover in this post, first off it is important to note that this post is going to show you only the basics and it is not going to focus on any one programming language, the tings covered in this post are of a more general nature the topics I am going to cover are the following.

So, there are a lot of ground to cover so let us get started, and remember to check out the youtube video in the bottom of the post 😊

Data Types  

One of the first things that you need to get used to when it comes to programming languages are what we call data types, almost all modern programming languages are what we call type safe languages, the strength behind this is that you always know just what kind of data there is stored in your variables, now even if the language is not type safe on the surface like for example PHP or JavaScript behind the scenes it will still convert your variables to these common data types, and in almost all programming languages you have the ability to type convert your generic variables into common data types, so what are these common data types? Well let us take a look, now remember these are the most common types, but every language might have their own ones besides these, but if you understand these common types you can pretty much use them anywhere.

String

This data type can hold text

string myString = "This can only hold text";

Integer / int

The integer can only hold whole numbers and not numbers with decimals, in some languages you might have to use the whole type name and in others you can use the short hand int. The integer can hold a number from between -2147483647 to 2147483647 when you talk about a 32 bit integer, while you can use a long if you need a larger number.

int myInt = 2147483647;

long myLong = 9223372036854775807;

Float

If you wish to store a small decimal number then float is the way to go it can store up to 7 digits of precision.

float myFloat = 3.402823466f;

Decimal / double

In most languages you do not use floats anymore, but we use decimal or doubles, the reason is that it can store a much larger number, and while back in the days you would use floats to save on data sizes in memory, that is no longer much of a problem in todays programs, however you might still see floats in game development where optimization still is factor.

double myDouble = 1.7976931348623157;

Boolean / bool

A boolean can only hold true or false values either as true or false or 1 and 0, in most languages you can use the shorthand bool for Booleans.

bool myBool = true;

DateTime

DateTime is used to hold information about dates and information about time.

DateTime myDateTime = DateTime.Now;

As I wrote earlier then every language has its own data types, but if you understand the above data types you are well on your way, to get started.

Collections

Next up we have collections, because while you might be able to write some small scripts using only data types, in most cases you are going to need a structured way to store your data so it is easy to work with, just like with the data types then every language might have there own types of collections, but the ones below should cover most cases.

Array

First off we have the Array, now you might not really see arrays being used that much any more mostly in old legacy code the reason being that arrays have some limits that other collections do not have, the largest limitation of the array is that it is a fixed size, so once you create it you cannot not change the size of your collection, which might either result in you creating too small arrays or creating too large which will make your code difficult to maintain.

int[] myArray = { 1, 2, 3, 4 };

ArrayList

To solve the size problem with arrays the concept of arraylists was introduced, and while it solved the problem of having to defined the size of the array creating it did create another problem of not being type safe and further more the way that they solved the size issue was by placing arrays inside arrays which is a very bad design choice, and because of this you will not see many arraylists however they do exist. But besides that it on the surface it looks much like any other collection where you can use .add to add elements.

ArrayList myArrayList = new ArrayList();

myArrayList.Add("Hello");
myArrayList.Add("World");
myArrayList.Add(1);

List

The list is the most used type of collection in modern development, the reason being that it is type safe and it is also generic which means that you can store objects and access them directly, they also give you powerful tools to allow you to sort your data and maintain it.

List<string> myList = new List<string>();

myList.Add("Hello");
myList.Add("World");

myList.Remove("Hello");

Dictionary

We use dictionary when we need to store data that should be easy to access, because how dictionaries are build up you have a key and a value, so if you at some point need to access some data in a collection and you know the key you can easily access that value without having to loop through the entire collection, the dictionary is the generic collection type of the hash table, but I will not cover the hash table since it is not used very often, but if you do run into it just know that it works much like the dictionary.

Dictionary<int, string> myDictionary = new Dictionary<int, string>();
myDictionary.Add(1, "Test");
myDictionary.Add(2, "Hello");
//Get value
string value;
myDictionary.TryGetValue(2,out value);

And that was it for collections, now there are a lot more types of collections, but with these basic ones you should be able to get very fare in any modern programming language.

Loops

Now that we have covered datatypes and collections, now it is time to cover something that you are going to be using a lot in your programming life which are loops, you use loops to either run through collections or to make your code run until some criteria has been meet.

For loop

The for loop is properly one of the most popular loops, because it has been around for a very long time, the idea behind the for loop is that it will run for x amount of iterations, this could be a fixed number or it could be the length of a collection. It works by you having an integer that you will then keep adding up, usually by 1 until you reach the length.

for (int i = 0; i < 10; i++)
 {
 	Console.WriteLine(i.ToString());
 }

for (int i = 0; i < myList.Count; i++)
{
  Console.WriteLine(myList[i]);
}

Foreach

While the for loop is most properbly the most popular loop then the foreach is close second, and that is because of its simplicity, but what makes this different from the for loop is that foreach must run on a collection, which is what makes it much for readable than the for loop.

foreach(string s in myList)
 {
   Console.WriteLine(s);
 }

While

The while loop will keep running util a specific criteria is meet, this would normally be if some variable is set to false, this is for example used in game development, where the whole game is running inside what you call a game loop, which pretty much is just a while loop that keeps running as long as a variable is true, but please be careful when using while loops because they can run forever, and unless your program is multi threaded (multi threaded means running many process side by side) and if not it will lock your program.

while (true)
{
  //Game loop
}

bool run = true;
int i = 0;
 while (run)
    {
    if (i < 3)
      {
         Console.WriteLine(i.ToString());
       }
       else
        {
          run = false;
         }
       i++;
       }

Structure your code

Now with all the basic things out of the way, it is time to start looking at just how you can structure your code, we are going to look at how you can write your code so that it is easier to read, reuse and maintain, we are going to talk about functions and methods, now there are some technical differences between a function and a method, but just to keep it simple we will say they are the same thing, and to be honest I do not think that you will find any programmer that will make fun or put you down if you say the wrong thing, so we are just going to call them methods 😊

Methods  

Using methods are a way for you to structure your code so that you can call it from many different places without having to write the same code twice. Methods are one of the places where the syntax can be very different depending on the programming language that you use, but in this post I am going to use C# however the structure is similar in other languages. When you define your method it will be something like this.

<access modifier> <name> <return type> (<parameters>) {  //code }

Access modifiers – this will for the most part be either be private or public, where private will only let you access the method within the same context, and public will let you access the method from anywhere in you program, or externally.

Return type – this defines what the method will return this can be any data type or object, or if you do not wish to return anything then you can write void which means it will not return anything.

public NewClass()
        {
            AddNumber(2, 4);
            WriteToConsole("Hello World!");
        }

        public int AddNumber(int number1, int number2)
        {
            return number1 + number2;
        }

        private void WriteToConsole(string text)
        {
            Console.WriteLine(text);
        }

Recursive methods

The idea behind recursive methods is one of the more complex topics in this post, but it is a practice that if use correctly is very powerful, the idea is that you create a method that calls it self and thereby creating a kind of loop, but without having a loop, but be careful when using recursive methods because they can be very heavy.

public void Counting(int number)
        {
            
            Console.Write("Counting..." + number.ToString());
            number++;
            if (number < 5)
                Counting(number);
        }

Thinking like a programmer

One think is learning the syntax of a programming language, that is the easy part the “hard” part is learning to think like a programmer, now when I write the hard part it is really not that hard, there are some very simple things that you need to understand and once you do; learning any programming language is not that different. The first thing that you must learn is to break things down into smaller parts, you might feel overwhelmed if someone told you to for example to create a new CRM system, now do not get me wrong a CRM system is a complicated system but the most important part is to break it down into as small parts; as possible, so first off what do you need for a simple CRM system? Well we are going to need to have some customers, and next we are going to need a way to create and update customers. To make a problem like this simpler most programming languages has created the concepts of classes and objects, classes and objects are a way for us as programmers to take real life things and model them in our code. So classes and objects works like this, a class is a blueprint telling you how for example you customer should look, while the object is the customer itself, so from one class you can create multiple objects.

This could look like this in code

class Program
    {
        static void Main(string[] args)
        {
            Customer James = new Customer();
            James.Name = "James";
            James.City = "Odense";
            James.ZipCode = 5000;
            James.Phone = 1111111;
            James.Email = "james@test.com";

            Customer Jill = new Customer();
            Jill.Name = "Jill";
            Jill.City = "Svendborg";
            Jill.ZipCode = 5700;
            Jill.Phone = 2222222;
            Jill.Email = "jill@test.com";

        }
        
    }

    public class Customer
    {
        public string Name { get; set; }
        public string City { get; set; }
        public int ZipCode { get; set; }
        public int Phone { get; set; }
        public string Email { get; set; }
    }

In the above code you would create two objects of the Customer class, now sometimes when you create a class you might wish that some data is mandatory and should always be added when you create the object, for this we use what is called a constructor, the constructor is a method that will always be called upon creation of an object, thereby creating a contract, so let us say that we would also want at least a name for our Customer we could do something like this.

    class Program
    {
        static void Main(string[] args)
        {
            Customer James = new Customer("James");
            James.City = "Odense";
            James.ZipCode = 5000;
            James.Phone = 1111111;
            James.Email = "james@test.com";

            Customer Jill = new Customer("Jill");
            Jill.City = "Svendborg";
            Jill.ZipCode = 5700;
            Jill.Phone = 2222222;
            Jill.Email = "jill@test.com";

        }
        
    }

    public class Customer
    {
        public string Name { get; set; }
        public string City { get; set; }
        public int ZipCode { get; set; }
        public int Phone { get; set; }
        public string Email { get; set; }

        public Customer(string name)
        {
            this.Name = name;
        }
    }

The next thing might be that we do not want to create all these objects like above but we would like to add them to a List so let us create a method for that.

List<Customer> customers = new List<Customer>();

        public void addCustomer(string name, string city,int zipCode,int phone,string email)
        {
            Customer customer = new Customer(name);
            customer.City = city;
            customer.ZipCode = zipCode;
            customer.Phone = phone;
            customer.Email = email;
            customers.Add(customer);
        }

Now there are some good practice design patterns on how to structure your code; but I will not go into that here, the main take way is that ones you start breaking your tasks into smaller parts it does not seem that scarry when you are starting on a new project.

The last thing that I will say in this post is, do not be afraid to make mistakes or ask for help, the biggest part of being a programmer is not knowing everything by hard but knowing where to find the information that you need.

Well that was it for this post it was a long one but I hope it inspired you to get into programming and please also check the video below where I also cover this topic, and until next time stay safe.

Leave a Reply