Live data from Hacker News

Ask HN: I just inherited 700K+ lines of bad PHP. Advice?

news.ycombinator.com

41–50 of 78 posts

Re: Ask HN: I just inherited 700K+ lines of bad PHP. Advice?

#41
post #35

What is most important is what are you trying to achieve. Are you trying to make the system as stable as possible at the lowest cost to the client or are you trying to bring this system into a future proof state and the client is willing to pay for that? Personally if I were in your position I would explain to the client that I'm happy to temporarily fix some bugs but long term the system needs to be rewritten. 700k…

I've spent quite some time the cross-component spaghetti code large companies sometimes write. I've come to believe that the skill of not rewriting from scratch but forcing yourself to slowly refactor (as per Martin Fowler's definition) existing systems into a proper state is one of the most important skills you can develop. That way, once you've refactored most of the system (which includes adding tests for all the…

Second this. The temptation to rewrite from scratch is to be avoided; the code is going to be a mass of edge-cases, and you can't spot them all at once. Rewriting will take just as long as refactoring and introduce new bugs instead of killing old ones.

Re: Ask HN: I just inherited 700K+ lines of bad PHP. Advice?

#42

* Rewrite. * NEVER modify existing one. Once you change one line of comment, you own all the code and problem from that point. * If rewrite is not allowed, then ask huge pay raise for this work. Basically it is not about money, it is about bring everyone on the same page on the status of he existing solution. * If the above does not work out, prepare to switch to another project, or quit the job totally.

There is absolutely no way to rewrite a million lines of business logic without ending up with an even bigger mess. See also: http://www.joelonsoftware.com/articles/fog0000000069.html

Re: Ask HN: I just inherited 700K+ lines of bad PHP. Advice?

#43
post #34
post #24

1. Get it onto version control. 2. Make sure there is some workable strategy for deploying and testing the code. 3. Ask somebody to provide you with a list of the changes, or else try to create some kind of diff against the original version of the code. If you can see crazy stuff here then find out who did it... 4. Ask somebody what the biggest bugs are? Which things are causing clients the most problems? 5. Try to e…

First read the Fowler's 'Refactoring' book; it was written just for you. Then: 1. Identify a small and easily separable piece of code (what you woud call a component in a normal system.) 2. Write tests covering every (important?) edge-case of the piece of code you want to rewrite. 3. Mercilessly refactor until it's nice and squeeky clean. 4. Lather, rinse and repeat. And of course, make sure your client acknowledges…

> First read the Fowler's 'Refactoring' book; it was written just for you.

"Refactoring" is not the tool for the job, although it's a nice sidearm.

What OP needs is the big gun, Feathers's "working effectively with legacy code": http://www.amazon.com/Working-Effectively-Legacy-Michael-Fea...

As the title hints, it was written specifically and expressly for the "I just got a huge amount of complete shit of a codebase shoved unto me, how do I survive". Just check the TOC of part 2 (the meat of the book): http://my.safaribooksonline.com/book/software-engineering-an...

> And of course, make sure your client acknowledges that it's a giant clusterf...

That's hugely important. No promises of delivery, and that the client understands it's not a cakewalk.

Re: Ask HN: I just inherited 700K+ lines of bad PHP. Advice?

#44
* Situation like this is not only technical issue any more. Your solution needs to reflect that.

* This is not best situation to be in, but if you learn to deal with this and emerge from it. This experience will make you so much stronger. So be ready to quit, but do not quit too early.

Good luck!

(I replied earlier, but the above two points are so important that they worth a different post.)

Re: Ask HN: I just inherited 700K+ lines of bad PHP. Advice?

#45
post #9

I would start by spending a week or two wrapping the application in Acceptance tests (cucumber/capybara) just so when you do make a change you are able to quickly find out to a semi decent level of confidence things are ok. I would also recommend Working Effectively with Legacy Code By Robert C. Martin. Good luck!

"Working Effectively with Legacy Code" is by Michael Feathers. http://www.amazon.com/Working-Effectively-Legacy-Michael-Fea...

Re: Ask HN: I just inherited 700K+ lines of bad PHP. Advice?

#46
This is a case where having a defined development process and good sharp tools can be very helpful. Here are the steps I'd use to tackle this problem code (though it's based on what you wrote above and might need to be adapted as you learn more.

1) Study how the software is actually used and design the "ideal architecture" (this may be a moving target).

2) Get the software into a version control system.

3) When a section of the code needs work, first write tests that pass for the current functionality of the module but fail for the behavior you're trying to fix.

4) As you repair code in step 3, also migrate the code "towards" your preferred architecture ... this is going to be a very gradual process so don't try to complete it in one step and use your tests to verify you haven't broken the system. This is also a good time to start inserting patterns like MVC/MVP as it will help. - http://c2.com/cgi/wiki?TestEveryRefactoring

5) When you've found "reams of copy+paste code", refactor that code into utility classes (files, whatever). - http://martinfowler.com/refactoring/

6) Establish processes for migrating the database both forwards and backwards between versions (you'll need a rollback someday).

7) Treat the database schema as source code and refactor it as you work. It sounds like you're a long way from being able to use an ORM, but have a plan for migrating the database towards the day you can. - http://martinfowler.com/articles/evodb.html

8) Get the PHP code out of the database ... that's going to be painful but worthwhile.

