Hello and welcome
Thanks for stopping by.
Table of Content
Full table of content for the entire series is here
Today, we will continue the following
-
Getting started (cont'd)
- Learn Programming concepts (cont'd)
Programming Concepts
Here we are again to serve you more basic programming concepts.
Functions
This is the term used to describe a block of code that performs a specific task or we can say it is a block of code that performs a specific function 😜. They are also sometime called ‘methods’, ‘procedures’, ‘subroutines’ in some programming languages.
We need functions to ensure that the same operation or task can be implemented once in the code and reused as many times as needed instead of re-implementing the same operation everywhere it is needed.
Let us take a function to add 2 numbers:
function add(a,b) {
return a+b
}
In the function above, we gave it a name of add
and passed the 2 numbers we want to add to it as the variables a
and b
and then we return the result of adding them.
Now anywhere in the code we want to add 2 numbers, we simply call the function to do the job for us instead of reimplementing the addition again.
An example of adding 2 numbers might seem trivial but imagine if you had to take a list of users, call a save function in a database for all of them and send a confirmation email for all of them when done.
The single implementation of this alone is a big task to do. Now imagine having to re do the same logic anytime you have some users who need these operations.
This is where functions shine. Implement logic once and call it everywhere you need it in the code
Do not worry about how the code above works, we are not there yet
Just understand that reusability is a crucial part of application development and functions are one of the tools that help to achieve that. We will do more on functions when we get to demo.
Input/Output (I/O)
This simply describes operations that enable the program to read from and write to the storage of the server.
Sometimes you might want to store a user’s profile image on a server or read an existing image so you can display it on the user’s profile when they log on. I/O operations enable these types of actions. We will take some time to learn some basics about these when we get to demo.
Data structures
This as the name suggests is the structure of data. It is a term used to refer the different way in which data can be structured and stored to enable its efficient use.
Some data structures make it easier and/or more efficient to read data from them than alternatives.
Some data structures make it easier and/or more efficient to add data to them than alternatives.
Some data structures make it easier and/or more efficient to edit data in them than alternatives.
Some data structures make it easier and/or more efficient to delete data from them than alternatives.
There are many data structures in different programming languages but the most common include:
Array / List
This is a list of items of any data type or objects. Some programming languages enforce uniform types for all items while others (like our beloved javascript) allows arrays with items of different data types.
Example:
arrayOfStudentAges = [12,15,12,13,14,13,12]
Object / Map
This is a data structure that provide a collection of key-value pairs that can be used to store information about an object or provide a mapping of information together
Example:
person = {
name: "John",
age: 30,
city: "New York"
};
Set
This data structure is similar to an array but with a condition that all items must be unique in the list
Example:
setOfUniqueStudentAges = [12,13,14,15] // no duplicates
Stack
The stack is like a list except that it enforces Last in First out (LIFO) interactions. This means that any data added to the stack can only be remove after all subsequent items have been removed.
Data can only be added or removed only from the top. Picture a stack of books, or boxes. Ideally, you will remove or destack (yes, its a real word) each item from the top of the stack.
Queue
This is also quite similar to a list except that it enforces First in First out (FIFO) operations.
So like a queue at the mall or at the filling station, first in first out. Items can be added from the back of the queue only and can be removed from the front of the queue only.
There are quite a few more data structures out there like Linkedlists, Trees, Graphs, Heaps and more. We will get to them as we go along because we are here to help
They are however more complex and require more than basic knowledge to implement and understand
Bugs
A bug is anything that does not go right with your code. Bugs are defects or errors in your programs that make them to behave unexpectedly or generate incorrect or unintended results.
Some common types of bugs include:
Syntax Errors
These bugs occur due to code that does not match the syntax rules of the programming language in use. They are typically easier to find and fix using a good IDE. Syntax errors can also be detected by the compiler or interpreter and prevent the code from running since the computer will not understand the language while trying to compile it and so it will throw errors.
Logic Errors
Sometimes, the syntax is correct, but the logic is wrong. In this case the code will run without any syntax errors because the problem is not a syntax one, but it will produce incorrect, unintended or unexpected results due to flaws in the logic or algorithmic mistakes.
For example, if you are writing a function to check if a number is greater than 5 but you use the less than operator in your comparison (because we all do this sometimes), the code will run fine but the function will return the exact opposite of what was intended. Logic Error
Runtime Errors
These bugs occur during program execution and often lead to program crashes or unexpected behaviour if not handled properly. Examples include division by zero, accessing invalid memory locations, or using uninitialized variables.
Debugging
Debugging is the process of finding and fixing these bugs to ensure the program works as intended. It is an essential part of the software development process because no matter how good the developer, we all tend to introduce bugs from time to time.
There involves various techniques and tools to locate and resolve issues effectively.
Print Statements
Inserting print statements at various points in the code to output the values of variables and track the program's flow can be an effective way to understand what's happening during execution.
Debuggers
Debugging tools and integrated development environments (IDEs) provide debuggers that allow developers to set breakpoints, inspect variables, and step through the code line by line to find issues.
Logging
Using logging methods in the programming language (console.log
in javascript) to write debug information to the console or to log files can help in tracking the program's execution and resolving issues when they arise.
Unit Testing
Writing unit tests to verify the correctness of individual units (functions, methods) in the code can help catch bugs early in the development process before shipping it off to users.
Code Reviews
Having peers review your code can be valuable in catching errors that you might have missed because 2 heads are better than one.
The typical process of finding and fixing bugs is as follows
- First, reproduce the Bug
- Isolate the Issue by determining which part of the code is causing the bug (using the techniques above).
- Read error messages and stack traces as they are typically good at helping to pinpoint the location and nature of the bug. Some error messages even recommend fixes (what a time to be a developer).
Once the bug is identified, apply the necessary fixes, and thoroughly test the code to ensure that the issue is resolved without introducing new bugs 😅.
Debugging is a skill that improves with experience. It requires patience, attention to detail, and a methodical approach to systematically find and fix bugs in the code. Effective debugging practices lead to more robust and reliable software.
This is it for today. Until next time. Be cool.
Previous post is here
Next post is here
Top comments (0)