Code Smell 123 - Mixed 'What' and 'How'
Published: 2022-03-22 03:03:37

*We love looking at the internal gears of the clock, but we need to start focusing on the hands.*

> TL;DR: Don't mess with implementation details. Be declarative. Not imperative.

# Problems

- Accidental Coupling

- [Coupling](https://dev.to/mcsee/coupling-the-one-and-only-software-design-problem-2pd7)

- Lack of design for change

- [Comments](https://twitter.com/TonyFlury/status/1103649549355233280) distinguish the 'how' and the 'what'.

# Solutions

1. Separate 'What' and 'How' concerns. 

# Context

Separating concerns is very difficult in the software industry.

Functional software survives ages.

Implementative software brings coupling and is harder to change.

Choosing wise declarative names is a daily challenge.

# Sample Code

## Wrong

[Gist Url]: # (https://gist.github.com/mcsee/d6d3df14e1268f9308fdd2121ccd598d)
```javascript
class Workflow {
    moveToNextTransition() {
        // We couple the business rule with the accidental implementation
        if (this.stepWork.hasPendingTasks) {
            throw new Exception('Preconditions are not met yet..');
        } else {
            this.moveToNextStep();
        }
    }
}
```

## Right

[Gist Url]: # (https://gist.github.com/mcsee/4fb3c004b00d81cfb1749bd22394772c)
```javascript
class Workflow {
    moveToNextTransition() {
        if (!this.canWeMoveOn()) {
            throw new Exception('Preconditions are not met yet..');
        } else {
            this.moveToNextStep();
        }
    }

    canWeMoveOn() {
        // We hide accidental implementation 'the how'
        // under the 'what'
        return !this.stepWork.hasPendingTasks();
    }
}
```

# Detection

[X] Manual

This is a semantic and naming smell.

# Tags

- Readability

# Conclusion

We need to choose good names and add indirection layers when necessary.

Of course, *[premature optimizators](https://dev.to/mcsee/i-wrote-more-than-90-articles-on-2021-here-is-what-i-learned-1n3a)* will fight us, telling us we are wasting computational resources and they need to know the insights we are hiding from them.

# Relations

{% post https://dev.to/mcsee/code-smell-92-isolated-subclasses-names-3i84 %}

{% post https://dev.to/mcsee/code-smell-05-comment-abusers-2ba5 %}

# More Info

- [On premature optimizators](https://dev.to/mcsee/i-wrote-more-than-90-articles-on-2021-here-is-what-i-learned-1n3a)

- [Coupling](https://dev.to/mcsee/coupling-the-one-and-only-software-design-problem-2pd7)

- [What is in a name?](https://dev.to/mcsee/what-exactly-is-a-name-part-ii-rehab-20gd)

# Credits

Photo by [Josh Redd](https://unsplash.com/@joshredd) on [Unsplash](https://unsplash.com/s/photos/clock)
  
The idea of this smell is here:

[Code Smell 118 - Return False's comment](https://dev.to/otumianempire/comment/1mge9)

and here

{% twitter 1103649549355233280 %}

* * *

> We are constantly interfacing with other people's code that might not live up to our high standards and dealing with inputs that may or may not be valid. So we are taught to code defensively. We use assertions to detect bad data and check for consistency.

_Andrew Hunt_
 
{% post https://dev.to/mcsee/software-engineering-great-quotes-26ci %}

* * *

This article is part of the CodeSmell Series.

{% post https://dev.to/mcsee/how-to-find-the-stinky-parts-of-your-code-1dbc %}