Object-Oriented Design Heuristics (2017) [slides]
I try to follow a few rules when writing OO code.<p>1. Make classes do one thing that is basically summarized in the class name. (This also takes care of &#x27;separation of concerns&#x27;)<p>2. Be careful to not over-abstract. That results in many tiny classes, aka ravioli code. Nobody, not even you, will remember what all those little bastards do.
3. If a class is bigger than a page (or two) of code, it&#x27;s probably too long.<p>4. Design patterns are for inspiration, not the law. Solve problems, don&#x27;t prove how smart you are.<p>5. Make your classes as simple as possible. You will thank yourself later.<p>6. If you have to write a comment that explains how some confusing thing works; that code block is a good refactoring candidate.<p>7. Refactor, refactor, refactor. Refactoring is rewriting code for clarity. The more you do this, the more your code will become clear to the reader. I find refactoring often reduces my LOC drastically.<p>8. If you just learned a technique&#x2F;pattern and that technique&#x2F;pattern is suddenly popping up everywhere in your code; make sure you aren&#x27;t just using it because it&#x27;s fun. Say you just learned what a singleton pattern is, now your code is littered with singletons. That&#x27;s a good sign that you are forcing yourself to use your new found toy and reconsider.<p>9. Long method names. Don&#x27;t be afraid to have long method names. Yes, it looks ugly, yes it takes time but you will thank yourself later and often a long method name will compensate for the lack of documentation on that method.<p>10. Your program should basically be a bag of loosely related, self-contained components. Think of them like parts of an engine, they each do one thing very well and they each have an interface to the rest of the engine. Also, they can individually be swapped out for another component (as long as they bolt up to the engine appropriately).<p>Understanding OOP is definitely a paradigm shift in thinking and it is also loaded with many a foot gun (especially when you are new).