C # basic series Question 6 clean code

Source: Internet
Author: User
Document directory
  • One function only does one thing.
  • Single Responsibility Principle)
  • One premise: unit testing is required.
  • Cost of "small"
One function only does one thing.

A function should only do one thing, so that you can better name your function and understand and read the code. If you have to break this rule in a special situation, you can stop and think about whether your understanding of this "special situation" is not enough. The function should execute one thing precisely and only execute this thing.

Uncle Bob mentioned this in his "clean code" and some other books on code and even architecture.

Before reading this point of view, I found that I would walk in this direction without knowing it. After knowing this point of view, I began to regard it as a habit-of course, I admit that I write a lot of code that does not meet this requirement, Because I know there is still a distance from the road.

But if you want to make it easier for yourself or others to maintain the code, you should try to do it as much as possible and then try to persuade your colleagues to do it well.

Single Responsibility Principle)

Classes will become smaller and smaller-this is not your purpose, but when you follow the object-oriented class design principles, the classes will become smaller. We hope to implement high cohesion and low coupling of classes. When you want to modify a class, you should have only one reason, that is, the class should have only one responsibility.

The single responsibility principle is a very important concept. A lot of people may be afraid of small classes like me at the beginning-think it will make the project more difficult to understand-for such a situation, I 'd like to say that you should try it first, what are the results? This is not an adventure, especially when we have a powerful Visual Studio management view.

One premise: unit testing is required.

One premise for writing "clean" code is to have a unit test? You may think that I have gone wrong. However, I found that if we didn't establish a unit test, we wouldn't have the courage to constantly modify our code, or even have no way to sleep. Okay. I can change the title to the premise of sleep: unit testing is required.

Cost of "small"

Generally, the steps for a person to change the mode are to hear a new concept-> suspect-> execute-> regret-> stick to it-> Accept or return to the original state. So what is the cost for functions and classes to become smaller? My own experiences and experiences are:

There is no time for reconstruction of existing code-it is understandable that when the demand changes, when I want to change the existing code, I began to refactor them in this direction-to make them more "clean" than before I "came ".

For new code, it is constantly restructured during the writing process-you can't make your code perfect in the first place. After a few hours of writing, you find that your code is not beautiful. It doesn't matter. Don't doubt your capabilities. It takes time to refactor, because neat code is completed in refactoring.

Related Article

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.