Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Rewriting 15-Year-Old Code

Rewriting 15-Year-Old Code

Have you ever had to maintain a 15-year-old application? Dead code everywhere, database queries in between HTML tags, and some pages still in PHP 3. This strategy-level presentation will lead you through a progressive rewrite, from very old legacy to the latest shiny version of PHP. Learn how to automate legacy testing, how to seamlessly jump between the old and new parts, and how to overcome other challenges that arise from dealing with legacy.

Anna Filina
PRO

December 06, 2016
Tweet

More Decks by Anna Filina

Other Decks in Programming

Transcript

  1. foolab.ca | @foolabca
    Rewriting

    15-Year-Old Code
    ConFoo, Vancouver - December 6, 2016

    View Slide

  2. Anna Filina
    • Project rescue expert.
    • Fix performance issues.
    • Refactor legacy code.
    • Workshops & advice on legacy,
    testing, Symfony, API.
    • FooLab + ConFoo
    2

    View Slide

  3. You Inherited a

    15-Year-Old Codebase
    Code Doesn't Age Like Wine

    View Slide

  4. The Web Was 600px Wide
    4

    View Slide

  5. Feeling Old Yet?
    • Y2K bug.
    • Internet Explorer 5.
    • ICQ was starting to be cool.
    • Rounded corners = status symbol.
    5

    View Slide

  6. Code Smells

    View Slide

  7. Mixed Concerns
    mysql_query('...');
    $total = 0;
    foreach ($products as $p) {
    $total += $p['qty'] * $p['price'];
    }
    ?>
    Total: = $total ?>
    7

    View Slide

  8. Global Functions & Constants
    include("functions.php");
    MyClass::myMethod(); // namespaced function?
    myMethod(DEV_MODE);
    8

    View Slide

  9. False Security
    $clean = htmlentities($_GET['password']);
    // use filter extension or a framework
    // use prepared statements
    9

    View Slide

  10. Inexplicable Conditions
    if ($order_id > 20117) {
    // use this sql
    } else {
    // use that sql
    }
    // weakness = time to refactor
    10

    View Slide

  11. Long Methods
    public function importCsv($path, $googleApiKey, $databaseDsn, $databaseUser, $databasePassword)
    {
    // Convert CSV to array of conferences
    $lines = file($path);
    $csv = array_map('str_getcsv', $lines);
    $conferences = [];
    foreach ($csv as $line) {
    $conference = new Conference();
    $conference->name = $line[0];
    $conference->location = $line[1];
    $conferences[] = $conference;
    // Get coordinates for location
    $location = urlencode($conference->location);
    $url = 'https://maps.googleapis.com/maps/api/geocode/json?address='.$location.'&key='.$googleApiKey;
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $response = curl_exec($curl);
    curl_close($curl);
    $json = json_decode($response);
    if (count($json->results) == 0) {
    continue;
    }
    $latitude = $json->results[0]->geometry->location->lat;
    $longitude = $json->results[0]->geometry->location->lng;
    $coordinates = $latitude.','.$longitude;
    $conference->coordinates = $coordinates;
    // Save conference to database
    $pdo = new PDO($databaseDsn, $databaseUser, $databasePassword);
    $statement = $pdo->prepare('REPLACE INTO conference (name, location, coordinates) VALUES (?, ?, ?)');
    $statement->execute([
    $conference->name,
    $conference->location,
    $conference->coordinates,
    ]);
    11

    View Slide

  12. Average Legacy Code File
    • 3000-6000 lines of code.
    • Half of it is commented "in case we need it later".
    • Method length of 800 lines.
    • Abuse of helper classes.
    • Sometimes no classes at all.
    12

    View Slide

  13. Codebase
    • 5,000 classes.
    • 20,000 methods.
    • 1,500,000 lines of code.
    13

    View Slide

  14. Before You Code

    View Slide

  15. Strategy
    • Make a strategy based on
    constraints.
    • Full rewrite vs progressive:
    ◦ By class.
    ◦ By feature/module.
    ◦ By HTTP call.
    • How to run code side-by-side:
    ◦ Session sharing.
    ◦ mod_rewrite.
    15

    View Slide

  16. Data Can Be Lost, Stuff Can Break
    • Backup: test restore.
    • Nullify sensitive data.
    • Staging: simulate deployments/
    upgrades/batch processes.
    • Automate tests before code
    changes.
    • Make a risk assessment:
    ◦ Don't be too optimistic.
    ◦ Account for side-effects.
    16

    View Slide

  17. Rewrite Example

    View Slide

  18. PHP 3 to PHP 5.6
    • HTML + PHP + SQL in same file.
    • Includes all over the place.
    • IFs that concatenate SQL.
    • Previous rewrite attempt:
    ◦ Failed, made things worse.
    ◦ Folders of dead code.
    ◦ Classes with static functions (no instances).
    18

    View Slide

  19. Solution
    • Rewrite complex forms in Symfony:
    ◦ mod_rewrite for concerned pages.
    • Rewrite biggest module as OOP:
    ◦ Design extraction.
    ◦ Flexible architecture.
    ◦ Automated tests.
    19

    View Slide

  20. Design Extraction

    View Slide

  21. Avoid Code Bias
    • Old code → design docs.
    • Validate design docs:
    ◦ Clarify business rules.
    • Improve design:
    ◦ Reduce technical debt.
    ◦ More flexible.
    • Design docs → new code.
    21

    View Slide

  22. Fixing Bugs

    View Slide

  23. Code Duplication
    • Sometimes, the bug is repeated
    80+ times.
    • Remove duplications ASAP.
    23

    View Slide

  24. Fix Long Methods
    • Extract broken part into its own method.
    • Write unit tests for it.
    • Fix it.
    • Call it from the mega-method.
    24

    View Slide

  25. Spot Logical Groups
    25
    Code block
    Comment
    Code block
    Comment
    Code block
    Comment
    Convert CSV to array of conferences.
    Get coordinates for conference location.
    Save conference to database.

    View Slide

  26. Extract
    26
    Code block
    Comment
    Code block
    Method
    Code block
    Comment
    Call method

    View Slide

  27. Name
    27
    Code block
    Method
    Code block
    Method
    Call method
    Call method
    Code block
    Method
    Call method
    getConferencesFromCsv
    getLocationCoordinates
    saveConference

    View Slide

  28. Rewrite Example #2

    View Slide

  29. ASP Classic to PHP 5.6
    • 15+ spaghetti and hacks.
    • Language no longer supported.
    • Huge ERP with lots of code.
    29

    View Slide

  30. Solution
    • Rewrite page by page to framework.
    • mod_rewrite for concerned pages.
    • DB session adapter in both apps.
    • Page in any language = HTTP request.
    ◦ Guzzle tests FTW!
    30

    View Slide

  31. Guzzle Tests
    31

    View Slide

  32. Stuck?

    View Slide

  33. Try Something New
    • Bounce ideas.
    ◦ New people to avoid tunnel vision.
    • Has this been done before?
    • Can I try another approach?
    33

    View Slide

  34. Takeaways
    • Make a strategy.
    • You touch it, you refactor it.
    • Use known tools & methodologies.
    • Get inspiration from others.
    • Refactoring gets easier.
    • Every problem has a solution.
    34

    View Slide

  35. @afilina afilina.com

    View Slide