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

Climbing out of the legacy quicksand

Climbing out of the legacy quicksand

Are you working with a legacy application? Whether you inherited it or if the project just grew out of proportion over the years it can be daunting. The thought of making changes or bug fixing can be enough to give you nightmares. No fear you can take a sigh of relief. All is not lost.

A few years ago I found myself working on a legacy project. It wasn't pretty. It was very challenging but I made it through.

And when the next project and the next one and the next and the one after that were also legacy systems, I soon found it much easier to work on them by being more methodical.

Antonis Pavlakis

December 02, 2016
Tweet

More Decks by Antonis Pavlakis

Other Decks in Programming

Transcript

  1. Climbing out of the
    legacy quicksand
    Antonis Pavlakis
    @pavlakis | pavlakis.uk
    PHP East Midlands December 2016
    @PHPem

    View Slide

  2. What is legacy ?

    View Slide

  3. “Legacy code is source code that relates to a
    no-longer supported or manufactured
    operating system or other computer
    technology. ”
    –Wikipedia (1/3)
    https://en.wikipedia.org/wiki/Legacy_code

    View Slide

  4. “…source code inherited from someone else…”
    –Wikipedia (2/3)
    https://en.wikipedia.org/wiki/Legacy_code

    View Slide

  5. “… source code inherited from an older version
    of the software.…”
    –Wikipedia (3/3)
    https://en.wikipedia.org/wiki/Legacy_code

    View Slide

  6. And can also be…
    * Insecure,
    * difficult to read,
    * difficult to test,
    * hard to maintain,
    * spaghetti code,
    * etc…

    View Slide

  7. Some positives…
    It’s been tried and tested
    Users know how it works
    Users are aware of the quirks and
    workarounds

    View Slide

  8. Let’s get started…

    View Slide

  9. This talk is about…
    TDD

    View Slide

  10. This talk is about…
    TDD

    View Slide

  11. This talk is about…
    TRR ?

    View Slide

  12. T
    est

    View Slide

  13. Refactor

    View Slide

  14. Repeat

    View Slide

  15. TDD
    done backwards?

    View Slide

  16. But… Legacy?!!

    View Slide

  17. Source: https://i0.wp.com/media2.slashfilm.com/slashfilm/wp/wp-content/images/Doctor-Strange-T
    railer-breakdown-6.png

    View Slide

  18. Let’s look at an example

    View Slide

  19. Example #1 - Authentication
    Enter some information
    Submit a form

    View Slide

  20. Example #1 - Authentication
    Queries the Database
    Returns a result

    View Slide

  21. Example #1 - Authentication
    Inputs
    Application
    Outputs

    View Slide

  22. Black Box
    T
    esting

    View Slide

  23. Vagrant or Docker ?
    I still use Vagrant
    But… it doesn’t really matter.

    View Slide

  24. Development Environment
    If using Vagrant
    Have a custom box
    Use provisioning just for the application
    Expose Database port
    Enable MySQL logging

    View Slide

  25. Expose DB port
    Edit /etc/my.cnf in the VM and within the [mysqld]
    section, add:
    bind-address = 0.0.0.0

    View Slide

  26. Expose DB port
    Give DB user privileges to access DB from anywhere:
    use mysql;
    update user set host='%' where user=‘app_user' and
    host='127.0.0.1';
    flush privileges;

    View Slide

  27. Expose DB port
    Add port forwarding to Vagrantfile
    config.vm.network "forwarded_port",
    guest: 3306, host: 3306, auto_correct: true

    View Slide

  28. Enable MySQL logging
    general_log = 1
    general_log_file = /vagrant/mysql.log
    Edit /etc/my.cnf in the VM and within the [mysqld]
    section, add:

    View Slide

  29. T
    esting Environment
    Can be a separate box

    View Slide

  30. T
    esting environment
    A lightweight environment (Vagrant, Docker,
    OS native)
    Initially we just need PHP (so can use latest
    version)

    View Slide

  31. T
    ools

    View Slide

  32. A php framework for autotesting your business
    expectations.
    http://behat.org/en/latest/

    View Slide

  33. T
    est behaviour
    Complete fields
    Submit form
    Check database
    T
    est response

    View Slide

  34. Checking the Database
    Can use a shell script
    Call it through Behat

    View Slide

  35. DB Shell Script

    View Slide

  36. Checking the Database

    View Slide

  37. Example #1
    T
    ests Passed! ✅

    View Slide

  38. Refactor

    View Slide

  39. Xdebug is your friend

    View Slide

  40. Dependency Injection
    Container

    View Slide

  41. DIC
    Get DB
    Get Request object
    Get customer authentication model
    Get Authentication service

    View Slide

  42. DIC
    $di = new My_App_DiContainer();
    $auth = $di->get(‘customer.auth’);
    $auth = (new \My\App\DiContainer)->get(‘customer.auth’);

    View Slide

  43. Example #1
    T
    ests Passed! ✅

    View Slide

  44. Autoloading…

    View Slide

  45. Composer
    Compatibility: PHP 5.3.2
    PHP 5.3 release date: 30 June 2009

    View Slide

  46. Composer
    composer install --optimize-autoloader

    View Slide

  47. Git

    View Slide

  48. Composer using Git

    View Slide

  49. Decouple all the things

    View Slide

  50. DRY?
    I don’t mind
    Do Repeat Yourself

    View Slide

  51. Start Fresh!

    View Slide

  52. Only for this feature

    View Slide

  53. BDD Time
    (yes, we’re still doing TDD)

    View Slide

  54. Domain Knowledge
    Understanding the system…
    (we’re still on Example #1)

    View Slide

  55. Information gathering…

    View Slide

  56. What do we need to know?
    Do users have different roles?
    What happens if …
    Login fails
    User forgets their password

    View Slide

  57. What do we need to know?
    Do users have different roles? We have two types of users.
    Customers and Staff members
    What happens if …
    Login fails
    A Customer’s account will lock for 5 minutes if they
    enter a wrong password more than 3 times.
    User forgets their password - A Customer can reset their
    password

    View Slide

  58. Ubiquitous language
    Identify the terminology the Users use
    Use the same language/terminology the user
    (or client) uses to describe the system

    View Slide

  59. Keep asking questions

    View Slide

  60. Domain Knowledge
    Understand what the feature was designed to
    do
    How it is supposed to work
    How it actually works
    Any special quirks or edge cases

    View Slide

  61. Create a new application

    View Slide

  62. Write Unit T
    ests!
    100% T
    est Coverage!

    View Slide

  63. T
    est
    Ensure initial Behat tests still pass!

    View Slide

  64. Example #1
    T
    ests Passed! ✅

    View Slide

  65. Resources
    Modernizing Legacy Applications in PHP - https://
    leanpub.com/mlaphp
    T
    est Legacy apps with Behat - https://
    speakerdeck.com/agpavlakis/test-legacy-apps-
    with-behat
    DB Shell Script - https://github.com/pavlakis/
    phpnw15-behat-tutorial/blob/scripts/scripts/db.sh

    View Slide

  66. Resources
    Unglue all the things - https://beau.io/talks/
    2016/09/16/unglue-all-the-things-symfony-
    live-london-2016
    mysql_* to mysqli_* conversion - https://
    github.com/philip/MySQLConverterT
    ool

    View Slide

  67. Thanks!
    @pavlakis | pavlakis.uk
    https://joind.in/talk/33e19

    View Slide