Skip to main content

My last day of work and hoping it's a beginning of something new

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 {
        minAge: getMinFromArrOfObj(students, 'height'),
        maxAge: getMaxFromArrOfObj(students, 'height'),
        aveAge: getAveFromArrOfObj(students, 'height')
    }
}
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. 


...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

Popular posts from this blog

Flutter: How to create an error alert or popup notification with SnackBar

There are 3rd party libraries out there that offer alerts or popups in Flutter. But, what I'll be teaching you today is how to use the built in SnackBar widget to produce an error popup or notification easily. For the video version of the tutorial go here:  How to use snackbar in flutter as error alert or popup notification To use the SnackBar widget and make a nice Snack Bar that you see in most google apps like youtube, all you need to do is wrap your entire app with the Scaffold Widget. So let's make create a new file page_wrapper.dart and create a new Widget named PageWrapper that accepts a widget as children. After that all we need to do is call the PageWrapper widget in all of our pages so that we have a base widget that uses the Scaffold in every page. This means we can now use the SnackBar on every page. In the main.dart all you need to do is this: Then now we can create the home page and create a snackbar. Before that we need a trigger

What is the DOM and how does it work?

     I've been asked to teach about the DOM... but first and foremost what is the DOM ? Is it when you are angry and you say DOMmit!!! hehe Or Is it your friend DOMinick??? Well kidding aside, the DOM just stands for Document Object Model (đŸ˜…Sorry for joking but please continue reading the article - there are javascript examples below in codepen ). Browser is taking the HTML sent from the server and converting it to the DOM. so the browser is based on the DOM and not on the html and everytime we edit the dom using javascript it gets reflected on the screen. Is the browser's internal programmatic representation of the web page. Which basically means one thing, you can manipulate the web page using the dom. and how do we do it? of course by using our favorite language, JAVASCRIPT! Since the dom is more or less an object in javascript, it contains several function to access, modify and delete certain elements. If you are familiar with jquery and thi

My last day of work and hoping it's a beginning of something new

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