A Philosophy of Software Design
Rate it:
Open Preview
Read between June 7 - July 14, 2023
53%
Flag icon
an abstraction is a simplified view of an entity, which preserves essential information but omits details that can safely be ignored. If users must read the code of a method in order to use it, then there is no abstraction:
53%
Flag icon
If you want to use abstractions to hide complexity, comments are essential.
53%
Flag icon
software projects are almost always under time pressure, and there will always be things that seem higher priority than writing comments. Thus, if you allow documentation to be de-prioritized, you’ll end up with no documentation.
54%
Flag icon
The overall idea behind comments is to capture information that was in the mind of the designer but couldn’t be represented in the code.
55%
Flag icon
One of the purposes of comments is to make it it unnecessary to read the code:
55%
Flag icon
Well-written comments are not failures. They increase the value of code and serve a fundamental role in defining abstractions and managing system complexity.
55%
Flag icon
The guiding principle for comments is that comments should describe things that aren’t obvious from the code.
55%
Flag icon
Developers should be able to understand the abstraction provided by a module without reading any code other than its externally visible declarations. The only way to do this is by supplementing the declarations with comments.
56%
Flag icon
Conventions serve two purposes. First, they ensure consistency, which makes comments easier to read and understand. Second, they help to ensure that you actually write comments.
56%
Flag icon
Every class should have an interface comment, every class variable should have a comment, and every method should have an interface comment.
57%
Flag icon
could someone who has never seen the code write the comment just by looking at the code next to the comment? If the answer is yes, as in the examples above, then the comment doesn’t make the code any easier to understand. Comments like these are why some people think that comments are worthless.
58%
Flag icon
A first step towards writing good comments is to use different words in the comment from those in the name of the entity being described.
58%
Flag icon
Comments augment the code by providing information at a different level of detail.
59%
Flag icon
When documenting a variable, think nouns, not verbs.
61%
Flag icon
Comments of the form “how we get here” are very useful for helping people to understand code.
61%
Flag icon
The only way to describe an abstraction is with comments. If you want code that presents good abstractions, you must document those abstractions with comments.
61%
Flag icon
The first step in documenting abstractions is to separate interface comments from implementation comments. Interface comments provide information that someone needs to know in order to use a class or method; they define the abstraction. Implementation comments describe how a class or method works internally in order to implement the abstraction. It’s important to separate these two kinds of comments, so that users of an interface are not exposed to implementation details.
66%
Flag icon
The main goal of implementation comments is to help readers understand what the code is doing (not how it does it).
67%
Flag icon
When documenting variables, focus on what the variable represents, not how it is manipulated in the code.
67%
Flag icon
The biggest challenge with cross-module documentation is finding a place to put it where it will naturally be discovered by developers.
69%
Flag icon
If your code is undergoing review and a reviewer tells you that something is not obvious, don’t argue with them; if a reader thinks it’s not obvious, then it’s not obvious. Instead of arguing, try to understand what they found confusing and see if you can clarify that, either with better comments or better code.
70%
Flag icon
Good names are a form of documentation: they make code easier to understand.
70%
Flag icon
you shouldn’t settle for names that are just “reasonably close”. Take a bit of extra time to choose great names, which are precise, unambiguous, and intuitive. The extra attention will pay for itself quickly, and over time you’ll learn to choose good names quickly.
71%
Flag icon
Names are a form of abstraction: they provide a simplified way of thinking about a more complex underlying entity. Like other forms of abstraction, the best names are those that focus attention on what is most important about the underlying entity while omitting details that are less important.
72%
Flag icon
If you find it difficult to come up with a name for a particular variable that is precise, intuitive, and not too long, this is a red flag. It suggests that the variable may not have a clear definition or purpose.
75%
Flag icon
Writing the comments first makes documentation part of the design process.
76%
Flag icon
Comments serve as a canary in the coal mine of complexity. If a method or variable requires a long comment, it is a red flag that you don’t have a good abstraction.
78%
Flag icon
If you want to maintain a clean design for a system, you must take a strategic approach when modifying existing code. Ideally, when you have finished with each change, the system will have the structure it would have had if you had designed it from the start with that change in mind.
78%
Flag icon
Whenever you modify any code, try to find a way to improve the system design at least a little bit in the process. If you’re not making the design better, you are probably making it worse.
80%
Flag icon
If information is already documented someplace outside your program, don’t repeat the documentation inside the program; just reference the external documentation.
80%
Flag icon
Consistency is a powerful tool for reducing the complexity of a system and making its behavior more obvious. If a system is consistent, it means that similar things are done in similar ways, and dissimilar things are done in different ways. Consistency creates cognitive leverage: once you have learned how something is done in one place, you can use that knowledge to immediately understand other places that use the same approach.
81%
Flag icon
Code reviews provide another opportunity for enforcing conventions and for educating new developers about the conventions. The more nit-picky that code reviewers are, the more quickly everyone on the team will learn the conventions, and the cleaner the code will be.
82%
Flag icon
reconsidering established conventions is rarely a good use of developer time.
82%
Flag icon
If code is obvious, it means that someone can read the code quickly, without much thought, and their first guesses about the behavior or meaning of the code will be correct. If code is obvious, a reader doesn’t need to spend much time or effort to gather all the information they need to work with the code. If code is not obvious, then a reader must expend a lot of time and energy to understand it.
82%
Flag icon
“Obvious” is in the mind of the reader: it’s easier to notice that someone else’s code is nonobvious than to see problems with your own code.
84%
Flag icon
Sometimes it isn’t possible to avoid code that is nonobvious. When this happens, it’s important to use comments to compensate by providing the missing information.
85%
Flag icon
software should be designed for ease of reading, not ease of writing.
88%
Flag icon
Developing incrementally is generally a good idea, but the increments of development should be abstractions, not features.
88%
Flag icon
Although I am a strong advocate of unit testing, I am not a fan of test-driven development. The problem with test-driven development is that it focuses attention on getting specific features working, rather than finding the best design.
89%
Flag icon
The greatest risk with design patterns is over-application. Not every problem can be solved cleanly with an existing design pattern; don’t try to force a problem into a design pattern when a custom approach will be cleaner. Using design patterns doesn’t automatically improve a software system; it only does so if the design patterns fit. As with many ideas in software design, the notion that design patterns are good doesn’t necessarily mean that more design patterns are better.
89%
Flag icon
Whenever you encounter a proposal for a new software development paradigm, challenge it from the standpoint of complexity: does the proposal really help to minimize complexity in large software systems? Many proposals sound good on the surface, but if you look more deeply you will see that some of them make complexity worse, not better.
1 3 Next »