On simple design II: simple! = Less

Source: Internet
Author: User

See also lightening talk: simple design

A frequently encountered dialog is: "Why is a class introduced here? The string is good, simple design "or" a switch/case should be done. What should I do with a subclass ?"

The fact is usually the opposite,Introducing a new type or subclass is easier than using a string or switch/case.. Let me explain it.

A simple design criterion is whether it is easier to understand. our understanding of things is built on concepts or models. for example, to create a simple supermarket package storage system, you can store packages and retrieve packages,No further requirements.The concept involved may include a package, a locker, and a credential for getting the package. In this case, an empty class is introduced in my design.Public class package {}To represent the package, another empty classPublic class ticket {}To represent the creden. You told me that strings should be used because they are simpler and there will be no new requirements. I disagree. Let's look at the two designs.CodeFor example, saving a package:

    • Public ticket put (package ){...}
    • Public String put (string package ){...}

Use:

    • New Cabinet (). Put (New Package ());
    • New Cabinet (). Put ("Some package ");

Even if there is no need for expansion, even if the first design has two more empty classes than the second design, the first design is simpler than the second one because it is easier to understand.. It is consistent with the concept and vocabulary of saving packets in my head and does not need to be translated. when using a string, I have to translate it into a package and a credential after seeing the code. No one even explained this code to me. I still don't know whether to translate it, it hinders my understanding, so it is more complicated. this is the first angle

Ubiquitous language

The second aspect is communication issues. when describing the requirements of the storage package system, we will inevitably mention the concept of packages and creden. tester will certainly use such words to write test cases, but it will suddenly disappear from the code, there are only strings left. You tell me this is a simple design, and you are a confusing design.

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.