- elegant and efficient
- does one thing well
- can easily be read, and enhanced by a developer other than its original author
- meaningful names
- minimal dependencies
- has unit and acceptance tests
- clean code always looks like it was written by someone who cares
- nothing obvious that you can do to make it better
- no duplication
- "spending time keeping your code clean is not just cost effective; it’s a matter of professional survival"
- "Each function, each class, each module exposes a single-minded attitude that remains entirely undistracted, and unpolluted, by the surrounding details."
- "remember you are an author, writing for readers who will judge your effort"
- "The proper use of comments is to compensate for our failure to express ourself in code"
- "Writing tests leads to better designs"
- When constructors are overloaded, use static factory methods with names that describe the arguments. For example,
Complex fulcrumPoint = Complex.FromRealNumber(23.0);
is generally better thanComplex fulcrumPoint = new Complex(23.0);
Consider enforcing their use by making the corresponding constructors private. - 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
- Choose descriptive and unambiguous names.
- Make meaningful distinction.
- Use pronounceable names.
- Use searchable names.
- Replace magic numbers with named constants.
- Avoid encodings. Don't append prefixes or type information.
- Small.
- Do one thing.
- Use descriptive names.
- Prefer fewer arguments.
- Have no side effects.
- Don't use flag arguments. Split method into several independent methods that can be called from the client without the flag.
- Always try to explain yourself in code.
- Don't be redundant.
- Don't add obvious noise.
- Don't use closing brace comments.
- Don't comment out code. Just remove.
- Use as explanation of intent.
- Use as clarification of code.
- Use as warning of consequences.
- Separate concepts vertically.
- Related code should appear vertically dense.
- Declare variables close to their usage.
- Dependent functions should be close.
- Similar functions should be close.
- Place functions in the downward direction.
- Keep lines short.
- Use white space to associate related things and disassociate weakly related.
- Don't break indentation.
- Hide internal structure.
- Avoid hybrids structures (half object and half data).
- Should be small.
- Do one thing.
- Small number of instance variables.
- Base class should know nothing about their derivatives.
- Better to have many functions than to pass some code into a function to select a behavior.
- Prefer non-static methods to static methods.
- Objects expose behavior and hide data.
- Data structures expose data and have no significant behavior.
- Write try-catch-finally first when wrtiting new code
- Create informative error messages
- Don’t return null (throws an exception or returns a special case object)
- Don’t pass null
- Wrap third-party API
- Test one thing
- Readable
- Fast
- Independent
- Repeatable in every environement
- Small
- SRP (Single Responsibility Principle)
- Should be open for extension but closed for modification
- 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”
- Small number of instance variables
- Should depend upon abstractions, not on concrete details
- Move all aspects of construction to "main" or equivalent
- Use abstraction for dependencies
- Use Dependency Injection (DI)
- Use the simplest thing that can possibly work.
- Runs all the tests
- Contains no duplication
- Expresses the intent of the programmer
- Minimizes the number of classes and methods
- Rigidity. The software is difficult to change. A small change causes a cascade of subsequent changes.
- Fragility. The software breaks in many places due to a single change.
- Immobility. You cannot reuse parts of the code in other projects because of involved risks and high effort.
- Needless Complexity.
- Needless Repetition.
- Opacity. The code is hard to understand.
- Follow standard conventions.
- Keep it simple stupid. Simpler is always better. Reduce complexity as much as possible.
- Boy scout rule. Leave the campground cleaner than you found it.
- Always find root cause. Always look for the root cause of a problem.
- Keep configurable data at high levels.
- Prefer polymorphism to if/else or switch/case.
- Separate multi-threading code.
- Prevent over-configurability.
- Use dependency injection.
- Follow Law of Demeter. A class should know only its direct dependencies.
- Be consistent. If you do something a certain way, do all similar things in the same way.
- Use explanatory variables.
- Encapsulate boundary conditions. Boundary conditions are hard to keep track of. Put the processing for them in one place.
- Prefer dedicated value objects to primitive type.
- Avoid logical dependency. Don't write methods which works correctly depending on something else in the same class.
- Avoid negative conditionals.