It was my last day of work and I'm happy I finished my remaining tasks on time. And now, it's time for me to move on to my next journey. The only reason I took the job was for me to get enough experience to build my own app. So I already planned it that I will quit my job after 3 years since I signed the 3-year bond contract. Didn't know that it was tough having one source of income but I think I'm good since I'm still living with my parents... There are times that I compare my income and life to my coworkers, same age brackets and shit... which resulted for me to lose my focus and my mind shifted a lot of times, and I think it's a waste of time to repeat it again. But... at the end of the day... I'm just too lazy I guess and I think that a day job is not suited for me. I'm more of the build one app and make it rain type of guy... Don't even know what to do now!!! I guess I'll try playing stocks and multiplying my money while trying to build my ...
Coding Tips: How to Fix Bugs Easier and Faster (Things I've learned as a full stack web developer for 3 years)
Here's some coding tips for y'all...
Imagine you got hundreds of bugs and each bugs cost you an hour or two to fix. And you don't need to be a mathematician to say you'll waste hundreds of hours just fixing bugs.
Before anything else let's first breakdown why it takes time.
Reasons may include:
- You forgot how the code works cause you code it a long time ago
- It's a first time that you experience this kind of bug and you don't know the cause
- You don't know what's causing the error and what line of code
- You need to change a lot of codes and it's scattered in different files.
- Syntax errors
- Logic Errors or change in business logic
That's a headache right? or right? Of course it is... And as a noobie programmer I'm here to help a fellow noobie.
So what do you do to fix bugs easier and faster?
It's simple there are two types of things you need to do or change in your coding style. First let's group the reasons in to two groups.
First group
- You forgot how the code works cause you code it a long time ago
- It's a first time that you experience this kind of bug and you don't know the cause
- You don't know what's causing the error and what line of code
- Syntax errors
Second group
- You need to change a lot of codes and it's scattered in different files.
- Logic Errors or change in business logic
The first group can be solved by logging or adding comments and the second group can be solved faster by coding wisely and making the code modular and reusable.
Logging and adding comments
It maybe tiring now but later in the future it will save you lots of time. We all know that there will always be bugs in our programs (Murphy's Law) so in that case why not come prepared??? All you need to do is pick a nice third party library for logging, there are many out there. Some examples may include:
After you've pick one then just log anything you can log. For Example you have a function or method that creates a user then you should have a logged like this:
Creating a user with name = 'Hans' and age = '12'...The good thing about this is you explained what the method works as soon as it entered it and you display the important values. So it saves you time remembering what it does and at the same time you'll easily catch and know the bugs since you've displayed the important values you need to see.
Also, the best part is most third party loggers allow you to log errors so it will be colored red etc. So you can put a log on expected errors so that the next time it happens you won't be surprised anymore.
if (user == null) {
Logger.error("User does not exist");
}
While logging is important, adding comment is too. For your code to be developer friendly I advise that you put short comments that explains your function or method. Also, put comments on codes or lines that seems complex. We want other developers or your future self to instantly understand your code. Time is crucial after all.
Making the code modular and reusable
What do I mean by modular? I don't know about you but for me it's breaking the code into tiny pieces but NOT TOO TINY. Why do we do this? It is so that we break each code into their specific functions and uses. And after we break it, it is now reusable and ready to be use in the future. Here is an example.
For example we have a function that gets the total list of students in the class room and returns the average, min and max age.
(I'll be using javascript)
function getAveAndMinAndMaxOfAgeOfStudents() {
let students = getStudents();
let min = null;
let max = null;
let sum = 0;
for (let i = 0; i < students.length; i++) {
let student = students[i];
//get min
if (min == null) {
min = student.age
}else if (min > student.age) {
min = student.age
}
//get max
if (max == null) {
max = student.age
}else if (max < student.age) {
max = student.age
}
sum += student.age;
}
return {
minAge: min,
maxAge: max,
aveAge: sum / students.length
}
}
As you can see we made a function to get all the data we need but can you see the problem? We could still break the pieces to a much cleaner and reusable code. like this one.
function getAveAndMinAndMaxOfAgeOfStudents() {
let students = getStudents();
return {
minAge: getMinFromArrOfObj(students, 'age'),
maxAge: getMaxFromArrOfObj(students, 'age'),
aveAge: getAveFromArrOfObj(students, 'age')
}
}
function getMinFromArrOfObj(arr, key) {
let min = null;
loopArrObj(arr, function doSomethingPerLoop(obj, index) {
if (min == null || min > obj[key]) {
min = obj[key];
}
});
return min;
}
function getMaxFromArrOfObj(arr, key) {
let max = null;loopArrObj(arr, function doSomethingPerLoop(obj, index) {if (max == null || max < obj[key]) {max = obj[key];}});return max;
}
function getAveFromArrOfObj(arr, key) {
let sum = 0;
loopArrObj(arr, function doSomethingPerLoop(obj, index) {
sum += obj[key];
});
return sum / arr.length;
}
function loopArrObj(arr, doSomethingPerLoop) {
for (let i = 0; i < arr.length; i++) {
doSomethingPerLoop(arr[i], i);}
}
Now can you see why it's cleaner... Imagine we have a bug on getting the average of the students then all we have to change or debug is the function: getAveFromArrOfObj so the less code you change and gets affected the less regression testing. This will truly save you lots and lots of time especially when you client or boss ordered you to change some features in the future.
...And I tell you this, it will save you lots of time if you code like this, not only for debugging and fixing bugs but for future features too. Imagine you would like to add a code that gets min, max and average height too of each students then you can code it faster like this:
function getAveAndMinAndMaxOfHeightOfStudents() {
let students = getStudents();
return {See it's the same what did I Change? Just the key, from age to height. I just changed the key to get the right property.
minAge: getMinFromArrOfObj(students, 'height'),
maxAge: getMaxFromArrOfObj(students, 'height'),
aveAge: getAveFromArrOfObj(students, 'height')
}
}
...Or you could make a generic function like this:
function getAveAndMinAndMaxOfStudents(key) {
let students = getStudents();
return {
minAge: getMinFromArrOfObj(students, key),
maxAge: getMaxFromArrOfObj(students, key),
aveAge: getAveFromArrOfObj(students, key)
}
}
And then you could just call the function like this...
getAveAndMinAndMaxOfStudents('age');
getAveAndMinAndMaxOfStudents('height');
And now that you know this stuff time for payback please subscribe to my blog or youtube channel.
...And Goodbye and goodluck cause I need to sleep again...
Comments
Post a Comment