Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,152,738 members, 7,817,030 topics. Date: Saturday, 04 May 2024 at 12:00 AM

General Strategies For Converting Legacy Code? - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / General Strategies For Converting Legacy Code? (2418 Views)

Converting .php To .exe / Converting Picture To Byte Array(blob) And Back To Picture / Converting Stand-alone Application To Network-based(client-server) (2) (3) (4)

(1) (Reply) (Go Down)

General Strategies For Converting Legacy Code? by ektbear: 10:00pm On Nov 26, 2011
You've just inherited a legacy codebase in an ugly language like PHP.

No tests have been written for this codebase. It isn't commented well. The author has global variables all over the place which he is modifying willy-nilly in functions, so it is hard to understand what is going on.

He also hasn't separated his business logic from his display.

You are tasked with improving the codebase and also adding new features.

How do you attack this problem?

What general strategies do you find useful?
Re: General Strategies For Converting Legacy Code? by Fayimora(m): 1:03am On Nov 27, 2011
To be HONEST I would re-write the whole application. I hate that word 'legacy'.
Re: General Strategies For Converting Legacy Code? by BCuZiMBlaCk(m): 10:06am On Nov 28, 2011
Damn right
Re: General Strategies For Converting Legacy Code? by candylips(m): 12:43pm On Nov 28, 2011
use the refactor facility in an IDE to refactor in small chunks and write extensive test to make sure you haven't broken anything
Re: General Strategies For Converting Legacy Code? by kodewrita(m): 1:24pm On Nov 28, 2011
Before tackling the huge tacky ball of weave called the backend, I would try to work on the frontend,

--look for echo statements printing literals in the code and simply convert them to PHP escape statements e.g echo "<head>" to ?> <head><?php .

--for some functions, I'll see if I can return the output as variables instead of echoing straight from the function. This would be a step before actually ==saving a copy in a different folder with function calls replaced with appropriate variables or placeholders
==bringing in smarty templates or plain old preg_replace to convert the variables into text.

--most old code especially PHP3.x ish have repeated code. Lots of copy and paste ish. I'll look through carefully for functionality repeated in many places and then start replacing them with a one-line function call to my new function with differing arguments.

--If its not live, I can try tracing the flow of the application by placing echo statements everywhere (I actually use this for debugging) e.g

function xyz(){
yada yada yada( lines of code originally there).
echo "function xyz, present sir"; // :-) you get the idea
}

--tracing the flow will help me know how to combine all the scattered code. Essentially, you need to look for code that is common to many scripts and bring them back to the same place. They will mainly be of two types:
==repeated lines of code
==configuration variables. old code tends to rely on globals for this but you may simply see it as repeated variables (you might as well put these in a central file and include them everywhere). i think.

I also try to work one user story at a time. So for creating a user post, I would refactor all functions relating to that single action before moving on to the next action.

candylips:

use the refactor facility in an IDE to refactor in small chunks and write extensive test to make sure you haven't broken anything

do that after each major change.

Anything longer than 20 lines is candidate for refactoring except:
==I dont understand it or

And if you have the special luck to be blessed with a full page of "functionless" PHP script, I would suggest trying to extract an API from it.
Re: General Strategies For Converting Legacy Code? by moderatorr: 2:16am On Nov 30, 2011
1. List all the features of the legacy app
2. Modify the database
3. Rewrite the entire app!
Re: General Strategies For Converting Legacy Code? by Nobody: 7:19am On Nov 30, 2011
^^^Exactly. Because by the time you patch one place, another place go tear again.
Re: General Strategies For Converting Legacy Code? by Xenisha: 3:10pm On Nov 23, 2022
Frankly, in my experience, there is no such thing as software that doesn’t have bugs , check legacy code meaning , and it is more a matter of experience with the particular project(s) involved to hunt them down.

As long as you have access to the relevant source code and can adjust code accordingly (without compromising the original design), it makes relatively little difference whether it’s new code or legacy code.

(1) (Reply)

Nse (nigerian Stock Exchange) Api Javascript Sdk / Free Source Code Of Web Sms / .

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 16
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.