# Code Smell 183 - Obsolete Comments

> TL;DR: Don't trust comments. They are dead.

# Problems

- Bad documentation

- Maintainability

# Solutions

1. Replace comments with tests

# Refactorings

%[https://maximilianocontieri.com/refactoring-005-replace-comment-with-function-name]

# Context

We know comments add almost no value to our code.

We need to restrict comments only to very important decisions.

Since most people change logic and forget to update comments they might become obsolete easily.

# Sample Code

## Wrong

[Gist Url]: # (https://gist.github.com/mcsee/fd5b6b44920df66b6558be35f729f0f7)
```cpp
void Widget::displayPlugin(Unit* unit)
{

  // TODO the Plugin will be modified soon, so I don't implement this right now
  
  if (!isVisible) {
      // hide all widgets
  	return;
  }
}
```

## Right

[Gist Url]: # (https://gist.github.com/mcsee/36f9dde4301f0ab77c034aeba7c0b391)
```cpp
void Widget::displayPlugin(Unit* unit)
{
 
 if (!isVisible) {
	return;
 }

}
```

# Detection

[X] Semi-Automatic 

We can warn for comments in our code and try to remove them.

# Exceptions

- Very important design decisions

# Tags

- Comments

# Conclusion

We need to think before adding a comment. Once It is in the codebase is beyond our control and can start to lie anytime.

# Relations

%[https://maximilianocontieri.com/code-smell-05-comment-abusers]

%[https://maximilianocontieri.com/code-smell-152-logical-comment] 

%[https://maximilianocontieri.com/code-smell-151-commented-code]

# 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 [Volodymyr Hryshchenko](https://unsplash.com/@lunarts) on [Unsplash](https://unsplash.com/s/photos/obsolete)
    
* * *

> Obsolete comments tend to migrate away from the code they once described. They become floating islands of irrelevance and misdirection in the code.
 
_Bob Martin_

%[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]
