php hooligans

No Code is Good Code


Strong opinions, loosely held

By Ben Peachey and Auke van Slooten

fight!

design patterns

are a code smell

MVC,factory,singleton,observer,

listener,strategy,decorator,active

 record,gateway,proxy,adapter


cute, so you're a hotshot programmer

now what does this

AbstractSingletonProxyFactoryBean

actually do?

keep it simple


don't use design patterns just because you can


don't name your functions after generic design patterns


we're not programming java here

Avoid type hints, embrace duck typing


It's like capabilities checking for data.

everything is a string

remember this and PHP will not surprise you
too often

PHP is designed for web applications

variables passed through GET or POST are strings

so PHP assumes anything is a kind of string

use === and !== to avoid surprises

Duck typing makes code


  • Easy to re-use
  • Easy to change
  • Short

It also makes it easy to trip
if you overcomplicate
so that's a win...

use of objects complicates code


Do not pass (mutable) objects around as arguments.

Objects are shared state


Shared state == complexity

Use basic types where you can

Use value- or immutable objects if not

Arrays in PHP are fast and flexible
They are passed by value
with copy-on-write

Functional Core
Imperative shell

A talk by Gary Bernhardt
from destroyallsoftware.com
not free but worth it

see this talk for a small part
https://www.destroyallsoftware.com/talks/boundaries

or any other talk there

class Inheritance is evil


  • hierarchical taxonomies never work
  • inheritance resists change
  • it increases code bloat
  • it adds to the API surface (protected)


Use Composition instead


Composition


You are already doing it

Dependency Injection is Composition

(Service Locator is not)

The big difference:


Inheritance is 'bound' while writing the code

Composition is 'bound' while running the code

Late binding makes flexible, simple code


Ide's destroy your code

Good programmers and good code
don't need an IDE

Programming


is not about writing code for computers to run

it is about making computer code
understandable to humans

Programming is... 


reading other people's code

understanding other people's problems
and solutions

and then writing code for other humans

IDE's are crutches for your mind


which is not a bad thing

but by relying on them

we never learn to walk



keep it small

The best code is no code

when in doubt, leave it out

don't overthink, build what you need now


All code is buggy


unit test only find bugs in what they test

the number of bugs per line of code is a constant

so by writing half the code you avoid half the bugs


Donald Knuth vs. Doug mcilroy

Knuth: literate programming

Several hundred lines of Pascal

Doug mcilroy


    tr -cs A-Za-z '\n' 
    tr A-Z a-z |
    sort |
    uniq -c |
    sort -rn |
    sed ${1} q
        

http://www.leancrew.com/all-this/2011/12/more-shell-less-egg/ 

SECOND BEST IS STOLEN CODE


re-use proven good code

build for re-use

build for change

"A wise engineering solution would produce—or better, exploit—reusable parts." - Doug McIlroy

URL routing in code sucks


cool url's never change

url's should reflect their content

not the code built to serve it

URL's are important


The web is built on links
but if you change a URL
somewhere a link is broken
and part of the web dies

So design web applications with stable URL's

URL routing breaks links

Symphony:
class BlogController extends Controller
{
    /**
     * @Route("/blog/{slug}", name="blog_show")
     */
    public function showAction($slug)
    {

Fat Free Framework:
$f3->route('GET /brew/@count',
    function($f3) {
        echo $f3->get('PARAMS.count').' bottles of beer on the wall.';
    }
);

URL's are content

Not code


Standard URL routing sees URL's as
part of your API

Something for the developer to define

Not an editor writing content for the site

Code changes break links


Unless your website is an API
Don't treat your URL's as part of it

Simple solution:
add one level of abstraction

Let editors manage the URL routing

Most CMS systems already do this

100% code coverage

is pointless


Testing is good

Testing everything is impossible

100% Code Coverage means nothing

Why unit test?

Because you ...

  • can refactor code with more confidence

  • spot bad code faster

  • implicitly document your API

  • get alerted to unforeseen effects

Writing unit tests

makes coding easier


But if you want to make the code better

less buggy

there are better tools

Code review

Greg Wilson - What we actually know about software development, and why we believe it's true.
(https://vimeo.com/9270320)

Rigorous inspections can remove 60-90% of errors before the first test is run. (Fagan 1975)

The first review and hour matter most. (Cohen 2006)

Write for code reviews


Write small components
that can be understood in one hour

Write clearly

Use simple concepts

and not too many of them

PHP is the only framework

...you need


First analyze your problem

then choose your components

A framework may be useful
but it is not a starting point

Learn PHP instead

Design first

Domain Driven Design used to be called
'Programming'



Use components

Composer
Packagist

There is no reason not to use these anymore


GO MICRO

If you need structure

Embrace the magic


Magic methods turn methods and properties into data
 calls and accesses into messages

The wholy trinity


  1. ducktyping
  2. magic methods
  3. composition

Results in

Less code
More flexibility

The END

Slides at slides.com/poefke/php-hooligans-6/
https://github.com/PHPHOOLIGANS

Auke van Slooten
auke@muze.nl
www.poefke.nl  / www.muze.nl

Ben Peachey
potherca@gmail.com
@potherca
pother.ca

PHP Hooligans (2015)

By Auke van Slooten

PHP Hooligans (2015)

  • 2,729