<p>I joined a new company about a month ago. The company is rather small in size and has pretty strong "start-up" feel to it. I'm working as a Java developer on a team of 3 others. The company primarily sells a service to for businesses/business-type people to use in communicating with each other.</p>

<p>One of the main things I have been, and will be working on, is the main website for the company - from which the service is sold, existing users login to check their service and pay their bills, new users can sign up for a trial, etc. Currently this is a JSP application deployed on Tomcat, with access to a database done thru a persistence layer written by the company itself.</p>

<p>A repeated and growing frustration I am having here (and I'm pretty happy with the job overall, so this isn't an "oh no I don't like my job"-type post) is the lack of any larger design or architecture for this web application. The app is made up of several dozen JSP pages, with almost no logic existing in Servlets or Beans or any other sort of framework. Many of the JSP pages are thousands of lines of code, they <code>jsp:include</code> other JSP pages, business logic is mixed in with the HTML, frequently used snippets of code (such as obtaining a web service connection) is cut and paste rather than reused, etc. In other words, the application is a mess.</p>

<p>There have been some rumblings within the company of trying to re-architect this site so that it fits MVC better; I think that the developers and higher-ups are beginning to realize that this current pattern of spaghetti code isn't sustainable or very easily scalable to add more features for the users. The higher-ups and developers are wary of completely re-writing the thing (with good reason, since this would mean several weeks or months of work re-writing existing functionality), but we've had some discussions of (slowly) re-writing certain areas of the site into a new framework.</p>

<p>What are some of the best strategies to enable moving the application and codebase into this direction? How can I as a developer really help move this along, and quickly, without seeming like the jerk-y new guy who comes into a job and tells everyone that what they've written is crap? Are there any proven strategies or experiences that you've used in your own job experience when you've encountered this sort of thing?</p>

