What is the best way to port an existing messy webapp to elegant MVC?

I joined the new company about a month ago. The company is quite small in size and has a fairly strong "starting" feeling. I work as a Java developer in a team of 3 people. The company mainly sells the service to business / business type people who are used in communication with each other.

One of the main things that I have done and will work is the main site for the company - from which the service is sold, existing users register to check their service and pay their bills, new users can register for a trial version, etc. This is currently a JSP application deployed on Tomcat, with access to a database run through a persistence layer written by the company itself.

The recurring and growing disappointment that I am experiencing here (and I am very pleased with the work in general, so it’s not “oh no, I don’t like my job”), it’s the absence of any larger design or architecture for this web application . The application is composed of several dozen JSP pages, with virtually no logic existing in Servlets or Beans, or in any other structure. Many of the JSP pages are thousands of lines of code, they are jsp:include other JSP pages, business logic mixes with HTML, frequently used code fragments (for example, getting a connection to a web service) are cut and pasted, not reused, etc. d. In other words, the application is a mess.

There were some troubles in the company trying to redesign this site to better match MVC; I think that developers and developers are beginning to realize that this existing model of spaghetti code is not sustainable or very easily scalable to add more features to users. Senior executives and developers are afraid to completely rewrite the thing (with good reason, since this would mean several weeks or months of work rewriting existing functionality), but we had some discussions (slowly) writing certain areas of the site into a new structure.

What are some of the best strategies for moving the application and codebase in this direction? How can I, as a developer, really help promote this together and quickly, without some new guy who comes to work and tells everyone that what they wrote is crap? Are there any proven strategies or experiences that you used in your own work experience when you came across such things?

+5
java design jsp architecture model-view-controller
source share
8 answers

It is probably best to reorganize it slowly when you go forward. Few of us have the resources that will be required to start completely from scratch with something that has so many business rules in it. Management really hates this when you spend months developing an application with more bugs than the one you replaced.

If you have the opportunity to create any single application from scratch, use all the best practices and use them to demonstrate how effective they are. When you can, gradually incorporate these ideas into your old application.

+10
source share

First, get a copy of Michael Peer working Effectively with Legacy Code . Then determine how best to test existing code. The worst case is that you are stuck with only some high-level regression tests (or nothing at all), and if you are lucky, unit tests will be performed. Then this is probably the case of slow refactoring while adding new business functions.

+3
source share

In my experience, the “elegance” of an application is usually more about database design than about anything. If you have a great database design, including a well-defined stored procedure interface, good application code should usually be regardless of which platform you use. If you have a poor database design, no matter what platform you use, it will be very difficult for you to build elegant application code, as you will constantly compensate for the database.

Of course, there is a lot of space between big and poor, but I want to say that if you want to get good application code, start by making your database able to sniff.

+1
source share

This is harder to do in applications that run only in maintenance mode, because it’s hard to convince management that rewriting what already works is worth it. I would start by applying MVC principles to any new code that you can work with (i.e., translate business logic to something similar to a model, put all your layout / view code in one place)

As you gain experience with new code in MVC, you can see the possibilities for modifying existing code so that it also enters the queue. This can be a very slow process, but if you can demonstrate the benefits of this method, you can convince others and get the whole team on board.

+1
source share

I would agree with a slow approach to refactoring; for example, take this code with copy and paste and extract it into any suitable Java paradigm (a class, perhaps, or, even better, use an existing library?). When your code is really clean and concise, but still lacking a common architectural strategy, you can make things fit into the overall architecture much easier.

+1
source share

The best way is to print the code, crumple it and throw it away. Do not even recycle paper.

You have an application written in JSP with over 1000 lines. He probably has a terrible domain model (if any) and not just a MIX presentation with business logic, it is FREE and sits there and continues to cling for hours. It’s not possible to extract code that is crappy and move into the MVC controller class and still do the right thing, you just get an MVC application with an anemic domain model or one who has things like database calls in the controller code you still fail .

You can try a new application that will do everything right, and then the two applications will talk to each other, but this new complexity in itself. In addition, you are likely to do the same work that you are going to do if you are starting from scratch, but it may be easier for you to try to convince your bosses that this is the best approach.

+1
source share

Iterative refactor. Also find new features that can be fully implemented in the new structure to show the value of the new structure.

0
source share

My suggestion would be to find rare pages that do not require import of other JSPs or have very little import. Treat each JSP imported as a black box and reorganize these pages around them (iteratively, checking each change and making sure it works before continuing). Once they are cleared, you can continue to search for pages with large and large volumes of imports, until finally you restore the import.

When refactoring, pay attention to the parts that are trying to access resources not listed on the page, and try to bring this to the controller. For example, everything that accesses the database should be inside the controller; let the JSP handle the display of information that the controller passes to it through forward. Thus, for each page, you will create several servlets or things like servlets. I would suggest using a front-controller-based framework for this refactoring (from my personal experience I recommend Spring and its controller interface), so that each controller is not a separate servlet, but rather is delegated from a single servlet, which is displayed accordingly.

For a controller, it is best to do database hits at the same time, rather than trying them in parts. Users can and usually tolerate page loading, but page output will be much faster if all the database data is transferred to a rendering code, and not a visualization code that hangs and does not transmit data to the client while it is trying to read another piece of data from Database.

I feel your pain and wish you good luck in this endeavor. Now that you need to maintain an application that abuses Spring Webflow, this is another story :)

0
source share

All Articles