How to write clean code

Quotations from "neat way of code"

3.12 How to write code like this :

Write code and write something else like. When you write papers or articles, you first think of what to write, and then polish him. Perhaps the first draft of crude disorderly, you will, where appropriate scrutiny, until you reach the eyes look.

I write a function, at the beginning are lengthy and complex. There are too many indented and nested loops. There are long list of parameters. Name is free to take, there will be duplication of code. But I will accompanied by a set of unit tests cover every line of ugly code.

Then I polished the code, split function, change the name, eliminating duplication. I shorten and resettlement methods. Sometimes I break classes. While maintaining the test.

Finally, follow the rules listed in this chapter, I assembled these functions.

I do not just write a function in accordance with the rules from the very beginning, I do not think anyone can do it.

Recalling the past used to write code that is, in accordance with the provisions of the document interface defines the function name or function name in accordance with its own function definitions. Then a function of a pass in the end, you can do functional testing. Due to the time, not to consider readability, simplicity of code.

After reading this book, I try to modify their own code written in the past and found that in the past there are many non-standard function definitions, many functions in violation of the [Single Responsibility Principle (single responsibility principle, SRP)], also in violation of the [open closed principle (open closed principle, OCP)]. Lead back to optimize their code, I found myself before I do not know what this code is used to do, and the business logic code has become blurred.

I should write code that, like the first realization of quotations based on business logic function, then polish the code to make it neat. "Let campsite cleaner than when you come."

On how to write clean code, current experience is: First, split function, to comply with the principle of single responsibility, as far as possible so that each function only one thing; Second, simplify the parameter list, no arguments, a single parameter (easy to understand) , two parameters (difficult to understand), three parameters (error-prone, can not accurately confirm the order parameter), when the parameter exceeds three above described parameters should be packaged as such; third, pulled try / catch block , so that only exception handling exception processing, instead of using the exception return error codes; Fourth, eliminate redundancy oriented programming or component-oriented programming, structured programming.

Here to make a break, such as reading the summary of the remaining part again.

Guess you like

Origin www.cnblogs.com/liyy7520/p/How_to_write_clean_code.html