My Process, When I Need to Understand a Part of Code

I have created some add-ons for some programs. One thing that I need to constantly do is trying to understand a part of the code. Usually it is a part of the code, that does something that I needed done also in my subprogram.

I have so far came up with 2 different way. One is going into the source code, until I understand how it works. It intuitively seems like a way it should be done. But going from a piece of actions, if lucky the function called when I press the button, and then follow the trail until I get to the part I want.

The other was to do it is to take another add-on, that also uses this type of action. Then I hack at the code, deleting all the parts that I don‘t need, until I am left with the so called minimal working sample.

So there was a comment written on one of my add-on pages, and it brought an interesting problem. It wanted it to work in only in certain conditions.

When I did not find the necessary hook in the code quickly, I turned to the second way of doing. A quick git-hub search brought me to the repository for Habit RPG addon. I don‘t even know what it does, but from skimming the short description it did have the part that I needed: how to differentiate between the correct and incorrect answers. It also had the disclaimer that it no longer worked.

So I ended up removing the lines of code, until I was left with the following one:

    from anki.hooks import wrap
    from aqt.reviewer import Reviewer

    def card_answered(self, ease):
        print("This happens with every answer")
        if ease > 1:
            print("This only happens when the answer is correct")

     Reviewer._answerCard = wrap(Reviewer._answerCard, card_answered)

Now I could just use this as it is, or I could go back in to the code to try and understand why it works. Apparently the ease in the function _answerCard() is the button pressed. Since 1 means incorrect, and 2-4 normally mean correct, it provides a good way to differentiate.

Also, the wrap function is there to execute by default the first function and return the second. It can do the reverse with the additional argument: pos == "before".

And that is one way how a self-learned programming person it trying to make sense of the code.