JavaRush /Java Blog /Random EN /Coffee break #20. What is legacy code and how to work wit...

Coffee break #20. What is legacy code and how to work with it. Tools that make writing technical documentation easier

Published in the Random EN group

What is legacy code and how to work with it

Source: Dou Sooner and later, a programmer will probably have to encounter legacy code. To ease the consequences of this acquaintance, I have selected some practical tips and examples from my own experience - in particular, working with a legacy Java system. Coffee break #20.  What is legacy code and how to work with it.  Tools that make it easier to write technical documentation - 1

Legacy Features

Legacy is someone else's code, which is often so terrible that it is generally not clear how to work with it. And if you have to work with a legacy system, in addition to the old code, you will also encounter:
  • with outdated technology;
  • heterogeneous architecture;
  • lack or even complete absence of documentation.
In fact, legacy code is not that scary, and here’s why: if the system has lived all these ten years and is still working, then it has some use. Maybe it makes good money (unlike your last startup). Additionally, this code is relatively reliable if it was able to survive in production for this long. Therefore, changes to it must be made with caution. First of all, you need to understand two things:
  1. We cannot disrespect a system that makes millions or is accessed by thousands of people a day. No matter how poorly it was written, this disgusting code survived to production and works 24/7.

  2. Since this system brings real money, working with it comes with great responsibility. This is not a startup, but something that users will work with tomorrow. This also implies a very high cost of error, and the point here is not in the client’s claims, but in the real state of affairs.

Reverse engineering

To successfully work with legacy code, you will have to use reverse engineering techniques. First, you need to carefully read the code to understand exactly how it works. This is mandatory, because we most likely will not have documentation. If we do not understand the author’s train of thought, we will make changes with unpredictable consequences. To protect yourself from this, you also need to delve into the adjacent code. And at the same time move not only in breadth, but also in depth. Where is the method called with the error? Where does the code that calls it come from? In a legacy project, "call hierarchy" and "type hierarchy" are used more often than anything else. You will have to spend a lot of time with the debugger: firstly, to find errors, and secondly, to understand how everything works. As for documentation, it would not be a bad idea to resort to industrial archaeology. It can be very useful to dig up old documentation somewhere and talk to those who remember how the code you inherited was written. Using these techniques, sooner or later you will begin to more or less understand the code. But to prevent your efforts from going to waste, you must immediately document the results of your research. To do this, I recommend drawing block diagrams or sequence diagrams. Of course, you will be lazy, but you definitely need to do this, otherwise in six months without documentation you will be digging through this code like it was the first time.

Don't rewrite the code

The most important thing in the development process is to beat yourself up on time and not try to rewrite the entire code from scratch. Estimate how many man-years this will require. It is unlikely that the customer will want to spend so much money on redoing something that already works. This applies not only to the system as a whole, but also to any part of it. Of course, they may give you a week to figure everything out, and another week to fix something. But they are unlikely to give you two months to write part of the system again. Instead, implement the new functionality in the same style as the rest of the code. In other words, if the code is old, you should not be tempted to use new beautiful technologies: such code will then be very difficult to read. For example, you may encounter a situation like we had: part of the system is written in Spring MVC, and part is written in bare servlets. And if in a part written in servlets, something else needs to be added, then we add it in the same way - in servlets.

Respect business interests

It must be remembered that any tasks are determined, first of all, by value for the business. If you do not prove to the customer the need for certain changes from a business point of view, these changes will not happen. And in order to convince the customer, you must try to stand in his place and understand his interests. In particular, if you want to refactor just because the code is hard to read, you won't be allowed to do it, and you have to live with it. If you really can’t bear it, you can reorganize the code quietly and little by little, spreading the work across business tickets. Or convince the customer that this, for example, will reduce the time required to find errors, and therefore ultimately reduce costs.

Test

It is clear that testing is necessary in any project. But when working with legacy systems, special attention must be paid to testing also because the impact of changes made is not always predictable. You will need at least as many testers as developers, otherwise you should be incredibly good at automation. In our project, testing consisted of the following phases:
  1. Verification, when the implemented functionality of a feature is checked in a separate branch.
  2. Stabilization, when a release branch is checked in which all the features are merged together.
  3. Certification, when the same thing is run again on slightly different test cases in a certification environment that is as close as possible to production in terms of hardware characteristics and configuration.
And only after going through all these three phases can we make a release. Someone probably thinks that certification is an extra phase, since everything has already been clarified at the stabilization stage, but our experience suggests that this is not so: sometimes during a regression test, which is run for the second round on another machine, something somehow it will come out.

Formalize DevOps and Release

The release procedure could, for example, be as follows. When development is completed and two or three testing phases have been completed, we write an email to the DevOps team 36 hours before the expected release time. After this, we call the devops team and discuss all changes in the environments (we inform them about all changes in the database and configuration). For every change we have a document (a ticket in Jira). Then, during the release, everyone involved in this gets together, and everyone says what they are doing now: “We uploaded the database,” “We restarted such and such servers,” “We went to run regression tests in the production environment.” If something goes wrong, the release rollback procedure is launched, exactly described in the original release document - without such a document we will definitely forget something or get confused.

Control code quality

And finally, code review is a practice that for some reason is not used in all projects. It's great if every piece of code is reviewed by more than one person. Even in a very strong team, some bugs are always discovered during the code review process, and if several people look at it, the number of bugs identified increases. Moreover, sometimes the third or fourth reviewer finds the worst thing.

Tools that make writing technical documentation easier

Source: Dzone Most programmers don't like to write technical documentation. Psychology expert Gerald Weinberg even called documentation “the castor oil of programming”: developers love to read it, but they simply hate writing it themselves. Coffee break #20.  What is legacy code and how to work with it.  Tools that make it easier to write technical documentation - 2A lack of guidance or a blank roadmap results in a lack of information about how different parts of the software work. This ultimately worsens the experience for end users of the code because, in the absence of documentation, they cannot rely on the accuracy and usefulness of the product. To make it easier for programmers to form the habit of writing documentation, I recommend paying attention to four excellent tools that are now available to almost everyone.

GitHub Pages

There's probably not a single developer today who doesn't have experience working on GitHub. It's also a great place for programmers who need somewhere to store documentation. Many people use a standard Readme in their codebase to provide the user with a simple how-to guide, but this is not the only way to create documentation on GitHub. With GitHub Pages , you get more than just hosting for your project's pages, including documentation and tutorials. You get the ability to interact directly with all GitHub repositories, allowing developers to update documentation the same way they update their code. Moreover, you can use Jekyll here - it helps you transform your markup into plain text or into full-fledged web pages.

Read the Docs

As the name suggests, Read the Docs provides developers with a platform for storing and reading documentation. The service works much like GitHub Pages: programmers can make changes to documentation from their favorite version control systems, including Git, Bazaar, Mercurial and others. Automatic versioning and page creation is also supported. The best part of Read Docs is its flexibility. It supports webhooks so you can automate much of the document creation process. This is a huge time saver on a task that most programmers want nothing to do with. Additionally, everything hosted on the platform is available to the general public in a variety of formats, including PDF, single-page HTML, and even e-book format. The service takes on a significant part of the routine work to keep documentation up to date.

Tettra

Tettra is not just a platform for storing software documentation, but a complete knowledge base. It works especially well when a project involves a large group of coders working on different pieces of software. Tettra can be used to document answers to common questions.

Apiary

What makes Apiary so useful for developers is the fact that it does a great job of helping with API documentation. The platform allows users to write their documentation in Markdown , including mock API calls. Apiary also allows you to test and prototype API elements. In other words, it is a documentation tool and testing platform in one place.
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION