Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

& lt;< Code cleanliness (clean code) & key Reading Notes of gt;>

2025-03-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

Neat code

1. The essentials of practicing art

1)。 Knowledge: acquire knowledge about principles, patterns, and practices, do everything you should know, and know it like the back of your hand

2)。 OK: master it through hard practice

two。 The reason for reading this book

1). You're a programmer.

2). You want to be a better programmer.

3. LeBron's rule:

Later equals never.

4. Taking the time to keep your code clean is not only about efficiency, but also about survival.

5. It is also unprofessional for programmers to follow the wishes of managers who do not understand the risks of chaos.

6. The only way to meet the deadline, the only way to do it quickly-is to keep the code as clean as possible

7. Broken window theory

8. Neat code does only one thing.

Strive to focus, each function, every class, module are focused on one thing, completely free from the interference and pollution of the surrounding details

9. Neat code should clearly show the tension to solve the problem.

10. Simple code rules:

In order of importance:

. Can pass all the tests.

There is no duplicate code

Embody all the design concepts in the system

Include as few entities as possible, such as classes, methods, functions, etc.

No duplicate code:

If the same piece of code appears repeatedly, it means that some idea has not been well reflected in the code and should be done.

To find out what it is, and then try to express it more clearly.

11. Improve the power of expression

1). A meaningful naming

2). Just do one thing.

3). Small scale abstraction

twelve。 If you want to do it quickly, if you want to finish it early, if you want to write code easily, make the code easy to read.

A meaningful naming

1. Once you find a better name, replace the old one.

two。 be worthy of the name

If the name needs to be supplemented by comments, it is not worthy of the name.

3. Avoid misleading

Make a meaningful distinction

Use a readable name

Use a searchable name

Such as: MAX_CLASSES_PRE_STUDENT

4. Class name

Class and object names should be nouns or noun phrases, such as Customer WikiPage Account

AddressParser

It shouldn't be a verb.

5. Method name

Should be a verb or phrasal verb, such as postPayment deletePage save

Property accessors, modifiers, and assertions should be prefixed with get, set, and is according to their names

Function

1. Shortness is the first rule of a function.

two。 Just do one thing, do it well.

3. One level of abstraction per function

Such as getHtml () high

In PathParser.render (pagePath)

.append ("\ n") low

Functions are mixed with different levels of abstraction, which can be confusing

Read the code from top to bottom: downward rule

4. Function parameter

The ideal parameter is zero (zero parameter function), followed by one, try to avoid functions with three or more parameters.

From the point of view of testing, the more parameters, the more difficult to measure.

5. The function of the conversion result should be reflected in the return value

6. We should try to convert a binary function into a unary function (meta: the number of parameters).

7. Parameter object

If the function appears to require three or more parameters, some of them should be enclosed as classes

Such as:

Circle makeCircle (double x, double y, double radius)

| |

Circle makeCircle (Point center, double radius)

Create objects from parameters, thereby reducing the number of parameters, and when a set of parameters are passed together, it is often part of a concept that should have its own name

8. Verbs and key words

Giving a good name to the function can better explain the intention of the function, as well as the order and intention of the parameters.

Such as unary function writeFile (name) verb / noun

Encode the name of the parameter into the function name, as shown in

AssertEqual- > assertExceptedEqualsActual (excepted, actual)

9. Avoid using output parameters and modify the state of the object if the function must modify a certain state

Most of the need for output parameters in object-oriented languages has disappeared, because this also means output parameters

10. Separate instructions from queries

Public boolean set (String attribute, String value)

If (set ("username", "unclebob").

The above instructions and queries are in the set function and should be changed to

If (attributeExists ("username")) {

SetAttribute ("username", "unclebob")

}

11. Use exception instead of return error code

Try {

DeletePage (page)

Registry.deleteReference (page.name)

ConfigKeys.deleteKey (page.name.makeKey ())

} catch (Exception e) {

Logger.log (e.getMessage ())

}

Perfect isolation (try catch code block body extraction)

Public void delete (Page page) {

Try {

DeletePageAndAllReferences (page)

} catch (Exception e) {

LogError (e)

}

}

Private void deletePageAndAllReferences (Page page) throws Exception {

DeltePage (page)

Registry.deleteReference (page.name)

ConfigKeys.deleteKey (page.name.makeKey ())

}

Private void logError (Exception e)

{

Logger.log (e.getMessage ())

}

twelve。 Error handling is one thing.

As in the code above, if the keyword try exists in a function, it is the first word of the function, and it should not have after the catch/finally code block.

Other content

13. Dependent on magnet

Public enum Error {

OK

INVALID

NO_SUCH

LOCKED

}

Using exceptions instead of error codes, new exceptions can be derived from the exception class without having to recompile or redeploy

14. Repetition may be the root of all evil in software.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 271

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report