Debug application like a pro

Hello developers, wellcome to my blog. I am using one technique for debugging or troubleshooting my application for years. This trick may help developers when debugging is needed. This is not a new invention so don't rise your expectation very high but I believe it is still worthy to share.

Let's consider the problem first. Most of the time our application gets bigger from time to time. As the business logic comes, the relevant files are increasing day by day. As a result, sometimes logic is dependent on many modules which reside in multiple files.

Now we get an issue from clients or users and need to debug the application. As the issue is related to multiple modules so we need to get the value or print the value from different modules to catch the issue. Here for simplicity module can be related to files. So we need to write the debug function in multiple files. By printing the debugging information step by step we are close to catching the issue and close to resolving it.

So now we resolve it by changing the code of what we do a silly mistake or if we don't consider the client present requirement. Now the code is working perfectly and running fine. But what happens that we did put some debug code in many files. We must remember the files and line number where we put the debug code.

Still no problem but after resolving the issue if we forget to remove any single debug information from any single file, the client or user can see your debug information. We don't want this. So you need to remove the debug code again that is double work even sometimes it is more than that (you may guess).

If your QA catches this, he/she can be your lifesaver. What if there is no QA or QA also missed. The following technique can rescue you from this case.

Put debug function in the conditional wrapper:

simple and straightforward. If you want to print the debug information put it under some condition. Better write a helper function or method. Inside it prints the information in if clause. The if clause condition will execute only when you want it to. A generic example will be like this

function debug_print_to_me_only(data){
   if( 'localhost' == getmyhost || 'xxxxxx' == getmyip){
      print data;
  } 
}

So whenever we debug the information we simply call the function with passing debugging data so that the control of output will be in our control when to display or when not. env variable is used sometimes, to check the condition like PRINT_DEBUG==TRUE

So if the files are moved with this functionality, debug information should not be printed. But it's always, best to remove all debug information from the production environment if possible. It's just a case when someone missed doing that for any reason.

Make your own Logg:

Logg will tell you the story, time to time if you maintained it so. I remember once my client complained that one particular user is not allowed to login. I just checked the logg file there I found the same user is inactive and that is recorded in the logg file. In login functionality, this log is created in inactive user status conditional clause. To check what's happening, I need not to login into the system or check the database, or even do the process again. A one-liner logg tells the whole story. Logg is important and maintains it with severity to identify easily. It is important to tell you that don't logg sensitive data.

There may be other tips that others do not know, if you have anything like that don't hesitate to share it.