![cover-img](https://project-assets.showwcase.com/1420x/16381/1675112875022-1675112874210-nick-seagrave-1tpLdmxki-c-unsplash.jpg?type=webp)
Code Smell 47 - Diagrams
30 January, 2023
2
2
0
Contributors
Diagrams are not code. They cannot be a code smell. They are just Diagram Smells.
Problems
- Maintainability
- Trash code
- Code Duplications
- Diagrams focus only on structure (accidental) and not behavior (essential).
Solutions
- Use diagrams only to communicate ideas with other humans.
- Program on your favorite IDE.
- Thrash all diagrams. Even the ones generated from the source code.
- Trust your tests. They are alive and well maintained.
- Use Domain Driven Design technique.
Examples
Sample Code
Wrong
![](https://project-assets.showwcase.com/16381/1675112905340-class-example-library-domain.png)
Right
<?
final class BookItem {
// No. We will not inherit from concrete class Book since this is another smell
// No. We will not list a bunch of anemic attributes. since yet this is another smell
function numberOfPages() {
//
}
function language(): Language {
//
}
function book(): Book {
// Book can tell us a lot about her/his author, title etc.
}
function edition(): BookEdition {
// ..
}
// Loan and overdues are not book items responsibility
}
final class LoanTracker {
function loan(BookItem $bookCopy, LibraryUser $reader, DatePeriod $loanDates) {
// DatePeriod is better than anemic $fromDate and $toDate
}
}
final class LoanTrackerTests extends TestCase {
// Lots of maintained tests telling us how the system really works
}
Detection
We can remove all code annotations and forbid them by policy.
Conclusion
Designing is a contact sport. We need to prototype and learn from our running models.
Papers and JPGs don't run. They live in the utopic world where everything works smoothly.
CASE was a very hot trend back in the 90s.
No good system was developed with these tools.
Relations
Code Smell 25 - Pattern Abusers
More Info
Credits
Photo by Nick Seagrave on Unsplash
The Diagram is Not the Model. The model is not the diagram. It is an abstraction, a set of concepts and relationships between them.
Eric Evans
Software Engineering Great Quotes
This article is part of the CodeSmell Series.