Clean Code: A Handbook of Agile Software Craftsmanship (Robert C. Martin Series)
Rate it:
Open Preview
8%
Flag icon
The name of a variable, function, or class, should answer all the big questions. It should tell you why it exists, what it does, and how it is used. If a name requires a comment, then the name does not reveal its intent.
10%
Flag icon
Classes and objects should have noun or noun phrase names
10%
Flag icon
Methods should have verb or verb phrase names
10%
Flag icon
When constructors are overloaded, use static factory methods with names that describe the arguments.
10%
Flag icon
Pick one word for one abstract concept and stick with it. For instance, it’s confusing to have fetch, retrieve, and get as equivalent methods of different classes.
10%
Flag icon
Let’s say we have many classes where add will create a new value by adding or concatenating two existing values. Now let’s say we are writing a new class that has a method that puts its single parameter into a collection. Should we call this method add? It might seem consistent because we have so many other add methods, but in this case, the semantics are different, so we should use a name like insert or append instead.
10%
Flag icon
you need to place names in context for your reader by enclosing them in well-named classes, functions, or namespaces. When all else fails, then prefixing the name may be necessary as a last resort.
11%
Flag icon
In an imaginary application called “Gas Station Deluxe,” it is a bad idea to prefix every class with GSD.
12%
Flag icon
The first rule of functions is that they should be small. The second rule of functions is that they should be smaller than that.
12%
Flag icon
the indent level of a function should not be greater than one or two.
12%
Flag icon
another way to know that a function is doing more than “one thing” is if you can extract another function from it with a name that is not merely a restatement of its implementation
13%
Flag icon
We want the code to read like a top-down narrative.5 We want every function to be followed by those at the next level of abstraction so that we can read the program, descending one level of abstraction at a time as we read down the list of functions.
14%
Flag icon
The ideal number of arguments for a function is zero (niladic). Next comes one (monadic), followed closely by two (dyadic). Three arguments (triadic) should be avoided where possible. More than three (polyadic) requires very special justification—and then shouldn’t be used anyway.
14%
Flag icon
Output arguments are harder to understand than input arguments. When we read a function, we are used to the idea of information going in to the function through arguments and out through the return value. We don’t usually expect information to be going out through the arguments. So output arguments often cause us to do a double-take.
14%
Flag icon
If a function is going to transform its input argument, the transformation should appear as the return value.
14%
Flag icon
even if the implementation in the first case simply returns the input argument.
14%
Flag icon
Flag arguments are ugly. Passing a boolean into a function is a truly terrible practice. It immediately complicates the signature of the method, loudly proclaiming that this function does more than one thing. It does one thing if the flag is true and another if the flag is false!
14%
Flag icon
When a function seems to need more than two or three arguments, it is likely that some of those arguments ought to be wrapped into a class of their own.
15%
Flag icon
Side effects are lies. Your function promises to do one thing, but it also does other hidden things. Sometimes it will make unexpected changes to the variables of its own class. Sometimes it will make them to the parameters passed into the function or to system globals. In either case they are devious and damaging mistruths that often result in strange temporal couplings and order dependencies.
15%
Flag icon
Temporal couplings are confusing, especially when hidden as a side effect. If you must have a temporal coupling, you should make it clear in the name of the function.
15%
Flag icon
Anything that forces you to check the function signature is equivalent to a double-take. It’s a cognitive break and should be avoided.
15%
Flag icon
In general output arguments should be avoided. If your function must change the state of something, have it change the state of its owning object.
15%
Flag icon
Functions should either do something or answer something, but not both. Either your function should change the state of an object, or it should return some information about that object. Doing both often leads to confusion.
15%
Flag icon
Returning error codes from command functions is a subtle violation of command query separation. It promotes commands being used as expressions in the predicates of if statements.
15%
Flag icon
Try/catch blocks are ugly in their own right. They confuse the structure of the code and mix error processing with normal processing. So it is better to extract the bodies of the try and catch blocks out into functions of their own.
16%
Flag icon
if the keyword try exists in a function, it should be the very first word in the function and that there should be nothing after the catch/finally blocks.
16%
Flag icon
Duplication may be the root of all evil in software.