Clean Code: A Handbook of Agile Software Craftsmanship (Robert C. Martin Series)
Rate it:
Open Preview
35%
Flag icon
Unit tests should be written just before the production code that makes them pass.
35%
Flag icon
With functions we measured size by counting physical lines. With classes we use a different measure. We count responsibilities.1
36%
Flag icon
The name of a class should describe what responsibilities it fulfills.
36%
Flag icon
The more ambiguous the class name, the more likely it has too many responsibilities.
36%
Flag icon
We should also be able to write a brief description of the class in about 25 words, without using the words “if,” “and,” “or,” or “but.”
36%
Flag icon
The Single Responsibility Principle (SRP)2 states that a class or module should have one, and only one, reason to change.
36%
Flag icon
Trying to identify responsibilities (reasons to change) often helps us recognize and create better abstractions in our code.
36%
Flag icon
However, a system with many small classes has no more moving parts than a system with a few large classes.
36%
Flag icon
We want our systems to be composed of many small classes, not a few large ones.
36%
Flag icon
Classes should have a small number of instance variables.
36%
Flag icon
A class in which each variable is used by each method is maximally cohesive.
36%
Flag icon
When cohesion is high, it means that the methods and variables of the class are co-dependent and hang together as a logical whole.
36%
Flag icon
You should try to separate the variables and methods into two or more classes such that the new classes are more cohesive.
37%
Flag icon
If there are a few functions that want to share certain variables, doesn’t that make them a class in their own right? Of course it does. When classes lose cohesion, split them!
37%
Flag icon
So breaking a large function into many smaller functions often gives us the opportunity to split several smaller classes out as well.
38%
Flag icon
Every change subjects us to the risk that the remainder of the system no longer works as intended.
38%
Flag icon
Private method behavior that applies only to a small subset of a class can be a useful heuristic for spotting potential areas for improvement.
39%
Flag icon
In an ideal system, we incorporate new features by extending the system, not by making modifications to existing code.
40%
Flag icon
Inversion of Control moves secondary responsibilities from an object to other objects that are dedicated to the purpose, thereby supporting the Single Responsibility Principle.
43%
Flag icon
A premature decision is a decision made with suboptimal knowledge.
43%
Flag icon
Writing tests leads to better designs.
44%
Flag icon
Understanding how to achieve reuse in the small is essential to achieving reuse in the large.
44%
Flag icon
It’s easy to write code that we understand, because at the time we write it we’re deep in an understanding of the problem we’re trying to solve.
45%
Flag icon
A good way to avoid shared data is to avoid sharing the data in the first place.
46%
Flag icon
Avoid using more than one method on a shared object.
46%
Flag icon
The synchronized keyword introduces a lock.
46%
Flag icon
All sections of code guarded by the same lock are guaranteed to have only one thread executing through them at any given time.
46%
Flag icon
A critical section is any section of code that must be protected from simultaneous use for the program to be correct.
46%
Flag icon
Keep your synchronized sections as small as possible.
46%
Flag icon
Testing does not guarantee correctness.
46%
Flag icon
Treat spurious failures as candidate threading issues.
46%
Flag icon
Bugs in threaded code might exhibit their symptoms once in a thousand, or a million, executions.
47%
Flag icon
To encourage task swapping, run with more threads than processors or cores.
47%
Flag icon
Multithreaded code behaves differently in different environments.
47%
Flag icon
The reason that threading bugs can be infrequent, sporadic, and hard to repeat, is that only a very few pathways out of the many thousands of possible pathways through a vulnerable section actually fail.
47%
Flag icon
Learn your library and know the fundamental algorithms.
49%
Flag icon
To write clean code, you must first write dirty code and then clean it.
52%
Flag icon
One of the best ways to ruin a program is to make massive changes to its structure in the name of improvement.
67%
Flag icon
What I am about to do is nothing more and nothing less than a professional review. It is something that we should all be comfortable doing. And it is something we should welcome when it is done for us.
69%
Flag icon
It’s generally a bad idea for base classes to know about their derivatives.
69%
Flag icon
Eliminating final flies in the face of some conventional wisdom.
72%
Flag icon
Comments should be reserved for technical notes about the code and design.
72%
Flag icon
A comment is redundant if it describes something that adequately describes itself.
72%
Flag icon
Comments should say things that the code cannot say for itself.
72%
Flag icon
Commented-out code is an abomination.
73%
Flag icon
No argument is best, followed by one, two, and three.
73%
Flag icon
Output arguments are counterintuitive.
73%
Flag icon
If your function must change the state of something, have it change the state of the object it is called on.
73%
Flag icon
Boolean arguments loudly declare that the function does more than one thing.
73%
Flag icon
There is no replacement for due diligence.