Slide 1

Slide 1 text

Inspire yourself by Symfony2 to create better code Marc Weistroff – SensioLabs

Slide 2

Slide 2 text

Have you seen this guy? Marc Weistroff @futurecat

Slide 3

Slide 3 text

h"p://www.flickr.com/photos/joeshlabotnik/4857173373/  

Slide 4

Slide 4 text

How many of you already used Symfony2?

Slide 5

Slide 5 text

How many of you looked at the code?

Slide 6

Slide 6 text

How many of you contributed?

Slide 7

Slide 7 text

WHY SYMFONY CAN BE CONSIDERED GOOD CODE?

Slide 8

Slide 8 text

Because fabpot wrote it !

Slide 9

Slide 9 text

and also more than 300 other developers.

Slide 10

Slide 10 text

Built on powerful concepts •  Separation of concerns •  Pragmatism •  Best practices

Slide 11

Slide 11 text

SEPARATION OF CONCERNS

Slide 12

Slide 12 text

“In computer science, separation of concerns (SoC) is the process of separating a computer program into distinct features that overlap in functionality as little as possible.” -- Wikipedia

Slide 13

Slide 13 text

“A concern is any piece of interest or focus in a program. Typically, concerns are synonymous with features or behaviors.” -- Wikipedia

Slide 14

Slide 14 text

HUH?!

Slide 15

Slide 15 text

Examples of SoC HTML / CSS –  Separating content from styling Internet Protocol Stack – HTTP or SMTP doesn’t care about TCP, nor IP Place where you eat / place where you shit – Don’t mix it up, it’s dirty and can be hazardous

Slide 16

Slide 16 text

Controller View Model The Controller analyses the user request, calls the Model and passes data to the View. The View layer formats data in a dedicated format (html, json…) The Model stores the business logic and classes that manipulate data. Request MVC Pattern

Slide 17

Slide 17 text

Uncoupled components

Slide 18

Slide 18 text

With optional dependencies

Slide 19

Slide 19 text

Bridges •  Bridges couple third party libraries with Symfony components. •  External to the component and the 3rd party library •  Prevent hard-coupling between component and third-party library.

Slide 20

Slide 20 text

HttpFoundation •  Implements the HTTP RFC •  Dedicated objects for – Request – Response / RedirectResponse – Session – SessionStorage – HeaderBag – ResponseHeaderBag

Slide 21

Slide 21 text

Lessons learned

Slide 22

Slide 22 text

Couple your code as less as possible

Slide 23

Slide 23 text

Dedicated object/library for dedicated work/concern

Slide 24

Slide 24 text

THiNK!

Slide 25

Slide 25 text

Is it the job of this class to do that?

Slide 26

Slide 26 text

Does it make sense ?

Slide 27

Slide 27 text

PRAGMATISM

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

Complete list of tips available online http://pragprog.com/the-pragmatic-programmer/extracts/tips

Slide 30

Slide 30 text

Pragmatic approach •  Simple & effective •  Download the project and start immediatly •  Oriented toward the real world, not purity for the sake of purity.

Slide 31

Slide 31 text

Pragmatic approach •  No magic = No WTF effect •  No magic = Keeping control •  Almost always explicit

Slide 32

Slide 32 text

Pragmatic approach •  Code generation thanks to the CLI commands

Slide 33

Slide 33 text

Pragmatic approach •  Easy debugging thanks to WDT •  Meaningful error messages •  Gentle to the end-user

Slide 34

Slide 34 text

Pragmatic approach for your project •  Make a prototype that works, and improve it step by step. •  Refactor early, refactor often. •  THINK constantly about what you’re doing and why you’re doing it

Slide 35

Slide 35 text

Premature optimization abstraction is the root of all evil

Slide 36

Slide 36 text

Coding standards •  Symfony come with it’s own coding standards •  Coming from another framework ? – It can be weird at first time, but you’ll get used to it really quickly

Slide 37

Slide 37 text

Stick to Symfony’s coding standards

Slide 38

Slide 38 text

Design by contract 4 137 0   20   40   60   80   100   120   140   160   symfony  1.4   Symfony2   Interfaces  

Slide 39

Slide 39 text

Contract •  Rights & Responsibilities of an object •  Interfaces are the contracts of OOP •  No surprises •  You can replace an interfaced object with your own implementation, without breaking anything

Slide 40

Slide 40 text

Make interfaces or forever hold your peace

Slide 41

Slide 41 text

