At first, I ignore the desire to change the code. This is sometimes difficult to do. But understanding at first and change later saves itself from many unpleasant "experiences."
Next, if the format is bad, reformat. Use a code formatter if you have one. This is because you tend to look at the indentation, and if it's bad, your understanding of the code is also in doubt.
Then, if there are complex data structures, I like to draw a small chart. The task here is as simple as possible. Large charts are funny on the wall, but in most cases they are cumbersome to watch. So it is wasted.
If you finally understand what a piece of code does, write a comment. This is important because otherwise you will not understand it the next time you are here.
The next step is to create unit tests. Now you can not only test the code, but also test your understanding of the code.
Finally, if you understand this, and know that it can (and should be) better, change it. But be sure to run the tests. If you are not paid for every error you solve.
source share