9) Get some help! I've used the Sonar source code quality analysis tools on Java projects for years. There's a PHP plugin for it here (http://docs.codehaus.org/display/SONAR/PHP+Plugin) and it will help you determine what areas might be worth targeting. It also helps by establishing style and practice rules that will help get a team coordinated.

One of the hallmarks of a project like this is that coding styles changed dramatically during the project's existence - Establishing a style guide (including patterns and forbidding anti-patterns) can be very helpful.

So in short ... don't "run screaming" but rather sit and think when you feel overwhelmed. If you can solve a complex problem when writing source code, you can also solve systemic problems.

Good luck!

Re: Ask HN: I just inherited 700K+ lines of bad PHP. Advice?

#47

So the cold reality for this client is that the codebase will have to be replaced over time. You are not trying to escape legacy simply for the lure of something new, you are trying to escape insanity. I would talk to the client about focusing your effort on helping them transition--a small piece at time--to a sane architecture. If they aren't open to that, they aren't your client. I've been a business leader in a po…

This is the beauty of the web- the transition to a sane architecture can be done page by page- without any visibility to the user. Every time you have to make a change- fix an old feature or add a new one, you replace it with the new architecture. Even if it's not a whole page- you can load in a partial with javascript. The challenge is holding back from going too deep on the refactoring all at once. Functional tests around the system have to be added to keep your sanity as part of the change process.

Re: Ask HN: I just inherited 700K+ lines of bad PHP. Advice?

#48
Take a few steps back and relax. If you were looking at this from the Space Station, who would you say has a problem with the code base? That's right, your client. Not you. Your client.

If he/she has 150 installs and 150 angry clients he/she knows that this thing is rotten somewhere. You client may or may not have some technical understanding but rest assured that they understand business.

Life often boils down to binary decision. You have two choices. Gracefully exit and move on or try to help your client.

If you choose option B you've also made another choice: Your first job is NOT to be a programmer. No, you are going to have to be a teacher.

You have to do your best to explain to your client why he might be sitting on a ticking time bomb (or whatever you might want to call it). It is imperative that your client understand that he has handed you an ugly, stinking, putrid and smelly mess. Without client buy-in I would walk away.

Now, here's the challenge: You have to find a way to communicate the problem that is not menacingly full of CS jargon and acronyms that mean exactly zero to your client.

I've had to deal with these kinds of problems before. On one or two occasions I made the mistake of not securing an understanding with my client and suffered the consequences. These were miserable walking-through-feces-infested-mud experiences. Never again. Once I learned that lesson things changed. My most memorable experience was when I got client buy-in from a major international corporation and, once they realized that they had a huge problem, they put me up at the Waldorf Astoria in Manhattan for a full month (these guys are so big that they have rooms pre-paid for "emergencies"). Imagine a guy in a t-shirt, jeans and sandals showing up at the Astoria. I've never been looked at like that before. Once they realized who my employer was things changed. Fuck, the room had marble and gold-plated crap everywhere.

But I digress, the point of that last example is that once a client understand the degree of the problem in their hands things change. If having a solution to this problem is important enough there is no end to what they will spend to fix it. Is it a business-killing problem? Even better.

Judging from your description my proposal to your client --after they really, really get it-- is to re-write their entire app from scratch.

I would further propose that you are going to need to hire a few more people (two to five?) in order to get this done as quickly as possible. And, yes, this will be expensive.

You can use many analogies to explain the problem. I'll leave that up to you. I've used ideas like that of constructing a building on a foundation of sand rather than concrete while using substandard supplies rather than industry-accepted good quality building components. Whatever analogy you use, it has to convey the severity of the problem without resorting to CS. If your client has some technical chops you can get into it a little AFTER you are done with your analogy.

Finally, the most important part: You have to be willing to walk away from it. You state the problem and explain that it will be expensive. You also state that you are not interested in anything other than a full re-write of the app because you are not in the business of doing further damage to your clients. Respectfully suggest that without full buy-in you'll need to move on and he will need to find another developer who might we willing to patch this thing up.

In many ways, it's that simple. Two choices.

Re: Ask HN: I just inherited 700K+ lines of bad PHP. Advice?

#49
If you haven't done anything like this for the past I would say that as much as it is a pain in the ass you probably can also learn a lot from it and you will get out of the job with a lot of experience in refactoring, testing, bugfixing and deploying. You could also see it as a chance to establish a long lasting relationship and a boost in confidence and salary if you do it right.

Re: Ask HN: I just inherited 700K+ lines of bad PHP. Advice?

#50
The first thing I would do, before doing any work, would be to sit down with the client/your boss and explain just how bad the situation is, that drastic solutions are in order, and that it will take years to get this under control (with 1m LOC and 150 clients presumably all running customised software, this would take years to sort out even with a large team working on it). Unless they understand that from the beginning you will never get the backing you need to sort this out.

This might be one of the few occasions where a complete rewrite is justified (if you can keep scope limited to reproducing what you have). You've said the code is incredibly complex, and if the problem domain is incredibly complex too, you're probably stuck refactoring. If the problem domain is pretty simple (a CRM without too many extra features might be), you may be better starting with your smallest client who uses the product the least, asking for all the pain points, and things they love, about the current software, and writing a simple CRM to cover their needs which replicates the features of the current product, then gradually porting other clients over to the new system and adding new features to it, while keeping the old code-base in maintenance mode and fixing serious bugs only. If you do a rewrite you'd have to port the 150 clients over 1 by 1, and leave the other code in maintenance mode - your primary client may not be at all happy with that.

If that's not possible, you'll have to refactor it slowly while keeping the code in place, so the first step is to get it into version control, sort out a sane deployment strategy with testing servers, then try improving some small isolated areas of the code for one of the clients in isolation. Good luck!

Post reply on HN