Skip to content

FrOSCon - Fixing Legacy Code

By Benjamin Eberlei and Kore Nordmann.

What Is Legacy Code

  • hard or impossible to fix errors or implement new features
  • hard or impossible to implement automatic testing

What Is The Talk About

Issues With Legacy Code

  • code issues
  • inversion of control
  • data structures
  • application or business logic
  • shared state

Code Issues

  • code size
  • high efferent coupling
  • code duplication
  • ...
  • Use phpmd to locate size or complexity errors.

Efferent Coupling

  • many outgoing coupling for one class in other classes
  • can be located with pdepend

Inversion Of Control

  • Missing abstractions
  • Inline new
  • Static calls
  • Inline life cycle management (only value objects and exceptions should be newed inline)

Data Structure

  • Missing abstractions
  • Inline new
  • Static calls
  • Inline life cycle management (only value objects and exceptions should be newed inline)

Data Structure

  • with arrays, you have a bad documentation
  • use simple value objects (also known as dto) and they are not immutable
  • split logic and try to avoid big models (seperation of concerns (roles/contextes) on model logic
  • branch by abstraction
    • introduce facade / proxy for old code
    • call old code from facade / proxy implementation
    • write new shiny facade / proxy implementation
    • use new implementation
    • delete old code

Application Or Business Logic

  • mixed business logic (application logic) in large controllers is the most common and pressing issue with legacy code (fat controllers)
  • try to use solutions like business requirement
  • extract business logic like service layer pattern
  • introduce (domain) events (in the same request)
  • introduce message queues (not in the same request)

Shared State

  • we know that global state is bad
  • sessions are (often) cross-request global state in bash maps
  • method execution pathes depending on current object state are horrible to track down

Testing Changes

  • if your legacy code is hard to test, try to test it from the http protocoll point of view
  • use behat as behaviour driven development (BDD)
  • use mink with "goute", "sahi" or "zombie.js", "selenium", "selenium 2"

Extra

  • do small checks when refactoring (good indicator are many, many commits)
  • test your primary use cases (put the chaos in the cage)
  • be bold (enough) and remove old code

FrOSCon - Beyond LAMP

First talk finished :-). No proofreading done so far. Next talk will start in a few seconds.

Its All About Scale

  • size
  • features
  • non-functional requirements
  • Language agnostic - language you are using doesn't matter

Background Tasks

User can't wait and server can't handle it

  • sending mail
  • calling 3rd party APIs / services
  • converting media (image, video, audio)
  • updating caches

Direct Approach

Don't do that since your are losing control

Slightly Less Bad Idea

  • Write jobs into sql database
  • have some workers that poll the database for jobs

Don't use it

Queus To The Rescue

  • write jobs into message queue
  • have some workers for the queue
  • many services available (ZeroMQ, RabbitMQ, Redis Pub/Sub, Amazon SQS ...)

Good idea but lot of work and hard to get right

Beanstalkd

  • create a job
  • process a job

RQ

  • simple job queue in python
  • backed by redis
  • enqeueu function call
  • run worker how stores result in database

Celery

  • python
  • multiple brokers
  • primarily python with clients for ruby and php
  • highly available (HA)
  • fast
  • flexible
  • monitoring
  • workflows
  • time and rate limits
  • scheduling
  • autoscaling

Search

Like query

  • no lingustic suport
  • no ranking
  • not indexed (mysql has fulltext index and postgresql)

Fulltext Search Engines - Solr Vs. Elastic Search

  • based on lucene
  • full linguistic support
  • faceted search
  • result highlighting
  • HTTP API
  • similar but different
  • easy to set up
  • clients available like PHP: solarium, symfony component or PHP Elastica
  • integrate search into framework (map objects into documents and back) or handeling updates and deletes

NoSQL

  • key-value stores
  • column based
  • document stores
  • graph

Errors

  • don't let it happen
  • send an email (overload mailboxes)
  • log errors
    • php symfony provides a component that you log only all messages if error occures (so only info logging if something happens)
    • properly configure logging
    • use event aggregator
      • sentry
      • errbit

Sentry (event aggregator)

  • started as OSS
  • now available as SaSS
  • udp
  • written in django
  • php client available

Errbit (event aggretator)

  • still OSS

Deployment

  • ftp
  • git pull / app server by ssh
  • but you want a automated deployment like fab deploy
  • automated deployment with webistrano (just a single button)

Wrap Up

  • use background tasks
  • use full text search
  • try to use nosql where needed
  • do error logging
  • think about deployment

