Welcome to Follow my GitHub and Follow my CSDN. For the rest, please refer to the Android directory and our wechat official account: Yang Shoule

So, how do you make your code better? Good code can be identified as code that is easy to read, understand, debug, and modify, and most importantly, has few bugs. Obviously, good code takes more time to write, but it’s also more valuable in the long run because it’s cheaper to maintain and easier to reuse.

In fact, we can equate good code with reusable code, which is the driving principle behind many of the techniques listed here. The code may achieve your short-term goals as a programmer for a particular function, but if no one else wants to reuse it (including your future self), then it must be missing some way. Either it’s too complex, too specific, too likely to break down in different environments, or not trusted by other programmers.

I’ve found that constantly trying to apply the best practices below to all the code you write, including your experiments and prototypes, leads to better code, regardless of your level of experience.

Functions are the programmer’s most important form of abstraction. The more code they can reuse, the less code they write, and the more reliable they are. Smaller functions that follow the single responsibility principle are more likely to be used over and over again.

2. Reduce shared state You should implicitly minimize shared state between functions, whether it is a file-scoped variable or a member field of an object, in favor of specifying the required 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 to member variables on the object.

Desirable side effects (e.g., printing to the console, logging, 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.

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

Interfaces that accept functions (or use template arguments or concepts in C++) are more usable than functions that run on classes.

Apply good principles to modules Look for opportunities to break down software projects into smaller modules (for example, libraries and applications) to support module-level reuse. Some of the key principles of the module are:

1. Minimize dependencies 2. Each project should have a clear responsibility 3. You should strive to keep your projects small and clear.

Avoid deep inheritance In object-oriented programming, inheritance, especially in virtual functions, is often a dead end in terms of reusability. I had very little success writing or using libraries that overloaded classes.

8. Test during design and development I’m not a core proponent of test-driven development, but when you start writing test code, there are a number of guidelines for writing tests. It also helps us catch n errors earlier. However, to avoid writing useless tests, good coding practices mean that higher-level tests (such as integration tests or feature tests for unit tests) are more effective at revealing defects.

I can’t tell you how often I’ve seen a better version of STD :: Vector or STD :: String, but it’s almost always a waste of time and effort. Other than the fact that you are introducing a new bug (see 10), it is unlikely that other programmers will reuse your code as opposed to it being widely understood, supported, and tested.

Avoid Writing new Code This is the most important tip every programmer should follow: the best code is code that hasn’t been written yet. The more lines of 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 if there is a tool, function, or library that does what you need. You really need to implement that feature yourself, rather than call another one that already exists.

I’ve discovered that programming is a skill very similar to learning an art form or a sport, where you get better by consciously practicing and learning from others. Constant efforts to improve the quality of your code will help you become a more effective programmer.

Translation: cdiggins. Making. IO/translation: blog.csdn.net/xiaole0313/…

If you fell in love with a programmer guy, for all your programmer girlfriends what made you decide to leave a company? How to choose when receiving multiple offers at the same time?

Have you learned anything after reading this article? Please share with more people to follow “Yang Shoule”, improve programming skills

blog.csdn.net/xiaole0313 [QQ technology group] 279126311 [not] [QQ technology group] 484572225 [not]