HACKER Q&A
📣 zubairq

What techniques do you use to understand and simplify complex code?


Hi, I spend most of my day trying to understand and simplify complex code with many moving and interdependent parts. My usual technique is to "rubber duck" by writing a mini essay to myself related to the code, or to try to step through the code, but I was wondering if anyone has some other techniques which I may find useful. Thanks in advance :)


  👤 sharemywin Accepted Answer ✓
Have you looked at design patterns. some of the more complex code usually uses one of these.

you can also look at a finite state machine. UML has interaction diagrams and/or state charts.

you can look at black box abstraction, try to separate the internal state from the external interactions.

I've also tried using a table to layout all the variations of the variables and what the output should be with "don't cares" to cut the options down.

you could always have chatgpt document the code and see what it says. it will probably be wrong but you never know.


👤 sophonX
What do you mean by moving and inter-dependent parts ?

I read doc (if any), talk with different people, although it's none in my team (T_T), then identify different services or processes, read their logs a lot, then glance through code structure + file names, then put thread dumps, then somehow found it.

Then explore around something, add more logs or use a debugger - run it locally or on cluster, then speak with people - share my findings, then they'll ask few more questions or explore on their own - this will happen for a week.

This way you'll exponentially (whether you remember that info is a different topic) gain knowledge + faster turn-around time for any poc or incremental feature change.

Ultimately, everything is a kernel process with some threads running in it. So, yeah attack codebase in anyway possible.


👤 throwawaysalome
A common two-bird-with-one-stone approach of experienced programmers is to rewrite the code to fewer lines, and to condense long-winded comments. This not only aids understanding, but also imparts ownership to the rewriter.

This approach often generates ill will, but eff them.


👤 kwertyoowiyop
Add a comment explaining each section. Hopefully that is where your mini essays end up!