Code is clean if everyone on the team can easily understand it. A worker who is not the code’s author can read and improve clean code. It is easier to learn, change, add to, and maintain something that is easy to understand.
This Articles Contents
Rules for everyone for clean code
- Do what everyone else does.
- Don’t make things too hard. Always keep things simple. As much as possible, make things simpler.
- Follow the boy scout rule. Clean up the site after you use it.
- Always look for the cause. Always try to figure out why something is going wrong.
Rules for design
- Keep info that can be changed at a high level.
- Polymorphism is better than if/else or switch/case.
- Keep multithreading code separate.
- Don’t make it too easy to configure.
- Put in place dependency injection.
- Stick to the Law of Demeter. It is best for a class to only know about its direct relations.
Tips for Understanding
- Always be the same. Just because you do one thing one way, do everything else the same way.
- Use factors that explain things.
- Wrap up the border conditions. Conditions at the boundaries are hard to keep track of. Do all of the work for them in one place.
- Dedicated value objects are better than basic types.
- Do not count on logic. Don’t write methods that only work if something else in the same class is also true.
- Don’t use negative conditionals.
Rules for names
- Pick names that are clear and describe what they are.
- Make a difference that matters.
- Use names that are easy to say.
- Use names that can be searched.
- Use named values instead of magic numbers.
- Don’t use encodings. Don’t type information or add prefixes.
Rules for functions
- Small.
- Do one thing.
- Names should be clear.
- Would like fewer fights.
- Do not cause any harm.
- Don’t come up with flag reasoning. You can split a method into several separate ones that can be called from the client without the flag.
Rules for comments
- Always use code to describe yourself.
- Do not repeat yourself.
- Don’t add noise that we can see.
- Do not use comments with ending braces.
- Do not leave comments in code. Just take it off.
- Use to explain your purpose.
- Use to make the code clear.
- Use as a warning of what will happen.
Structure of source code
- Put ideas in different columns. Linked code should look like it’s packed together vertically.
- Declare variables close to where they will be used.
- Functions that depend on each other should be close to each other.
- Functions that do the same thing should be close to each other.
- Place the parts so that they go down.
- Make your words short.
- Don’t arrange things horizontally.
- Use empty space to connect things that are related and separate things that are only lightly related.
- Do not break the line breaks.
Data patterns and objects
- Cover up the structure inside.
- Pick data structures over lists.
- Stay away from structures that are half object and half data.
- It needs to be small.
- Do one thing.
- There are not many instance variables.
- The base class shouldn’t know anything about the class that comes from it.
- It’s better to have a lot of functions than to feed code into one to choose an action.
- Use non-static ways instead of static ones.
Tests
- One claim per test.
- Easy to read.
Speedy. - On their own.
- Doable again.
Code smells bad
- Being stiff. It’s hard to change the program. A small change sets off a chain of changes that happen all at once.
- Being weak. One change makes the software not work right in many places.
- Being stuck. Because there are risks and a lot of work involved, you can’t use parts of the code in other projects.
- Too much complexity.
- Repeating yourself too much.
- Lack of visibility. It’s hard to understand the code.