Tests •  Symfony2 is heavily tested •  PHPUnit •  ~ 5000 tests, 13000 assertions •  Code coverage – Components : 84% – Bundles: 58% – Bridges: 56%

Slide 42

Slide 42 text

Avoid sleepless nights at work: write tests

Slide 43

Slide 43 text

The wheel, not reinvented •  Rock solid HTTP implementation •  Edge Side Include (ESI) •  HTTP Cache, no applicative cache – Did not reinvent the wheel !

Slide 44

Slide 44 text

If a standardized solution exists: use it

Slide 45

Slide 45 text

BEST PRACTICES

Slide 46

Slide 46 text

Follows PSR-0 •  PHP Standards Working Group •  http://groups.google.com/group/php- standards?pli=1 – autoloader interoperability – Namespace convention

Slide 47

Slide 47 text

No singleton and DIC •  Uses DIC •  No singleton = better testability thanks to the absence of hard-coupling •  Lazy loading •  Reentrancy

Slide 48

Slide 48 text

Interfaced exceptions •  Not present in all components – DependencyInjection, Routing, Serializer, Yaml •  Keeps semantic of the 13 SPL’s exceptions •  Makes it easier to catch exception of a component/library

Slide 49

Slide 49 text

// src/Symfony/Component/Yaml/Exception/ExceptionInterface.php! namespace Symfony\Component\Yaml\Exception;! ! interface ExceptionInterface! {! }! ! ! ! // src/Symfony/Component/Yaml/Exception/DumpException.php! namespace Symfony\Component\Yaml\Exception;! ! class DumpException extends \RuntimeException implements ExceptionInterface! {! }

Slide 50

Slide 50 text

OpenSource cross polination •  Benefit from the work of hundreds of people •  Security Component comes from Spring (Java) •  Monolog was inspired by logbook (python)

Slide 51

Slide 51 text

OpenSource cross polination •  Assetic is based on webassets (python) •  Composer is a port of the SAT solver algorithm of openSUSE’s libzypp (c) •  PHPCR, port of JCR

Slide 52

Slide 52 text

Search for solutions in other languages and port them!

Slide 53

Slide 53 text

and… IT WORKS ! •  Already used in hundreds of websites worldwide: – symfony.com – lockerz.com – exercise.com – Opensky.com

Slide 54

Slide 54 text

REAL WORLD

Slide 55

Slide 55 text

New project comes in •  User account platform •  SSO or webapp can share informations with other accepted webapps •  API for third party websites •  SDK for the API •  Other connected webapps can be done in any php frameworks or languages.

Slide 56

Slide 56 text

Think standards! •  OpenID •  OAuth2 •  CAS

Slide 57

Slide 57 text

Identify the best solution for your needs

Slide 58

Slide 58 text

OAuth2 •  RFC is ~40 pages long. •  OAuth2 is a complicated subject with a lot of HTTP workflows •  Do you really want to start from scratch?

Slide 59

Slide 59 text

Don’t suffer the Not Invented Here syndrom

Slide 60

Slide 60 text

How to choose? •  Search for other libraries in ruby / php / python / whatever •  Is the developper well known? •  Is the company behind renowned? •  Is the library well tested? •  Follow your guts!

Slide 61

Slide 61 text

Porting •  Porting OAuth2 library from ruby was complicated. – RAKE middleware to Symfony2 architecture – I had no knowledge of ruby

Slide 62

Slide 62 text

No content

Slide 63

Slide 63 text

Porting •  Port the test early! •  markTestIncomplete()

Slide 64

Slide 64 text

How to port? •  Port only what you need – Port only the things you need – Make it works – Repeat. •  Port the whole thing in one time

Slide 65

Slide 65 text

But be pragmatic •  Do not try to immediately release the port as open source •  Make it work for your project, and maybe one day, open source it when you’ll be happy of it

Slide 66

Slide 66 text

OAuth2   Provider   Consumer   (Symfony)   Consumer   (Silex)   Consumer   (ZF)   OAuth2 consumers

Slide 67

Slide 67 text

Think independent •  Do you want to tie your SDK to a Framework? •  SDK is framework-independent library •  Only dependency is Buzz (because it’s great) •  Only after that, integrate it into framework – Silex service provider – Symfony2 bundle – symfony 1.x plugin

Slide 68

Slide 68 text

QUESTIONS? rate this talk http://joind.in/3700

Slide 69

Slide 69 text

THANKS ! Marc Weistroff Sensio Labs marc.weistroff@gmail.com @futurecat http://www.marcw.net