How to edit someone else's code so that it does not break?

What are best practices for dealing with someone else's code without documentation?
Got the task to freelance for adding functions to your wordpress website written by someone before me.

The bottom line is this: a lot of Code. There is no documentation. Tests there. Comments in your code there. Used home-made template and plugin. Billing by the hour.

I tried to work very carefully. And have not made any edits to the code that would change the behavior of functions. And if that was writing so in order to maintain backward compatibility, as not analyzing the work of all the code I can't tell where could something break and where else are called functions.
And to ask payment for a few days just to thoroughly study the algorithms of the pile of code that was written before this kind of is also several times the budget will increase.

But when I corrected the comment in the template code with this text
<!--products_list-->
<?php
 PX()--->display_widget('JD\Widget\Producer\Products\Widget', array(
 'profile' => $profile,
 'posts_per_page' => 3
));
?>
<!--products_list-->

To this (the last line added the word "end" in the comments)
<!--products_list-->
<?php
 PX()--->display_widget('JD\Widget\Producer\Products\Widget', array(
 'profile' => $profile,
 'posts_per_page' => 3
));
?>
<!--products_list end-->


broken self-written parser caching system.
How can you effectively monitor the side effects of the edits?
June 14th 19 at 18:14
3 answers
June 14th 19 at 18:16
First you need to lay the time on the analysis of legacy code, it just need to speak with the customer. Knowing the problem, always understand THAT you need to write, but in the case of legacy it is also necessary to understand WHERE to write it. Without this in any way and therefore this time should be considered.

Second. Once I was taught that code should change just appending it, or at least removing, but in any case not overwriting. Therefore, if you need to change the behavior inherited, override the method and use the new class. It's hard to judge php as this project is implemented, but the PLO and to come up with that it is easier to maintain and it is easier modificeres.

The next option to go through the code, start writing tests for it. I this method is used rarely, mostly write on something I'm not sure I'm afraid to break.

Of course the version control system, more comity, often start a project, see that nothing is broken. I stopped being afraid to break something, because at any moment can compare what has changed in the code between the branches and if broken, then do revert or go to a working branch.

And actually reading legacy, it's a matter of experience. I remember the first other people's projects, I thought I was in hell. Now to delve into someone else's code, it's my favorite thing) I can sit for hours to disassemble someone else's code that superiors have me affliction: "I understand, I also love it, but it is necessary to do business")) on github just Love to climb on other people's projects to see how people think.
June 14th 19 at 18:18
1 - Always do a backup of someone else's code even before making any changes.
2 - Copy someone else's code and deploy it on your server. Continue to rule without fear that it will break something and the client will see it.
June 14th 19 at 18:20
It is desirable that the errors could be done quickly to fix it:
To be able to roll back a release from production in produsul version - always great to play the opportunity in the extreme case, if all went bad.
- Use a tool for code versioning - git, for example.
- Write tests for the code you are editing, will not only help to protect yourself from gloppy error, but it is better to learn how the code works.

Any specifics on wordpress unfortunately can not give.

Find more questions by tags FreelanceHTMLJavaScriptPHPWordPress