Show Your objects are a bunch of public attributes without behavior.Photo by Stacey Vandergriff on UnsplashProtocol is empty (with setters/getters). If we ask a domain expert to describe an entity he/she would hardly tell it is ‘a bunch of attributes’. Problems
Solutions1) Find Responsibilities. 2) Protect your attributes. 3) Hide implementations. 4) Delegate Examples
Sample CodeWrongRightDetectionDetection can be automated with sophisticated linters ignoring setters and getters and counting real behavior methods. Also Known as
Tags
More info
I'm often asked why the book Refactoring isn't included in my recommended developer reading list. Although I own the book, and I've read it twice, I felt it was too prescriptive – if you see (x), then you
must do (y). Any programmer worth his or her salt should already be refactoring aggressively. It's so essential to the craft that if you have to read a book to understand how it works, you probably shouldn't be a programmer in the first place. There's nothing wrong with codifying refactoring guidelines in a book. But the most important guideline is to watch for warning signs in your own code – so called "code
smells". Developing your "code nose" is something that happens early in your programming career, if it's going to happen at all. I combined all the documented code smells I could find into this reference; most of these smells should be familiar to you. Code Smells Within Classes
Code Smells Between Classes
This list was derived from the Smells to Refactorings PDF, and the Smells to Refactorings Wiki, which also provide additional guidance on the specific refactorings that might be helpful in each instance. The important thing, from my perspective, isn't the refactoring – it's learning to recognize the scent of your own code. And if you want examples of the stinkiest code imaginable, How to Write Unmaintainable Code is a good place to start. |