Fixing bugs in your codes

Fixing bugs in your codes

Hello techie,

Often times, you could write bunch codes without testing each block concurrently which is actually not a bad idea, it shows how good you are in the programming language. However, you might run into some dark cases where your codes might actually appear to be absolutely neat and correct, but it's otherwise. Then you wonder where the hell the bug could really be, it happens to so many developers and too you are never alone, if you ever felt like giving up in some cases, i wrote this article to get you on track and solve your problems yourself with the most interesting mindset. Whenever i am in a situation or a space and it makes me feel like i am not good enough to continue the process, something keeps bubbling within me and my mindset about the situation changes immediately and i eventually get over the situation which are mostly dark times while coding. I would share the secret with you shortly.

  Every time i'm stuck in my codes, what comes to my mind first is > so many people must have faced this issue before, and of course i'm currently going through it and someone somewhere would definitely go through it, if not worse and hopefully, you get the gist. Let's just get started in that in helping you to solve your bugs yourself.

Follow this principles for fixing programmatically

Step 1: Depending on the programming language, provide a try catch block of code or callback at every block of code that are likely to throw an error, then print the catch error in the console or a log module. This should be able to identify the exact portion of your code that is throwing the error when you have thousands of lines code, it makes debugging very easy and it provide a solution in most cases which are always defined the error logged.


const router = require('express').Router()
   const client = require('../controller/client')

router.get('/', async (req, res) =>{
   const sess = req.session
   if(sess.username){
      const username = sess.username
      console.log(username)
         const collection = client.db("diary").collection("notes");
         const result = await collection.find({user:username}).toArray()
            if(result){
               res.json(result)
               console.log('working on result')
               result.map((key)=>{
                  console.log(key._id)
                        console.log(key.title)
                  console.log(key.body)
               })
            }else{
               console.log('an error has occured')
            }
      }else{
         res.send('invalid user')
         console.log('User authentication required')
      }

})

module.exports = router

Step 2: Always make a return statement in every function or classes respectively to know the function that didn't perform an operation correctly and does not carry out the return statement.

Copy and paste the error on google or check stack overflow to see most related issues which has actually helped so many people to fix their bugs. If it still seem dark after trying to fix, reach out to people that knows more than you do. Try as much as possible to document solutions to errors, you would definitely need it some other time.

Follow this steps for exclusive fixing At first you might probably want to assign the bug to someone who knows better than you do, however it does not help your problem solving skills. You should record these three things before making an attempt.

  1. What the user was doing
  2. What they were expecting
  3. What happened instead

These will tell you how to recreate the bug. If you can't re-create the bug, then your chances of fixing it will be nil.

Step 1: Google the error message

If there is an error message then you're in luck. It might be descriptive enough to tell you exactly what went wrong, or else give you a search query to find the solution on the web somewhere. No luck yet? Then continue to the next step.

Step 2: Identify the immediate line of code where the bug occurs

If it's a crashing bug then try running the program in the IDE with the debugger active and see what line of code it stops on. This isn't necessarily the line that contains the bug (see the next step), but it will tell you more about the nature of it.

If you can't attach a debugger to the running process, the next technique is to use "tracer bullets", which are just print() statements sprinkled around the code that tell you how far a program's execution has got up to. Print to the console (eg: Console.WriteLine("Reached stage 1"), or printf("Reached stage 1")) or log to a file, starting very granular (one print per method, or major operation), then refining it until you've found the one single operation that the crash or malfunction occurs on.

Step 4: Identify the line of code where the bug actually occurs

Once you know the immediate line, you can step backwards to find where the actual bug occurs. Only sometimes will you discover that they're both one and the same line of code. Just as often, you'll discover that the crashing line is innocent and that it has been passed bad data from earlier in the stack.

If you were following program execution in a debugger then look at the Stack Trace to find out what the history of the operation was. If it's deep within a function called by another function called by another function, then the stack trace will list each function going all the way back to the origin of program execution (your main()). If the malfunction happened somewhere within the vendor's framework or a third-party library, then for the moment assume the bug is somewhere in your program--for it is far more likely. Look down the stack for the most recent line of code that you wrote, and go there.

Step 7: Log everything and analyze the logs

Go through each module or component and add more logging statements. Begin slowly, one module at a time, and analyse the logs until the malfunction occurs again. If the logs don't tell you where or what, then proceed to add more logging statements to more modules.

So many developers enjoys the joy after fixing an hectic bug, try it out too.