Average engineers pile code, good engineers elegant code, great engineers simplify code. How to write elegant, clean and easy to understand code is a science and an important part of software engineering practice. Here are some of the best practices outlined from the perspectives of annotations, naming, methods, concurrency, and more. I believe that every excellent engineer has a pursuit of excellence code heart.
Don’t comment on bad names. A good name is more important than a good comment. Good Code > Bad Code + Good comments Use global comments at the file/class level to explain how all parts work Be sure to comment constants team uniform definition tags TODO unresolved problems FIXME Known problematic code hacks have to adopt crude solutions
Use standard naming methods as much as possible, such as design patterns and general academic nouns. Use more expressive words. Use more specialized words, such as fetch or download instead of get. Avoid generic names, such as TMP. For example, if you add the unit ms or something like that, you have a longer name, and if you have a shorter name, you have a Boolean variable and you add is, has, can, should or something like that
Method functions shouldn’t be 100 lines long, 20 lines at the top and it’s good to have control statements like if else while where the code block should be one line, that is, a function call and there shouldn’t be more than two levels of locking in a function that only does one thing, A function should not be able to abstract out another function a private function that is called by a public function ideally with zero arguments, no more than three input arguments, try not to output arguments if a function takes three or more arguments it’s best to abstract them as class identifiers ugly, Passing booleans to functions to distinguish between different businesses is ugly and should be split into multiple functions
Exceptions and errors Are removed from the block of code contained in a try catch, which is abstracted as each exception thrown by a function, and should provide sufficient context to determine the source and location of the error without attributing system errors to chance events
Concurrency separation Concurrency related code strictly restricts access to data that may be shared with other code Avoid using multiple synchronization methods for a shared object to keep synchronization regions small and design critical regions as little as possible
1. What are your best tips for clean code engineering practices?
2. How to ensure clean and consistent code developed by hundreds of people?
3. What realm does your code fall into? Are you looking for clean code?