web - FrOSCo 2013 - Presale gestartet

Der Presale ist gestartet! Bis zum 08.08.2013 können unter https://presale.froscon.org/ Tickets und TShirts vorbestellt werden. Der Ticketverkauf geht anschließend bis zum 17.08.2013 weiter, jedoch können dann keine TShirts mehr geordert werden.

Neu sind dieses Jahr das Supporter- und das Businessticket Plus.
Das Supporterticket kostet 42 Euro und ermöglicht es euch die FrOSCon im kleinen Rahmen zu unterstützen. Das Businessticket Plus beinhaltet neben dem Erhalt einer Rechnung, auch ein Konferenzshirt, sowie Verpflegung auf der Konferenz.
Natürlich gibt es weiterhin das normale Ticket für 5 Euro, sowie das Businessticket für 100 Euro.
Alle Informationen zu den Tickets findet ihr in unserer FAQ.


Quelle

notes - froscon 2012 - alternative php runtimes - sebastian bergmann

Following are my notes to sebastians bergmann presentation. The presentation was quite good (you can add "as usual" if you have seen sebastian bergmann more than once :-)).

#120825 sebastian bergmann

_general - he found at least 10, some ready for production - php core is threadsafe, problems are linked in extensions

_php - bytecode-based interpreter (interpiler) - php->php-bytecode->execution - "the" (official) implementation of the php language ->currently no official specification exists

phpruntime - token base - reads file character by character (scanner) - tries to figure out what to do with the words (parser) ->compilekit shows you how your code looks when it is compiled as bytecode - bytecode is is not optimized

_hiphop (only productive alternative) - tries to save power (cpu usage) and also speed up "some things" ->main reason is saving powercosts

  • php->c++->native binary
  • reimplementation of the php runtime (with common extensions) in c++
    • like filesystem, mysql (what they need to run facebook)
    • compiling takes long (very long)
    • sebastian compield phpunit for hiphop
    • faster from factor 1 to 3
    • compiling takes up to factor 65 more time on hip hop but scalls very well with cores
      • CodeError.js (hphp -> tool to transform them in xml or whatever for continious integration)
    • binaries became very large (facebook uses bittorrent for deploing) ->thats why they implement the HipHopInterpreter (factor 2 slower than php) ->shares library of hip hop but is based on php
    • optimize code by compiling
    • tries to figure out the type your variables (to set native c++ types where possible)
      • significant smaller footprint by good code
    • php unit works (with all features)
      • you have to compile your code plus php unit code plus your test code

sebastian knows two companies that are working with hiphop.

_hippyVm - php interpreter implemented using PyPy - faceboog sponsored study by 1 person and 2 months - get as close to php as possible - is it feasible to implement a php runtime using PyPy ->it is possible to implement 80 percent of php in 20 percent of the time

_DaVinci Machine Project (MLVM) - Multi-Language renaissance for the JVM - First-Class Architectural support for languages other than java - Facebook is working on (or at least thinking about) PHP on MLVM - Isn't the first (and not the last) php runtime implementation on jvm

_HappyJIT - academic investigation - PHP Interpreter implemented using PyPY - PHP->Zend Parser->PHP Bytecode->APC->BcParser->HappyJIT Bytecode - tries to optimize php bytecode - they have to use APC to get the bytecode

_php.js - php interpreter implemented in java script - same 80/20 rule

_phalanger - php->common intermediate language (CIL - microsoft .net code) - rewrite underway to leverage the dynamic language runtile (dlr) - started as reasearch projcet

_php compiler (phc) - academic investigation - php->native binary - static analysis: abstract syntax tree in xml

_rose (rosecompiler.org) - framework for source-to-source transformation and static analysis - strated at a military laboratory - php support based on phc - php->AST in PHP XML->

_Roadsend PHP - PHP->Native binary - No longer maintained

_Roadsend PHP: Raven (RPHP) (available on github) - Based on LLVM, C++ Runtime - Reuses code from phc

_Pipp - php->parrot bytecode - looks dead

_Quercus - Implementation of php 100% in java

_ibm webspheare smash (projectzero.org/php) - php->java bytecode - c-level php extensions can be used via the nava native interface - support for DBGp debugging protocol

_talaria runtime (talariatech.com) - currently in private beta - claims 10 times faster - clains support for real-world applications such as drupal and wordpress - currently no information in what they implement php

_trade-off - use all or some php features - time to implement vs runtime

slides talks.thephp.cc

//reflectionApi practical php reflection | how to use php reflection api | Die Reflection Api