Read Clean Code

Source: Internet
Author: User
Tags data structures

Recently in the commute to the bus time to read clean code (Code neat way), and once again admire Uncle Bob humorous writing, unique perspective and understanding of perspective. What makes me most refreshing is Uncle Bob's interpretation of data structures and objects.

In general, data structure refers to the carrier of the information, exposing the data, and almost no meaningful behavior, you should scream this is not anemia class? Yes, it's very similar to our anemic type. The most common applications in distributed services, such as Wcf,webservice, Reset, and other distributed services in the data Transfer object (DTO) mode, DTO (Request/response) is a very typical data carrier, there are only simple get, Set property and is more likely to exist as a value object. And the object is just the opposite. As an object-oriented product, it is necessary to encapsulate the hidden data and expose the behavioral interface, and DDD domain model tends to not only manipulate itself or associate state in the data more exposed behavior.

Although the differences between the data structure and the objects are subtle, they lead to different essential distinctions: code that uses data structures facilitates the addition of new behaviors (functions) without altering the current data structure, and object-oriented code adds new classes without altering existing functions. In other words, it's hard to add new data types to the structure, because you need to change all the functions, and object-oriented code is hard to add new functions because you need to modify all the classes. In any complex system where data structures and objects exist at the same time, we need to determine whether we need new data types or new behavior functions to add.

In then is the Dimitri Law, or is translated as the minimum knowledge principle, does not speak to the stranger: the module should not understand the internal situation of the object he operates, the concealment of the object, the more detailed understanding of the method F of an object A, should also only call the object in the following range:

Property fields for Object A

Objects of object A hold

Object passed by the method parameter

Temporary object defined in method F

method should not operate on any object returned by the calling method, in other words, talk to a friend, not to a stranger. For example: Ctxt.getoptions (). Getsearchdir (), GetAbsolutePath (), is the inverse model of the Dimitri Law.

But the premise of current Dimitri law is object, if it is data structure, do not have any behavior, they will naturally expose its internal data structure, Dimitri Law also invalid.

Hidden as the most important feature of object-oriented, encapsulation hidden is the most important feature of object-oriented, a good object-oriented code is definitely to the object's internal details to do a good hiding package, after encapsulation is polymorphic, delegation and so on. A good object-oriented code must have a good hidden encapsulation, easy to test, unstable factors tend to focus on a small or fixed position, the change of instability will not lead to a larger area of the change spread.

Finally recommend the next book, it is worth in the spare time to look at, do not guarantee to read the code must write neat code, to do the Boy Scout discipline "Every check-in code is more tidy than checked out", not like beautiful prose, which needs to be practiced in the project practice to find, but the code quality meaning has a guiding role.

Always feel that their technology is still very far, so I always restrain myself to spare time to read some books, from different perspectives, different areas or some ideas of ascension and precipitation.

On the principle of object-oriented essay there are:

Design-oriented principles of understanding

(reprint) Some principles of software design

Software Architecture Design Proverbs Understand

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.