# Code Smell 191 - Misplaced Responsibility


> TL;DR: Don't be afraid to create or overload the proper objects.

# Problems

- [Bijection](https://maximilianocontieri.com/the-one-and-only-software-design-principle) Fault.

# Solutions

1. Find actual behavior on the real objects using the [MAPPER](https://maximilianocontieri.com/what-is-wrong-with-software).

2. Answer the question: 'Whose responsibility is..?'

# Context

Finding responsible objects is a tough task.

If we talk to anybody outside the software world, they will tell us where we should place every responsibility.

Software engineers, on the contrary, tend to put behavior in strange places like [helpers](https://maximilianocontieri.com/code-smell-22-helpers).

# Sample Code

## Wrong

[Gist Url]: # (https://gist.github.com/mcsee/66489fee9b0707d7514d520c515ad102)
```javascript
function add(a, b) {
  return a + b;
}

// this is natural in many programming languages,
// but unnatural in real life

class GraphicEditor {
  constructor() {
    this.PI = 3.14;
    // We shouldn't define it here
  }

  pi() {
    return this.PI;
  }

  drawCircle(radius) {
    console.log(`Drawing a circle with radius ${radius}
    and circumference ${2 * this.pi() * radius}.`);
  }
}
```

## Right

[Gist Url]: # (https://gist.github.com/mcsee/57f86bc7ab8f2e4c67039733ddacb118)
```javascript
class Integer {

  function add(adder) {
    return this + adder;
  }
}

// This won't compile in many programming languages
// But it is the right place for adding responsibility

class GraphicEditor {
  drawCircle(radius) {
    console.log(`Drawing a circle with radius ${radius} and 
    circumference ${2 * Number.pi() * radius}.`);
  }
}
// PI's definition is Number's responsibility

```

# Detection

[X] Manual

This is a semantic smell.

# Exceptions

- Some languages force you to add protocol in some objects and not on everyone (like primitive integers, Strings, Arrays, etc.)

# Tags

- Behavior

# Conclusion

If you put the responsibilities in the proper object, you will surely find them in the same place.

# Relations

%[https://maximilianocontieri.com/code-smell-22-helpers]

%[https://maximilianocontieri.com/code-smell-63-feature-envy]

# More Info

- [Clean Code Book](https://amzn.to/3k2apxY)

# Disclaimer

Code Smells are just my [opinion](https://maximilianocontieri.com/i-wrote-more-than-90-articles-on-2021-here-is-what-i-learned).

# Credits

Photo by [Austin Neill](https://unsplash.com/@arstyy) on [Unsplash](https://unsplash.com/photos/OWbH9a8Yi2I)
    
* * *

> Object thinking focuses our attention on the problem space rather than the solution space. Object thinkers take the advice of Plato, Parnas, Fred Brooks, Christopher Alexander, and many others by letting the problem define its own solution

_David West_
 
%[https://maximilianocontieri.com/software-engineering-great-quotes]

* * *

This article is part of the CodeSmell Series.

%[https://maximilianocontieri.com/how-to-find-the-stinky-parts-of-your-code]
