Visualizing with pseudo code

In a previous project I was working on, it involved multithreading and three conditional locks. There was so much happening and so much code to read through and I had the attention span of a puffer fish. To aid myself in understanding software, I write pseudo code in a stacked notation for an easier glance of things. This is a mix of pseudo code and actual function names so I will have reference back to the actual method or variable.

I have refined it over the years and have come up with two variations. They are nothing as complex as the image I have used above. It's just something that makes me look smart.

1. Forward notation

Or top-down notation. It just means that you start from the outermost point of focus and start drilling into functions, methods, subroutines, whatever floats your programming boat. Most pseudocodes I've seen only indent inside a loop or condition. I made sure to indent also for functions so I can glance what is happening in one pseudocode block. Here's a simple example:

  • [initWithToken:]
    • load protocol
    • check token
    • return if instance exists for it
    • [getStoredID:]
      • lock
      • get ID from preference file
      • unlock
    • if invalid ID
      • create new
      • [saveToFile:]
        • lock
        • save ID to preference file
        • unlock
    • return new instance for ID

Forgive the bullet format. I have not implemented a code syntax highlighter on my blog. Too lazy.

As you can see above, you can easily find out where the locks are and who accesses a file. This is pseudocode for Objective-C so those enclosed with square brackets are methods and the lines indented below them are what happens inside those methods.

2. Reverse notation

Or bottom-up notation. I use this when I want to see who the hell touched this goddamn variable. It might be bad code design but sometimes, you just end up working on someone else's code and it sucks balls and you don't quite understand yet who modifies who.

With this notation, I simply point out which methods touch a variable and onwards. But it doesn't have to be focused on the variable. It can be an action or another call and you want to see who makes calls to it. Example, I want to see who has been modifying the token:

  • token = (Yes, written like this to tell me that someone is assigning a value here)
    • [updateToken:]
      • [loadFramework]
        • main()
    • [deleteToken:]
      • [unloadFramework]
    • [anotherMethod]
      • [anotherInvokingMethod]
        • [evenAnotherInvokingMethod]
          • handle_signal()

In a glance, there are three entry points when the token would be modified and I started tracing when the outermost call would happen and so on and so forth.

So, yeah.

There are probably better ways to do it but I'm proud I came up with this myself. Please do share how you do pseudocode.

Also, I've been looking for an IDE that would support collapsing/expanding lines that are indented below it. Do you know of any? It would help me in easily navigating through quite a huge pseudocode I overbuilt ;)

Category: