10 tips for good and efficient programmers to write good code

How can I make the code excellent?

Good code can be considered easy to read, understand, debug, and modify, and most importantly, has few defects. Obviously, writing good code takes more time, but in the long run there will be more value, because the cost of maintaining and reusing the code will become lower.

In fact, we can equate good code with reusable code, which is the unifying principle behind many techniques. As a programmer who develops a specific function, the code may achieve your short-term goals, but if no one else wants to reuse it (including yourself), then it must have shortcomings, either it is too complex or too specific, And it is likely to crash under different circumstances, causing other programmers to distrust the code.

I found that trying to apply the following best practices to all the code you write (including your experimental and prototype code) allows you to write better code regardless of your level of experience.

  1. Follow the single responsibility principle

In the programmer's code base, functions are the most important abstract form. The more code you can reuse, the less code you write, and the more reliable they are. Small functional code that follows the single responsibility principle is more likely to be reused.

2. Minimize shared state

You should minimize the implicit shared state between functions. Whether it is a file-scope variable or a member field of an object, it supports explicit values ​​as parameters. When the code clarifies what the function needs to produce the desired result, the code becomes easier to understand and reuse.

In this case, you should prefer static stateless variables instead of member variables on the object.

3. Side effects of localization

Ideal side effects (for example: console printing, logging, changing global state, file system operations, etc.) should be placed in a separate module, rather than scattered throughout the code. Functional side effects often violate the single responsibility principle.

  1. Prefer immutable objects

If the state of an object is set once in its constructor and never changes again, debugging becomes much easier, because once it is constructed correctly, it is still valid. This is one of the easiest ways to reduce the complexity of a software project.

5. Use more interfaces and use less classes

Functions that use interfaces (or template parameters or concepts in C++) are easier to reuse than functions that run on classes.

  1. Apply good principles to modules

Look for opportunities to decompose software projects into smaller modules (for example: libraries and applications) to encourage module-level reuse. Some key principles of the module are:

Dependency minimization

Every project should have a clear function

Don't repeat

You should strive to make your project small and clear.

  1. Avoid inheritance

In object-oriented programming, especially in virtual functions, inheritance is often a dead spot in terms of reusability. I have hardly succeeded in writing or using libraries that can override classes.

  1. Testing during design and development

I am not a big fan of test-driven development, but as I start to write code, the test code will naturally follow many guidelines. It can also help us find many errors earlier. However, to avoid writing useless test code, good coding means higher-level testing (for example: integration testing or unit testing and functional testing), and it is more effective in revealing defects.

  1. Preferred choice over handwritten standard library

I can't tell you how long it will take me to see a better declaration of std::vector or std::string, but this is almost always a waste of time and energy. Except for the obvious fact that you are introducing a bug (see Tip 10), other programmers are unlikely to reuse your code because it is not code that is widely understood, supported, and tested.

  1. Avoid writing new code

This is what every programmer should follow: "The best code is the code that isn't written". The more lines of code you have, the more defects you have, and the more difficult it is to find and fix bugs.

Before writing a line of code, ask yourself, is there a tool, function, or library that has done what you need? Do you really need that function instead of calling another function that already exists?

At last

Programming is an art form or a skill that is very similar to learning. Only through hard practice and continuous learning from others can you write better. Continuously improving code quality will help you become a more efficient programmer.

Guess you like

Origin blog.csdn.net/cxyITgc/article/details/111477401