Commit 7b50cda9 authored by Julien Janvier's avatar Julien Janvier

Tell Don't Ask

parent fe690028
......@@ -38,7 +38,7 @@ todo gif with phpstorm
Here is the list of rules to follow to add a new annotation:
- Each annotation should spot a common OOP design flaw.
- Each annotation should be named by the problem it highlights, not by its solution.
- Each annotation should be named after the problem it highlights or by a general principle whose name already emphasizes the problem. It should not be named after its solution.
- Each annotation must contain a description. The description should remain simple. Its role is just to introduce the problem, not to supplant great blog posts or books.
- Each annotation must come with several good English free references.
- Each annotation can come with good non free references, like a book for instance.
......
<?php
/**
* @\Jjanvier\BrokenOop\TellDontAsk()
*/
class ForbiddenDirectory
{
private $path;
public function __construct(string $path)
{
if (!is_dir($path)) {
throw new \Exception('The path does not exist.');
}
$this->path = $path;
}
public function getPath(): string
{
return $this->path;
}
}
$forbiddenDirectories = [
new ForbiddenDirectory('/foo/bar'),
new ForbiddenDirectory('/foo/baz'),
];
// ...
foreach ($forbiddenDirectories as $forbiddenDirectory) {
// Here the "Tell Don't Ask" principle is broken.
// We retrieve data ("ask") from an object whereas the object could perfectly perform the action by itself ("tell").
rmdir($forbiddenDirectory->getPath());
}
<?php
class ForbiddenDirectory
{
private $path;
public function __construct(string $path)
{
if (!is_dir($path)) {
throw new \Exception('The path does not exist.');
}
$this->path = $path;
}
public function remove(): void
{
rmdir($this->path);
}
}
$forbiddenDirectories = [
new ForbiddenDirectory('/foo/bar'),
new ForbiddenDirectory('/foo/baz'),
];
// ...
foreach ($forbiddenDirectories as $forbiddenDirectory) {
$forbiddenDirectory->remove();
}
<?php
namespace Jjanvier\BrokenOop;
/**
* The "Tell Don't Ask" principle can be summarized as follows: the one which owns the data performs the action on it.
* The client object "tells" what to do, instead of "asking" the data and performing an action with that data.
*
* Please note that the goal of the "Tell Don't Ask" principle is not to eradicate all getters or query functions
* of an object.
*
* References:
* - Tell Don't Ask by Martin Fowler {@see https://martinfowler.com/bliki/TellDontAsk.html}
* - Getter Eradicators by Martin Fowler {@see https://martinfowler.com/bliki/GetterEradicator.html}
* - [French] Le langage objet passé, présent futur par Sophie Beaupuis {@see https://afup.org/talks/2215-le-langage-objet-passe-present-futur}
*
* @Annotation
*
* @author Julien Janvier
* @license GNU GPLv3 https://opensource.org/licenses/GPL-3.0
*/
class TellDontAsk
{
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment