lundi 18 août 2014

acronym of the day: YAGNI

This is an important principle, as important as it is easy to understand. It stands for "You aren't gonna need it".

Quoting Wikipedia (YAGNI at wikipedia):
You aren't gonna need it" is a principle of extreme programming (XP) that states a programmer should not add functionality until deemed necessary.
I link this principle to The MicroPHP Manifesto: where it states "I want less code, not more" and "I need to justify every piece of code I add to a project".

Now, if we look at the current trend of the web frameworks, & in peculiar PHP framework, every programmer should always ask to himself:

  • Do I really need DI (dependency injection?)
  • Do I really need ORM?
  • Do I really need Unitary tests (compared to functional tests) for my web app?
I cand easily answer to most of them: No.

About dependency injection

First: what for?

Validation: Without dependency injection, writing tests would be a nightmare or impossible.
You can then create a Mock that has a predefined behaviour and explicitly test the logic of a software component (a mock is a fake avatar of other component that is user by the tested component).

What is it?
Genrally speaking, dependency injection end up in accepting in the object constructor every external dependency instead of creating them itself in this constructor.


Dependency injection always comes with an "IOC container" (Inversion of control),  and an IoC container provides two things:
  • dynamic binding
  • a dynamic language (usually an incredibly crappy one, built on top of XML or in newer versions on top of annotations)
Then you need a "Service Locator", a sort of object instances directory.
Those usage & patterns direclty come from Java & the J2EE platform.

My  statement of the day

With a scripting language, so PHP here, it is totally USELESS & goes directly into the YAGNI principle.

Dynamic binding is already part of a dynamic language and the dynamic language is already a dynamic language. Therefore, an IoC container simply doesn't make sense: the language is already an IoC container if you prefer.

Secondly, the whole thing is not really helpfull in the PHP context, since PHP destroys everything at every page load.
One of the main features of IOC containers is that you can automatically "wire" your modules together "at runtime". In dynamic languages you can do this fairly easily without any fancy reflection-based logic. 
Those principle existed for platfoms that keeps objects persistant in memory (or disk) across the time. First they failed (to scale), and second this is exactly what we don't want for PHP. 
Again it is a Java thing that has been wrongly ported to PHP.



0 commentaires:

Enregistrer un commentaire