IDE Tips for reading abstracted code

Sometimes you are reading model/tokenizers code that reuses some other model or tokenizers code, either through inheritance or composition, like:

I usually (in Pycharm) just use cmd-b to jump to a function’s caller, but interested to know other people’s tricks!

4 Likes

Goal: minimize cognitive burden when reading/tweaking python code.

Hypothesis: Jumping to parent/helper fn definition is a start. Being able to read the implementation on hover is even better.

Pycharm

Push Members Down

Warning: This actually copies the implementation of methods to the child class. git checkout -b {code_reading} first or git stash after.

Go to name of child class.

Type meta-b to go to declaration of parent class.

Right click → refactor → push members down. Check all the boxes that you want to read in your child’s file:

Result:

2 Likes

VSCode:

Jump to parent, Peak References

(jump to parent) In child’s signature, hover over parent. type f12 (or right click, to jump there.

Then right click and select peak references to see child implementations in the same window with an overlay.

2 Likes

Thanks for the tips. Can you please also share for Vim?

I am new to OOP , and beside I alwasys read Transformer source code directly from GitHub, so I am not 100% sure what’s going on here :smiley:

I always go to the parent class straightforwardly too see the details
(or maybe use GitHub search within Repo)

But it’s great to see a pro trick here :slight_smile:

Surely, someone is working on a tool to expand the abstracted code into a flat code - obviously, controlling the level of expansion, i.e. expand functions only from a given list of libs/imports/projects.

My other would-be-amazing-to-have-tool is a tool which records an execution path, like profilers do, and dumps just that code, removing any “branches” that didn’t get executed, so that it could be read and even executed. That would make understanding a complicated code a breeze. This would make debuging simple too in certain situations.

I’m on emacs and there are quite a few different ways to jump around the code abstractions. I can share if there are emacs users here that need it.

1 Like

I’m a bit confused. Are you giving tips on how to write code or on how to read other code? Copying all methods into the child class seems redundant, so I assume that this is for making the code easier to read.

As you say, I simply jump through the code in PyCharm (e.g. by ctrl+clicking a method name). After a while this may lead to a rabbit hole - but that in itself might be a good sign that your code needs some refactoring and simplification.

Yes, focused on reading!

1 Like