Home  >  Article  >  Backend Development  >  10 programming tips you must know

10 programming tips you must know

ringa_lee
ringa_leeOriginal
2017-09-17 10:25:461489browse

Summary: How to type good code? Good code can be defined as easy to read, easy to understand, easy to debug, easy to change, and most importantly, has few defects. Obviously, it takes a lot of time to type good code, but this makes sense in the long run because you can spend less time and energy maintaining and reusing your code. This is a summary from an experienced programmer with 30 years of software experience.

Why do you knock out a good code?

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

In fact, we can equate good code with reusable code, which is also one of the important principles mentioned below. The code may only fulfill a specific function for a short-term goal in the programming work, but if no one (including yourself) is willing to reuse your code, the code can be said to be insufficient and defective in some way. Either it's too complex, or it's too specific, or it's very likely to break under different circumstances, or other programmers may not trust your code.

No matter your experience level, if you consistently apply the following tips to your code (including your experiments or prototypes), you will have good code at your fingertips.

1. Follow the single responsibility principle

Function is the single most important abstract form in the programmer's library. The more opportunities there are for reuse, the less code you have to write, and the more reliable that code is. Small functions that follow the single responsibility principle are more likely to be reused.

2. Minimize shared state

The implicit shared state between functions should be minimized, whether it is a file scope variable or a member field of an object, which is conducive to explicit Pass the desired value as a parameter. When it is clear that a function achieves the desired result, the code becomes easier to understand and reuse.

One conclusion can be drawn from this, you should prefer static stateless variables to member variables of objects.

3. Localized side effects

Ideal side effects (such as printing to the console, logging, changing global state, file system operations, etc.) should be placed in separate modules rather than scattered across throughout the code. Functional side effects often violate the single responsibility principle.

4. Prefer immutable objects

If the state of an object is set once in its constructor and not changed again, debugging becomes much easier because once constructed correctly Just stay valid. This is one of the easiest ways to reduce the complexity of your software project.

5. Use more interfaces and less classes.

Functions that accept interfaces (or template parameters 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) to achieve modular reuse. Some key principles for modules are:

  1. Minimize dependencies

  2. Every project should have a single well-defined functionality

  3. Don’t repeat

You should strive to keep your projects small and clear.

7. Avoid inheritance

In object-oriented programming, inheritance, especially virtual functions, is often a Achilles heel in terms of reusability. I've rarely had success using libraries that override classes.

8. Test as much as you design and develop

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

9. Prefer over hand-written standard library

I can't tell you how long it will take before you see a better version of std::vector or std::string, but it's almost always Waste of time and energy. Except for the obvious fact that you are introducing bugs into a new place. (See Tip 10) Other programmers are less likely to reuse your code than code that is widely understood, supported, and tested.

10. Avoid writing new code

The most important point is that every programmer should follow: "The best code is the code that isn't written" (The best code is code that never needs to be copied). The more code you have, the more defects there will be, and the harder it will be to find and fix bugs.

Before writing a line of code, ask yourself, is there a tool, function or library that already does what you need? Do you really need to implement this function yourself instead of calling another already existing function?
Summary
Programming is like an art form or a sport. Only through continuous practice and continuous learning from others can you continuously improve the quality of your code, which will help you become more efficient. programmer.

The above is the detailed content of 10 programming tips you must know. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn