clean-code-php icon indicating copy to clipboard operation
clean-code-php copied to clipboard

Spoiler tag

Open peter-gribanov opened this issue 7 years ago • 2 comments

Maybe we should use spoilers for examples? This is just an idea.


Avoid conditionals

This seems like an impossible task. Upon first hearing this, most people say, "how am I supposed to do anything without an if statement?" The answer is that you can use polymorphism to achieve the same task in many cases. The second question is usually, "well that's great but why would I want to do that?" The answer is a previous clean code concept we learned: a function should only do one thing. When you have classes and functions that have if statements, you are telling your user that your function does more than one thing. Remember, just do one thing.

Bad:
class Airplane
{
    // ...

    public function getCruisingAltitude()
    {
        switch ($this->type) {
            case '777':
                return $this->getMaxAltitude() - $this->getPassengerCount();
            case 'Air Force One':
                return $this->getMaxAltitude();
            case 'Cessna':
                return $this->getMaxAltitude() - $this->getFuelExpenditure();
        }
    }
}
Good:
interface Airplane
{
    // ...

    public function getCruisingAltitude();
}

class Boeing777 implements Airplane
{
    // ...

    public function getCruisingAltitude()
    {
        return $this->getMaxAltitude() - $this->getPassengerCount();
    }
}

class AirForceOne implements Airplane
{
    // ...

    public function getCruisingAltitude()
    {
        return $this->getMaxAltitude();
    }
}

class Cessna implements Airplane
{
    // ...

    public function getCruisingAltitude()
    {
        return $this->getMaxAltitude() - $this->getFuelExpenditure();
    }
}

I offer this only because the document is very large.

There is another solution. Move sections to separate files.

├ README.md
├ variables
│ ├ use-meaningful-and-pronounceable-variable-names.md
│ ├ use-the-same-vocabulary-for-the-same-type-of-variable.md
│ ├ ...
│ └ use-default-arguments-instead-of-short-circuiting-or-conditionals.md
├ ...
├ solid
│ ├ srp.md
│ ├ ocp.md
│ ├ lsp.md
│ ├ isp.md
│ └ dip.md
├ dry.md
└ translations.md

This will make it possible to use a more detailed description for each example. Yet again. It's just an idea.

peter-gribanov avatar Sep 14 '17 13:09 peter-gribanov

I prefer a very large document with all the examples pre-expanded and on the one page.

someonefamous avatar Oct 05 '17 23:10 someonefamous

@someonefamous Please put :-1:. We will vote.

peter-gribanov avatar Oct 06 '17 10:10 peter-gribanov

I believe we already came to the conclusion This is just a reminder to close this issue once it is solved Thank you 🙏 @someonefamous @peter-gribanov

ranggakd avatar Oct 04 '22 08:10 ranggakd