Clean Code Made Simple - Part 2
Tags: programming, php, cleancode, softwaredesign
Published: 2022-08-11 16:49:00

Further to my last post [Clean Code Made Simple - Part 1](https://dev.to/mehradsadeghi/clean-code-made-simple-part-1-2f32), in this post I'm going to continue introducing techniques from [Robert C. Martin](https://en.wikipedia.org/wiki/Robert_C._Martin)’s [Clean Code book](https://www.google.com/aclk?sa=L&ai=DChcSEwii56mA5bL5AhWXt3cKHW49DWYYABABGgJlZg&sig=AOD64_0RHTphNDrbz7XN-NjV31HGTV4IFA&ctype=5&q=&ved=0ahUKEwjo8qOA5bL5AhUkuqQKHaN7DF8Qww8Iuwk&adurl=).

**#6 Hiding Data**

Which one of below code snippets do you think is better designed?

1)

```php
interface Vehicle {
    public function getFuelTankCapacityInGallons();
    public function getGallonsOfGasoline();
}
```

2)

```php
interface Vehicle {
    public function getPercentFuelRemaining();
}
```

In both of the above cases the second option is preferable. We do not want to expose the details of our data as much as possible. Rather we want to express our data in abstract terms. Serious thought needs to be put into the best way to represent the data that an object contains. The worst option is to blithely add getters and setters.

So in any situation where it's suitable to hide the data of an object, You may do it.

**#7 Simple Design Rules**

Kent Beck introduced these 4 rules as basic criteria to design programs better:

- Runs all the tests
- Contains no duplication
- Expresses the intent of the programmer
- Minimizes the number of classes and methods

In an effort to make our classes and methods small, we might create too many tiny classes and methods. we should also keep our function and class counts low. this is the forth rule of simple design rules by kent which is more important than other ones.

**#8 Intention Revealing Functions**

how do you think this code snippet can get better ?

```php
public function getFlaggedCells() {

    $flaggedCells = new List();

    foreach ($cells as $cell) {
        if ($cell[STATUS_VALUE] == 1) {
            $flaggedCells->add($cell);
        }
    }

    return $flaggedCells;
}
```

We can write an intention-revealing function (call it isFlagged) to hide the magic numbers. It results in a new version of the function:

```php
public function getFlaggedCells() {

    $flaggedCells = new List();

    foreach ($cells as $cell) {
        if ($cell->isFlagged())
            $flaggedCells->add($cell);

    return $flaggedCells;
}
```

**#9 Writing Tests Leads to Better Designs**

> Systems that aren’t testable aren’t verifiable. Arguably, a system that cannot be verified should never be deployed. Fortunately, making our systems testable pushes us toward a design where our classes are small and single purpose. Writing tests leads to better designs.

**#10 Separation of Concerns**

What would you recommend the direction of arrows should be, to separate construction from use ?

```
Main             Application



Builder
```

Directions should be as follow:

```
            run
  Main  --------->  Application
   |
   | build and construct
  \|/
Builder
```

It simply means that the construction of our application should be separate from using it. These two steps are fundamentally separate and decoupling them gives us a better design and structure.

Okay. This is it for this part. You can join my [Telegram](https://t.me/themehradsadeghi) channel to get notified of the latest posts. Also, you can follow me on [Twitter](https://twitter.com/realmehrad) and [LinkedIn](https://linkedin.com/in/mehradsadeghi).