By Christopher Diggins

Translation by Chen Yunlong

With over 30 years of software experience, here are 10 best tips.

So what makes good code?

Good code can be defined as easy to read, easy to understand, easy to debug, easy to change, and most importantly, fewer bugs. Obviously, it takes a lot of time to type good code, but it makes sense in the long run because you spend less time and effort maintaining and reusing your code.

In fact, we can equate good code with reusable code, which is one of the key principles mentioned below. The code may only accomplish a specific function for the short term purpose of the programming effort, but if no one (including yourself) wants to reuse your code, it is deficient and flawed in some way. It may be too complex, too specific, too likely to break under different circumstances, or other programmers may not trust your code.

No matter your level of experience, if you consistently apply the following tips to your code (including your experiments or prototypes), good code is readily available.

1. Follow the principle of single responsibility

Functions are the single most important form of abstraction in a programmer’s library. The more that can be reused, the less code you have to write, and the more reliable that code will be. Small functions that follow the single liability principle are more likely to be reused.

2. Minimize the share status

Implicit shared state between functions should be minimized, whether it is a file-scoped variable or a member field of an object, in favor of explicitly taking the desired values as arguments. Code becomes easy to understand and reuse when functions are explicit to achieve the desired results.

One conclusion is that you should choose static stateless variables over object member variables.

3. Localization side effects

Desirable side effects (such as printing to the console, recording, changing global state, file system operations, and so on) should be placed in separate modules rather than scattered throughout the code. Functional side effects often violate the single liability principle.

4. Select immutable objects first

If an object’s state is set once in its constructor and does not change again, debugging becomes much easier because it remains valid once constructed correctly. This is one of the easiest ways to reduce the complexity of a software project.

5. Use interfaces and classes less

Functions that accept interfaces (or template arguments or concepts in C++) are more reusable than functions that operate on classes.

6. Apply good principles to modules

Decompose software projects into smaller modules (such as libraries and applications) for modular reuse. Some of the key principles of the module are:

  • 1. Minimize dependencies;

  • 2. Each project should have a single, clear function;

  • 3. Don’t repeat yourself.

You should strive to keep your projects small and specific.

7. Avoid inheritance

Inheritance, especially virtual functions, is often a dead end in terms of reusability in object-oriented programming. I’ve rarely been successful with libraries that override classes.

8. Test as well as design and development

I’m not a big fan of test-driven development, but writing tests naturally follows a lot of guidelines when you start writing test code. It also helps to expose mistakes early. Avoid writing useless tests; good coding means that more advanced tests (for example, integration or functional tests in unit tests) are more effective at showing defects.

9. Prioritize handwritten libraries over handwritten ones

I can’t tell you how long it takes to see a better version of STD :: Vector or STD :: String, but it’s almost always a waste of time and effort. Except for the obvious fact that you are introducing the bug to a new place. (See Tip 10.) Other programmers are less likely to reuse your code than code that is widely understood, supported, and tested.

Avoid writing new code

Most importantly, every programmer should follow: “The best code is The code that isn’t written.” The more code you have, the more bugs you have, and the harder it is to find and fix them.

Before writing a line of code, ask yourself, is there a tool, function, or library that does what you need? Do you really need to implement this feature yourself, rather than call another one that already exists?

conclusion

Programming is like an art form or a sport, and you can only improve the quality of your code by practicing and learning from others, which will help you become a more effective programmer.