Answer the question
In order to leave comments, you need to log in
How to edit someone else's code without breaking it?
What are the practices for working with someone else's code without documentation?
I got a freelance task to add features to a wordpress site written by someone before me.
The bottom line is this: a lot of code. There is no documentation. There are no tests. There are no comments in the code. Used custom template and plugin. Payment is hourly.
I tried to work very carefully. And I did not make changes to the code that would change the behavior of functions. And if I added something in such a way as to maintain backward compatibility, since without analyzing the work of the entire code, I cannot say where something could break and where else the functions are called from.
And asking for payment for a few days of work just to thoroughly study the algorithms of that heap of code that was written before, it seems like it will also increase the budget several times.
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-->
<!--products_list-->
<?php
PX()->display_widget('JD\Widget\Producer\Products\Widget', array(
'profile' => $profile,
'posts_per_page' => 3
));
?>
<!--end products_list-->
Answer the question
In order to leave comments, you need to log in
Firstly, you need to set aside time for parsing the legacy code, you should immediately talk about this with the customer. Knowing the task, you always understand WHAT you need to write, but in the case of legacy, you also need to understand WHERE to write it. Without this, in any way, and therefore this time must be taken into account.
Second. Once I was taught that the code should be changed only by adding it, in extreme cases, deleting it, but in no case rewriting it. Therefore, if you need to change the behavior, you inherit, override the method and use the new class. It's hard for me to judge php, how this project is implemented, but OOP was invented for this, that it is easier to maintain and it is easier to modify.
The next option is to study the code, start writing tests for it. I rarely use this method, mostly I write on what I'm not sure about, that I'm afraid to break.
Well, of course, the version control system, commit more often, run the project more often, see that nothing is broken. I stopped being afraid of breaking something, because at any moment I can compare what has changed in the code, between branches, and if it breaks, then make a reverse or go to the working branch.
In general, reading legacy is a matter of experience. I remember my first other people's projects, I thought I was in hell. Now digging into someone else's code, this is my favorite thing) I can sit for hours to analyze someone else's code, that the authorities have to let me in: "I understand, I love this too, but I have to do something")) I just like to climb on github on other people's projects, see how people think.
1 - Always make a backup of someone else's code before even making any changes.
2 - Copy someone else's code and deploy on your server. Then edit without fear that something will break and the client will see it.
It is desirable that errors can be made and quickly corrected:
- Being able to rollback a release from production to the previous version is a great last resort if everything went badly at all.
- Use a code versioning tool - git for example.
- Writing tests for the code you edit will help you not only protect yourself from stupid mistakes, but also better learn how the code works.
Unfortunately, I can't give you any specifics on wordpress.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question