Sunday, May 2, 2021

Software debugging process.. How to find bugs and fix them..

Every problem has a solution. Every.

0 Unsorted_

Reload the device

Reboot the phone, reboot the laptop.

Nuclear solution..

Just re-create everything from scratch: git reset --hard

Backup to previous commit

Check whether the error appeared at one of previous commits. Then see its difference with the current one: git diff oldCommit..newCommit

Often we are not sure at which commit the error started appearing. So we can perform binary search then. Checkout to the oldest commit, say the first out of 100. If the error is not there, checkout to the middle commit, it is the 50th one. If the error is not there, checkout to the middle of the remaining half, it is the 75th one. Etc. Binary search could be the fastest way of searching in our case.

See whether the object supports the property I am calling

In the line of code that causes the error, try placing a dot (".") after the object name and see what properties (attributes) does IDE suggest to me.

See whether the function supports all the arguments we pass to that

Arguments might be of certain type, in certain sequence, with certain number.

Check punctuation (syntax) carefully

Often compiler does not explicitly tells about the punctuation error.

E. g. we have an extra comma in the end of the line that was posted accidentally.

Another example: in Kotlin and some other programming languages, exclamation mark "!" means "not' in the boolean expression. It is very easy to miss it in the long line of an expression.

So the compiler could highlight with red color the next line, not the line with extra comma..

Sometimes there is a miscellaneous error in the neighbor line, though the compiler does not point on it. E. g. bracket is missing on line n, however the compiler points on the line n+1.

Go through all the chains of all the references

That's why nuclear solution (recreate everything from scratch) almost always works. Because we recreate all the references from scratch without mistakes.

Recently I got a tricky error. Due to mes with multiple merges I didn't know that I got eventually 2 files "NotificationService" and "NotificationSService" from two different commits in the same repository. I didn't mention that. AndroidManifest file referenced only one of them.

Search the error text in the source code of Android

Log (Logcat) gives me an error text. I can grab it and search throughout all the source Java files of Android framework (not only throughout code written by me). Then I can find the root cause of the error. Say the log gives me: "No layout manager attached; skipping layout". I grab this text and search it thoughout all Android source Java files. I can find fragment of the Android code which is to flag this error:

