4 Fundamental Programming Concepts to Understand

4 Fundamental Programming Concepts to Understand

Programming concepts to help kickstart your development journey

Whether you’ve chosen to learn C# as I am currently doing or Java, C++, or any other programming language, there are 4 fundamental programming concepts you need to understand in order to learn and understand any programming language.

Here are the 4 concepts:

  1. Variable
  2. Data type
  3. Algorithm
  4. Method/Function

Variables

sergi-kabrera-2xU7rYxsTiM-unsplash.jpg

Wikipedia did a pretty good job of defining a variable mathematically. It says that a variable is a symbol that works as a placeholder for expression or quantities that may vary or change.

Tutorial Point also did an amazing job. It states that variables are the names you give to computer memory locations that are used to store values in a computer program.

Simply put, a variable can be defined as a container used to hold information concerning a user, event, or activity at any given point in time.

Let’s use your social media account for example. Facebook, Instagram, Twitter or even LinkedIn. When the developers first designed the applications, they decided that each user would have their own unique user information and details. As such, before logging in, each user must insert their username and password.

Now, let us assume that at the back (the code governing all the actions we see), the developer decided to call the input textbox field yourName for username and yourPassword for password. These names are what we refer to as variables.

In our case, yourName holds your username info while yourPassword holds your password info.

Every application that exists today operates using variables. Without them, we would not be able to store or retrieve any information.

This brings us to our second concept, data types.

Data Type

mika-baumeister-Wpnoqo2plFA-unsplash.jpg

If variables are containers, then a data type is simply the type of material the container can hold per time.

Data types are very important. They make it easy to classify and identify data. This means that variables can be manipulated based on the data type it contains.

The most common data types are string, integer, and datetime. More data types like double, float, and uint exist, however, we will only focus on the big 3.

Now, let us assume we have a simple application. Our application does 2 things.

  1. It requests for a user’s name and
  2. It returns the inputted value in the upper case

Meaning, if I type Fikemi as my username, the application will display FIKEMI.

Fantastic, our application worked great.

However, it should be noted that this can only happen because the input is a string. In other words, a list of alphabets.

Let's say that instead of Fikemi, I insert Fikemi9. By default, our program would convert Fikemi9 into a string before applying our upper case function. This is required because the upper case function only works with strings. Makes sense?

Awesome 👍🏼

Another great example is an application that requests for a user’s age. If the user’s age is less than 18, it tells the user that they are not eligible. However, if the user’s age is 18 or greater, it grants the user access.

Now, say we have a variable called userAge that takes in numbers (integer) and I input 17, the application will perform a check that looks somewhat like this:

{
If userAge < 18
    Console.WriteLine(“You are not eligible”);
Else
    Console.WriteLine(“You can proceed”);
}

Automatically, we know for a fact that the user will be returned the message “You are not eligible” and vice versa if the age is greater than or equal to 18.

But let us assume I input Seventeen. This becomes a problem. The major reason being that I am no longer checking an integer against another integer (17 against 18). I am now checking a string against an integer (Seventeen against 18). Hence, our application crashes. Sounds easy enough?

Great!

Now, I would like to note that we can write algorithms to help us convert the string to an integer. This allows our application to check the age regardless of being a string or integer. However, doing so will make our application run more lines of code which we probably do not really need to do. Simply stick with a single data type and perform your action.

Only convert a data type to other forms when absolutely necessary.

Now, algorithms.

Algorithms

jukan-tateisi-bJhT_8nbUA0-unsplash.jpg

An algorithm is simply a set of steps used to complete a specific task. An algorithm can be said to be the directions Google Map gives you when trying to get to your favourite pizza spot.

Turn right at the intersection, turn left at the traffic light and continue down the road for another 1 mile. That sort of thing

An algorithm simply tells your program what to do at every step of the way.

Let’s try out an example. We are going to write a mini-application right here. Our application will ask for a username and display “Welcome username”

So, if I input Fikemi, it will display Welcome Fikemi.

namespace HelloMessage
{
    public class Program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Hello, what is your name?: ");

            string userName = Console.ReadLine();

            Console.WriteLine($"Welcome {userName}");
        }
    }
}

Now, based on our mini-application, you could say that the flow of our application or the steps taken in our application are:

  1. Ask the user for their name – Console.WriteLine("Hello, what is your name?: ");
  2. Store the user’s response - string userName = Console.ReadLine();
  3. Display the user’s response - Console.WriteLine($"Welcome {userName}");

Method/Function

markus-spiske-AaEQmoufHLk-unsplash.jpg

Methods or Functions are a set of instructions bundled together to achieve a specific outcome. Functions allow you to perform actions without repeating the same block of code every time you need that action performed.

Let’s use our Welcome Fikemi for example. Say we wanted to ask for my first name and last names separately and display them separately, how would you do that.

You’d probably be thinking of doing something like this:

namespace HelloMessage
{
    public class Program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Hello, what is your first name?: ");

            string userFirstName = Console.ReadLine();

            Console.WriteLine("Hello, what is your last name?: ");

            string userLastName = Console.ReadLine();

            Console.WriteLine($"Your First Name is {userFirstName}");

            Console.WriteLine($"Your Last Name is {userLastName}");
        }
    }
}

However, what happens when you need to ask more people the same question?

This is where methods come in. Take a look at the code below:

namespace HelloMessage
{
    public class Program
    {
        public static void Main(string[] args)
        {
            string textFirstName = "First Name";
            string textLastName = "Last Name";

            RequestMessage(textFirstName);

            DisplayMessage(textFirstName, Console.ReadLine());

            RequestMessage(textLastName);

            DisplayMessage(textLastName, Console.ReadLine());
        }
        public static void RequestMessage(string userDetailRequired)
        {
            Console.WriteLine($"Hello, what is your {userDetailRequired}?: ");
        }
        public static void DisplayMessage(string userDetailRequired, string userDetailInputted)
        {
            Console.WriteLine($"Your {userDetailRequired} is {userDetailInputted} ");
        }
    }
}

The methods used to replace the request and display of First Name and Last Name are the RequestMessage and DisplayMessage Methods respectively.

Developing your applications this way allows you to make changes easily without breaking your code.

For instance, if we are advised to change the message from Your First Name is Fikemi to something like “First Name - Fikemi”. Using the first way of writing, you’d have to change the sentence structure everywhere it is used. However, using methods, all you must do is change the sentence structure within the method, and you are fine.

And that my friend is the last concept I would be talking about.

These will not be the only concepts you will learn in your development journey. They are however foundational to growing as a software developer. Classes and objects might be one of the other concepts you would learn. However, this is dependent on your deciding to follow the path known as Object-Oriented Programming. This programming paradigm is totally optional, and a lot of work can still be done using only functions and variables.

Nonetheless, I think it’s worth learning.

So, till next time, keep going after the life you want to live. Keep growing, learning, and evolving.

Peace ✌.