tezvyn:

Code Smells: Indicators of Deeper Problems, Not Flaws

AI-drafted, machine-checkedSource: martinfowler.comintermediate

A code smell is a surface-level hint, like a long method, that suggests a deeper design problem. It's an indicator, not a definitive flaw. The footgun is blindly "fixing" every smell; a smell is a reason to investigate, not an automatic command to refactor.

THE MENTAL MODEL: A code smell is a surface indication that usually corresponds to a deeper problem in the system. It's not the problem itself, but a symptom that's quick to spot, or "sniffable." Think of it as a smoke alarm: the alarm isn't the fire, but it warns you to look for one. The term was coined by Kent Beck and popularized by Martin Fowler.

HOW IT WORKS: A developer notices a characteristic in the code that is often associated with poor design, such as a very long function or a class that only holds data. This smell doesn't automatically mean the code is bad, but it serves as a trigger for a deeper look. The developer then investigates to see if an underlying issue, like low cohesion or high coupling, exists. If a problem is confirmed, the smell has successfully guided the developer toward a valuable refactoring opportunity.

WHEN TO USE IT: Use code smells as a guide for continuous improvement and refactoring. They are excellent teaching tools; junior developers can easily learn to spot them, even if they don't yet know how to fix the underlying issue. Teams can adopt a "smell of the week" to focus their learning and collectively improve code quality, making architectural improvement a concrete, shared activity.

WHEN NOT TO USE IT: Do not treat code smells as a strict, dogmatic rulebook. The biggest mistake is refactoring code just because it exhibits a smell, without confirming a real problem exists. Some long methods are fine. A smell is a heuristic, a rule of thumb, not a law. Always investigate the "why" before changing the code. Blindly "fixing" smells can introduce new bugs or complexity for no real benefit.

ONE CANONICAL EXAMPLE: A "Data Class" is a class that has fields, getters, and setters, but no other behavior or methods. This is a smell because object-oriented design suggests that data and the behavior that operates on it should be bundled together. When you see a Data Class, ask: "What behavior belongs in this class?" The refactoring process then involves moving methods from other parts of the code that operate on this class's data into the class itself. This turns an anemic object into a rich, useful one.

Read the original → martinfowler.com

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.