void dispatchLayout() {

    if(this.mAdapter == null) {

        Log.e("RecyclerView", "No adapter attached; skipping layout");

    } else if(this.mLayout == null) {

        Log.e("RecyclerView", "No layout manager attached; skipping layout");

    } else {...

Now I know that mLayout is null..

Compare line by line with the working example

If the problem to be solved is not so original, there should be a working best practices on the Internet yet. Sometimes my code is simply copied and pasted from those best practices.. I shouldn't say that though.. Anyway, the error can be found by merely comparing line-by-line my code vs working code..

See how variable is used

In Intellij, we can search usage of a variable throughout all the project files via the Menu Edit -> Find -> Find in path. If the error is related to the certain variable, we can find and see analogues to compare with the code causing the error.

Google more common ways to debug

There is always something not listed on this particular web page.

Learn the theory 

Read or watch YouTube about the subject topic of debug. E. g. if the error is caused by the crappy java keystore, read articles about what the keystore is and how to use it, etc. 

Enjoy the debugging process and avoid stress

When debugging is boring and stressful..

Debugging can be stressful because we don't see the perspective of the positive result and have a feeling that time is wasted. It can also be boring because some steps from manuals are boring to implement. Reading logs, reading manuals, reading stack overflow answers..

Go step by step

Instead of trying to resolve the whole problem immediately, break it into simple steps. Our brain feels comfortable to be concentrated on a simple easy to implement step, instead of trying to do multiple difficult tasks simultaneously. Steps can be listed with a simple list or with a mind map. Possible hints for steps are listed below. Following software can be used for mind mapping or listing:

  • Mindmeister is a good and partially free software for the mind mapping. I personally use it.
  • Google Sheets allow to organize lists online in the table format. List with sublists in hierarchical order is an analogy for mind map. It is often more easy to navigate via the table of lists in comparison with mind map.

Don't commit to a deadline

Never commit to a particular deadline unless you die if you don't meet it. Deadline is the major reason for stress.

Watch movies..

Indeed, debugging process is boring. If debugging is boring, what is funny then? YouTube videos, movies, TV series, news, cartoons, documentary etc. I can do boring debugging simultaneously with watching YouTube. Yes, it will take more time. Yes, it will prevent me from concentrate on the debugging problem. Nevertheless, eventually it makes the process funny! It is always better to move with a slow speed rather than not moving at all.

Feed brain with gas

Cars won't run without gas. Neither they run without oil. God created us with heads and brains inside, and the brain like any mechanism has some rules of service and feeding.

For the brain, gas is an oxygen. Without air our body cannot survive for more than few minutes. Blood should be filled with oxygen and then blood circulation brings it to the brain. 

The sad thing is fake schools and fake universities teach us to stop feeding brain with oxygen. Maybe they do it on purpose? By fake schools I mean 99% of public schools in the country. Students are forced to seat on the chair for a long hours. Sitting is the worse ever position for blood circulation. It causes sleepiness, low mood and results in chronically deceases. I believe the reason most children having bad marks at school is forcible sitting. 

We can definitely get new ideas when walking on the street because moving stimulates our blood to circulate, we breath more and get more oxygen. Here is how workplace of the world best software engineer look like:

Though I personally think Mr Torwalds wastes electricity by using treadmill. Instead, personally prefer taking an advantage of walking - there are a lot of things to do in the city. Go to government bureaucrats, grocery store, using our legs instead of riding a car. Yes, I can write code while walking to the store.

Today we all have mobile phones. This great device allows to do the mind work simultaneously with moving activity. This even allow us to write a code on mobile device. E. g. we can write code on a phone and commit it with GitHub website, not using a computer. Remote servers with remote shells also allow us to debug the code with a phone.

Of course, we still need to sit and lay down. At least to sleep. Or we can take a sit to take a rest when muscles are so tired. Or to eat something. Or when in the toilet... Nevertheless today people sit too much, much more than they really need. It is not necessary to sit on a chair to make the brain working.

Just forget about it..

If the task is neither important nor urgent, place this task on hold for a while and work on another task.

Once resolved, document it!

If I resolved the problem, it should not be the end. We need to prevent it from happening in the future. Otherwise, we will spend time and feel stress again and again. Invest time in making a note about the way that works. Future me will thank current me for that. Because future me doesn't remember anything that he did in the past. It is even better if the note about method of resolution is public - it could help someone else also. Even if it would help just one person, our life has value. Public note can be at any public searchable (googlable) source - personal blog, stackoverflow etc.

Localize the problem

Log the error per se:

        try {

            Code with error        }

        catch (Exception e){

            Log.i(TAG, e.getMessage());

        }

Make stopper points

Stop the program at certain point. Then see the values of objects at that point or log them into the console. Some of them might be null e. g.    

Localize the problem

Put logs around the problem in multiple parts of the code. See which one flags on.

Launch program step by step:

Set up breakpoints in the IDE. Then, launch the "Debug" menu instead of "Run". The program will stop at the breakpoints we placed beforehand.

Here is how

Turn off some components one by one

That will help to find where the root cause of the error is. For example if Google account sign in doesn't work in the app, I can turn Firebase off. That would allow to see whether the error is on the side of Firebase or Google.

Say I hear creak in my car while driving. I can try to turn of the engine and let it move forward with neutral transmission on its own. So, I see whether creak is coming from the engine or from other parts such as chassis. Then I can stop the car and manually sway it. If creak persists, I know that it is not from the wheels (because they are stopped while I sway the car).

Replace components with analogues

Say the mobile app doesn't want to sent messages to the user. Let's see:

- Does it send messages to another users?

- Does it send something else (data, notifications, metadata) to the same user who doesn't receive messages?

Ask..

Sometimes we find the answer by ourselves merely in the process of formulating the question.

Ask Google (google it)

Look for official general information about this type of error. Just google the error text..

Ask online forums

If the problem is unique and even google search does not give any result.. There are bunch of sources to ask the question at. They could be (but not limited to):

  • StackOverflow
  • GitHub - sometimes stronger and faster than even StackOverflow
  • Slack communities - some organizations have them
  • ?

Ask customer service

Surprisingly, some services offer support free of charge. E. g. Firebase support commits to answer engineers' questions. Though there is a delay of multiple days and everybody use corona virus pandemic as an excuse..

Ask a colleague / friend

Every engineer should have a friend-engineer..


No comments:

Post a Comment