The Jade In the mountains can be reconstructed: ID card number 4 (fourth day)

Source: Internet
Author: User
Document directory
  •  

It can be said that only today can I really start to refactor/improve what I want. Previously, I just supplemented the test and adjusted the structure.
Yes, it's very slow. It's actually called "agile "!? Aren't you surprised?
Fortunately, this rhythm is suitable for young people of this age. It is good to use it together, whether it is wind or snow.

= Test coverage

The most important asynchronous mode was missed last time. Test Coverage:

I thought it would show off a beautiful 100% coverage test. The person is not as good as the day, but there is actually a method of 75%!
(Copyright: 2012-2013 Yu qin'an)

Well, the birthday is not tested.

It's easy, just add a test, and it's not here. Paste the coverage rate directly to display it.

The following code quality parameters are displayed:

The most complex is the constructor. Maintenance indicators are still good at 76 points.

==> Optimization improvements: attributes, static settings and others

As in the previous section, you can perform vertical changes based on one test. You can also test all changes together. The former is a strict test drive. However, I don't think it should be too academic. The key is that your task is small enough to be completed today.

1. Change all information blocks to attribute methods, because one is the difference between Java and C #, and the other is the ultimate logic of the buffer birthday of the original code (extreme programming? Oh) buffer at the beginning (in the constructor)

 public string CardNumber { get;private set; } public string AddressCode { get; private set; } public DateTime BirthDate { get; private set; } public Gender Gender { get; private set; }

2. Data Parsing is placed in the constructor and independently implemented as a method. It is called only in the constructor.

void extract()        {            AddressCode = CardNumber.Substring(0, 6);            Gender = ((int) CardNumber[CARD_NUMBER_LENGTH - 2])%2 == 0 ? Gender.Female : Gender.Male;            BirthDate = extract_birth_date();        }

Date is complex enough, so the method is independent.

Public datetime extract_birth_date () {try {return datetime. parseexact (cardNumber. substring (6, 8), birth_date_format, null);} catch (exception e) {Throw new applicationexception ("the date of birth of the ID card is invalid ");}}

3. From the previous code analysis parameters, we can see that the constructor is too complex and mainly involves several verifications. Make an improvement, propose a verification method, and remove null, empty verification because the regular expression already contains.

 private void validate(string cardNumber)        {            if (!SOCIAL_NUMBER_PATTERN.IsMatch(cardNumber))                throw new ApplicationException("Card Number has wrong charactor(s).");            if (cardNumber[CARD_NUMBER_LENGTH - 1] != verifier.verify(cardNumber))                throw new ApplicationException("Card Number verified code is not match.");        }

 

public SocialID(String cardNumber)        {            validate(cardNumber);            CardNumber= cardNumber;            extract();        }
= OK. Now you can stand up and have a cup of coffee to enjoy our results.

The maintainability is increased to 82, and the highest complexity is validate () 3,

Complete code, isn't it clear?

Using system; using system. text. regularexpressions; namespace skight. eliteweb. domain {public Enum gender {female, male} public class socialid {Private Static Verifier verifier = new Verifier (); Private Static string birth_date_format = "yyyymmdd"; Private Static int card_number_length = 18; private Static RegEx social_number_pattern = new RegEx (@ "^ [0-9] {17} [0-9x] $"); Public socialid (string cardnum Ber) {validate (cardNumber); cardNumber = cardNumber; extract ();} private void validate (string cardNumber) {If (! Social_number_pattern.ismatch (cardNumber) throw new applicationexception ("card number has wrong charactor (s)."); If (cardNumber [card_number_length-1]! = Verifier. verify (cardNumber) throw new applicationexception ("card number verified code is not match. ");} void extract () {addresscode = cardNumber. substring (0, 6); Gender = (INT) cardNumber [card_number_length-2]) % 2 = 0? Gender. female: gender. male; birthdate = extract_birth_date ();} public datetime extract_birth_date () {try {return datetime. parseexact (cardNumber. substring (6, 8), birth_date_format, null);} catch (exception e) {Throw new applicationexception ("the date of birth of the ID card is invalid");} Public String cardNumber {Get; private set;} Public String addresscode {Get; private set;} public datetime birthdate {Get; private set;} public gender Gender {Get; private set ;}}}

(The Copyright in this article is 2012-2013 Yu Qinan | repost the article to indicate the author and source)

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.