[{'answer_id': 40262, 'author': 'Eric Z Beard', 'author_id': 1219, 'author_profile': 'https://Stackoverflow.com/users/1219', 'pm_score': 5, 'selected': True, 'text': '<p>Your best bet is probably to refactor it slowly as you go along.  Few us of have the resources that would be required to completely start from scratch with something that has so many business rules buried in it.  Management really hates it when you spend months on developing an app that has more bugs than the one you replaced.</p>\n\n<p>If you have the opportunity to build any separate apps from scratch, use all of the best practices there and use it to demonstrate how effective they are.  When you can, incorporate those ideas gradually into the old application.</p>\n'}, {'answer_id': 40274, 'author': 'Joel Coehoorn', 'author_id': 3043, 'author_profile': 'https://Stackoverflow.com/users/3043', 'pm_score': 2, 'selected': False, 'text': '<p>In my experience, the "elegance" of an app typically has more to do with the database design than anything.  If you have a <em>great</em> database design, including a well-defined stored procedure interface, good application code tends to follow no matter what platform you use.  If you have <em>poor</em> database design, no matter what platform you use you\'ll have a very difficult time building elegant application code, as you\'ll be constantly compensating for the database.</p>\n\n<p>Of course, there\'s plenty of room between <em>great</em> and <em>poor</em>, but my point is that if you want good application code, start by making sure your database is up to snuff.</p>\n'}, {'answer_id': 40280, 'author': 'Dan Cramer', 'author_id': 3274, 'author_profile': 'https://Stackoverflow.com/users/3274', 'pm_score': 2, 'selected': False, 'text': "<p>This is harder to do in applications that are only in maintenance mode because it is hard to convince management that rewriting something that is already 'working' is worth doing. I would start by applying the principles of MVC to any new code that you are able to work on (ie. move business logic to something akin to a model, put all your layout/view code in one place)</p>\n\n<p>As you gain experience with new code in MVC you can start seeing opportunities to change the existing code subtly so that it also falls in line. It can be a very slow process, but if you can show the benefits of this way of doing it then you will be able to convince others and get the entire team on board.</p>\n"}, {'answer_id': 40286, 'author': 'Domenic', 'author_id': 3191, 'author_profile': 'https://Stackoverflow.com/users/3191', 'pm_score': 2, 'selected': False, 'text': "<p>I would agree with the slow refactoring approach; for example, take that copy-and-pasted code and extract it into whatever the appropriate Java paradigm is (a class, perhaps? or better yet, use an existing library?). When your code is really clean and terse, yet still lacking in overall architectural strategy, <em>then</em> you'll be able to make things fit into an overall architecture much more easily.</p>\n"}, {'answer_id': 40381, 'author': 'bpapa', 'author_id': 543, 'author_profile': 'https://Stackoverflow.com/users/543', 'pm_score': 0, 'selected': False, 'text': "<p>The best way is to print out the code, crumple it up, and throw it out. Don't even recycle the paper.</p>\n\n<p>You've got an application that is written in 1,000+ line long JSPs. It probably has a god-awful Domain Model (if it even has one at all) and doesn't just MIX presentation with business logic, it BLENDS it and sits there and keeps stirring for hours. There is no way to take out the code that is crappy and move into an MVC Controller class and still be doing the right thing, you'll just end up with a MVC app with an anemic domain model or one that has stuff like Database calls in Controller code, you're still failing. </p>\n\n<p>You could try a new app that does the right thing and then have the two apps talk to each other, but that's new complexity in itself. Also you're likely going to be doing the same amount of work that you were going to do if you just started from scratch, but you might have an easier time trying to convince your bosses that this is  a better approach. </p>\n"}, {'answer_id': 40762, 'author': 'John Channing', 'author_id': 3305, 'author_profile': 'https://Stackoverflow.com/users/3305', 'pm_score': 2, 'selected': False, 'text': '<p>First pick up a copy of Michael Feather\'s Working <a href="https://rads.stackoverflow.com/amzn/click/com/0131177052" rel="nofollow noreferrer" rel="nofollow noreferrer">Effectively with Legacy Code</a>.  Then  identify how best to test the existing code.  The worst case is that you are stuck with just some high level regression tests (or nothing at all) and If you are lucky there will be unit tests.  Then it is a case of slow steady refactoring hopefully while adding new business functionality at the same time.</p>\n'}, {'answer_id': 65645, 'author': 'James A. N. Stauffer', 'author_id': 6770, 'author_profile': 'https://Stackoverflow.com/users/6770', 'pm_score': 1, 'selected': False, 'text': '<p>Iteratively refactor.  Also look for new features that can be done completely in the new framework as a way to show the value of the new framework.</p>\n'}, {'answer_id': 69121, 'author': 'MetroidFan2002', 'author_id': 8026, 'author_profile': 'https://Stackoverflow.com/users/8026', 'pm_score': 1, 'selected': False, 'text': "<p>My suggestion would be to find the rare pages that do not require an import of other JSPs, or have very few imports.  Treat each JSP imported as a black box, and refactor these pages around them (iteratively, testing each change and making sure it works before continuing).  Once these are cleaned up, you can continue finding pages with more and more imports, until finally you have refactored the imports.</p>\n\n<p>When refactoring, note the parts that try to access resources not given to the page and try to take this out to a controller.  For instance, anything that accesses the database should be inside a controller, let the JSP handle the display of the information the controller gives to it via a forward.  In this way you will develop several servlets, or things like servlets, for each page.  I would suggest using a front-controller based framework for this refactoring (from personal experience I recommend Spring and its Controller interface), so that each controller isn't a separate Servlet but is rather delegated to from a single servlet that is mapped appropriately.</p>\n\n<p>For the controller, it is better to do database hits all at once rather than attempt them piecemeal.  Users can and generally do tolerate a page load, but the page output will be much faster if all the database data is given to the rendering code rather than the rendering code hanging and not giving data to the client while it is trying to read yet another piece of data from the database.</p>\n\n<p>I feel your pain and wish you luck in this endeavor.  Now, when you have to maintain an application that abuses Spring Webflow, that's another story :)</p>\n"}]
['https://Stackoverflow.com/questions/40242', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4249/']