And think like a programmer
Disclaimer: This is not a tutorial on how to code using a specific programming language. Rather, this is a guide for someone studying (or willing to study) a programming language on how to start or from where to start in order to learn the language efficiently and thus be able to think like a programmer.
Learning and studying programming could be tricky sometimes. The key point for learning programming is to be able to think like a programmer.
This is the first thing you need to learn, understand & grasp
In my opinion, the first thing you need, before getting further into any other details, is to have a good understanding of the data types and structures used in the language you’re learning.
The main common data types/structures between all programming languages are: integer, double (or float), boolean, string, character, array and object. Python, for instance, has in addition, list, tuple, dictionary and set.
Understanding how each language stores information will enable you to get a feel of what data type you will need to create to store each variable for the problem you’re implementing. For example, if you’re creating a program that calculates the user’s BMI (body mass index), and stores the result along side the user’s name and age, you can easily observe that you will need a string to store the user’s name, a floating variable to store his weight and height (needed for calculating the BMI), and an integer to store his age.
Next you need to learn the fundamental statements, logical, relational and arithmetic operators, and array indexing
If elseif else, for loop, while loop (including do-while loop if supported by the language), and switch-cases. By understanding the concept and logic of each of them, you will be able to know how, why, and when you will be using each statement in your code.
These statements will constitute a huge portion of any code that you will be writing, so it’s important to have a good understanding of how they work.
While learning how to use these statements, you will definitely need to learn the basics of conditional operators, as all the statements, except switch-cases, work depending on a logical condition being satisfied or not. The main logical operators are AND (&&), OR (||) and NOT (!) (note that the logical operators syntax I wrote are for Java, C++ and C#, for instance, but are not necessarily the same for other languages). In addition to logical operators, relational operators will also be handy for creating conditions. For example, you might want to check equality (==), greater than (>), less than (<), greater than or equal (>=), less than or equal (<=) and inequality (!=).
For some simple mathematical operations in your code, these are the basic arithmetic operators that you will need: addition (+), subtraction (-), division (/), multiplication (*), and modulus (%).
Array indexing is the technique used to access elements of an array (works the same for lists, tuples, and dictionaries in Python). It is very important to understand how it works, and it’s going to be very useful when using loops.
Get experience with some useful built-in functions and writing your own functions
There are many built-in functions or methods which are useful for data manipulation. These will prove helpful in many cases and will save you time, reduce complexity, and you will be able to do what you intend to do with just a single line of code.
In addition to built-in functions, it is very important to know how to create your own. Functions will be useful in situations where you will find yourself using the same block of code at different places in your program. Therefore, instead of just copying/pasting the code several times, create your own function which will complete the set of tasks required, and call the function in your program whenever needed. This will save you time, space, and will make your code much neater, readable and understandable.
Understanding Object-Oriented Programming
Today, most programming languages support object-oriented programming (OOP). OOP is a model which is based on the concept of objects, which contains attributes and methods. In simple terms, attributes are variables which store data related to and defining the object, whereas methods are the functions which we can use to access and manipulate the object’s features.
OOP has many advantages, which include — but not limited to — the following:
- It provides a clear modular structure for programs.
- It helps in defining abstraction for any real-world idea to be implemented in code.
- Advantage of reusability in other programs instead of reinventing the wheel.
- Using objects makes your program more tidy, easy to maintain, organized and understandable as the implementation details are hidden from other modules.
- New objects can be created from other existing objects by inheriting attributes and methods. Inherited objects (known as “child”) would usually have their additional features and methods, in addition to the ones they inherited from the “parent”.
What you should be doing while learning a programming language
I believe, the most important aspect of understanding, learning, and thence thinking as a programmer is writing some code; applying what you learn while learning.
From the time you’ve enrolled in a programming course, or maybe you want to study on your own, the first thing you should do is download and install that language that you are going to study, together with an IDE (Integrated Development Environment).
An IDE is a software application where you can write, edit and debug your code. I usually recommend IDE’s instead of simple text editors, especially for beginners, because they include:
- An integrated compiler and/or interpreter.
- An integrated debugger.
- Code auto-completion features which gives you hints while writing.
- Error highlighting features to tell you if you have wrote something wrong or if there is something missing.
- Gives you a summary (or few lines) from the documentation of any function or library you’re using.
- Has an integrated command line terminal where you can see the output of your code or for typing any user input from within the IDE.
- When you run your code and there is an error, a helpful error description (usually in red color) will be displayed on the integrated command line to help you know what caused the error.
If you’re studying Python, using Jupyter Notebook can also be very useful for studying as it allows running each block of code (in a cell) at a time to see the output before going on to the other cells and running them. But of course, its purpose is not to write complete programs and run them from within.
So, while studying, I highly recommend trying to implement what you’ve read. Open your laptop and start coding while studying. You are going to find it very beneficial, especially if you encountered something that you didn’t really understand how it works, what does it mean, or how is it interpreted. Write it down on that IDE, run it (even if it’s a single line of code) and check the result. Make mistakes, check the error, and try to debug your code. This way, in addition to getting a real in-depth understanding of what you’re learning, it will also help you in learning the syntax much faster.
Now you’ve learned the language, it’s time to implement some program
It is very important to understand the problem you’re trying to implement. Never go to you’re laptop and start coding right-away.
Sit down, understand the problem, divided into parts, and understand how each part is connected.
Don’t right code yet! Start by writing down a pseudocode instead. A pseudocode is writing your program using English language to provide a high-level description and overview of you’re program; how the different parts of you’re program are related, which goes first, which next, and so on.
A Toy Example — Robot Pet Caring
Assume that you have a pet, and you’ve recently bought a robot that you would like to program it to take care of your pet. You want your robot to do the following:
- Every morning, the robot should refill food and water and clean the pet’s cage.
- Play with the pet in the afternoon.
- Refill food and water in the noon and night time.
However, only in the morning, the robot should not perform any action if the pet is (still) sleeping. The robot should leave and come an hour later to check whether the pet is still sleeping or not in order to perform the postponed actions (i.e., cleaning and refilling food and water). If the robot goes and comes 3 times and the pet is still sleeping, then the robot should clean the cage, refill the food and water regardless. But, if while the robot is cleaning the cage and the pet stays sleeping, then the robot should send a notification to your phone to inform you that “the pet might be sick!”.
A pseudocode for such a problem could be written as follows:
By first writing down a pseudocode will help you in organizing your thoughts for how you will build your code and how each part of the code is related.
Hypothetically speaking, the above pseudocode can be then easily transformed into the following Python code, assuming we have created the classes “robot” and “pet” with some necessary functions:
Hope you enjoyed reading. I’d like to hear from you if have any ideas, comments, or suggestions that you’d like to share with me!
I would also like to say a big thank you to a Mechatronics Engineering student who has suggested to me to write about this topic.
About the Author
I’m is a Computer and Communications Engineer with research interest in wireless communications, resource allocation, game theory, optimization, antenna design and cognitive radio. I love programming too, and like to combine my expertise in programming to solve problems related to my research. You can connect with me on Medium Ahmad ElAmine and LinkedIn.