Fixing bugs is a good way to learn a new code base. You often go through the procedure of understanding the high level software …
Fixing bugs is a good way to learn a new code base. You often go through the procedure of understanding the high level software architecture, and are then given what appear to be fairly straightforward bugs, however, if the nuances of a system are not understood you will end up introducing more bugs than you fix. More often than not it is the fault of the code’s former owner. One of the most common issues that stack the cards against newbies is when positional dependencies have been built into the code.
If a function is very long it is most likely doing too many things, and either doing them in a particular order. Changing even a single line of code or re-factoring sections into separate functions can throw off the end result. If the initial author of the function is available ask them if it makes sense to break up the function into logical units that only perform a single operation. If you can’t, try understanding how each of the components interact with one another by stepping through a debugger and seeing what values are output and how they are re-used in each section of the function.
In try/catch/finally blocks developers usually place the section that should be “tried” inside the try, and despite the try succeeding or failing the finally block will be executed. However, it is possible that code can be ‘misplaced’. Placing finallly logic inside the try block or vice versa can throw off the end result in exception handling. When fixing bugs simulate exception conditions to verify that the exception is being handled properly with your fix.
Developers use “return” or “break” statements in the middle of functions to avoid executing the function once a condition fails. Make sure you understand why the function should be abruptly ended. “continue” statements are another control block that throws people off. Skipping an iteration if the rest of the loop perform costly operations is a good practice, but it is important to understand what should come before and after the continue block.
Understanding a new code base can be daunting, but when it doubt it helps to understand interactions and dependencies before putting in a fix. Talking to each developer who has touched it is a great benefit. But if they are not readily available then try stepping through test cases for the code you are fixing, it will help you understand how values and parameters are being processed, and expose any positional dependencies. Or create a few tests of your own by setting up data conditions, and see how they are handled.