[Reconstruction learning] 05. Rebuilding and learning 05 Functions

Source: Internet
Author: User

[Reconstruction learning] 05. Rebuilding and learning 05 Functions

The code in refactoring is java. I want to use C.

Today, my main task is to write a large piece of junk code and then refactor it (only for functions, not other refactoring ).

Program interface:

 

Function introduction:

The knight's name is retrieved by himself, and then click the attribute of the random role,

The attribute values of the root bone, meridian, flexibility, and comprehension are all random.

The rest are calculated based on these four attributes:

Root bone: affects qi and blood, basic external attack and basic internal attack

Meridian: affects internal force and basic internal attack

Flexibility: affects body and basic Dodge

 

The Spam code for the button function is as follows:

/// <Summary> // generate a knight // </summary> private void btnCreateSwordsman_Click (object sender, EventArgs e) {var random = new Random (); int boneValue, meridianValue, flexibilityValue, savvyValue; txtSurname. text = "Wang"; txtShortName. text = "Daniel"; boneValue = random. next (10); meridianValue = random. next (10); flexibilityValue = random. next (10); savvyValue = random. next (10); txtBone. text = boneValue. toString (); txtMeridian. text = meridianValue. toString (); txtFlexibility. text = flexibilityValue. toString (); txtSavvy. text = savvyValue. toString (); txtHP. text = (boneValue * 20 + 20 ). toString (); txtMP. text = (meridianValue * 10 ). toString (); txtAGI. text = (flexibilityValue * 5 + 10 ). toString (); txtExteriorAttack. text = (boneValue * 2 ). toString (); txtInsideAttack. text = (meridianValue * 3 + boneValue * 2 ). toString (); txtDodge. text = (flexibilityValue * 1.5/100 ). toString ("p ");}

For ease of understanding, there is very little code, but there is enough garbage. Let's rebuild it step by step through the following learning!

 

Basically, function refactoring is caused by the function being too long. Some say 50 rows, some say 30 rows, and some say a screen. In any case, don't be too long. Obviously, the above functions seem very short. They are actually due to my laziness. For example, the names are random. (Do not worry about the magic numbers and names. If I write half of them, I think we should write random dota hero attributes. I can copy them directly because naming is really troublesome)

All of the following refactoring examples may not show obvious results because the code is simple, and sometimes it may make you feel inexplicable, however, if you think of it as a part of a large system and complicate the logic in it, then these refactoring will become necessary.

1. Extract A function: extract a piece of code from the function, put it into a new function, and ask the function name to explain the purpose of the function.

Motivation: If the granularity of each function is small, the chance of function reuse will be greater, and overwriting will be easier. The higher-level function reads will be like comments.

Practice: Create a new function (what is the name, not how it is done), extract code to the new function (pay attention to the temporary variables and parameters)

Extract functions without local variables:

/// <Summary> // generate a knight // </summary> private void btnCreateSwordsman_Click (object sender, EventArgs e) {RandomSwordsmanName (); RandomSwordsmanAttribute ();} /// <summary> /// generate a random knight name (you pretend to be random) /// </summary> void RandomSwordsmanName () {txtSurname. text = "Wang"; txtShortName. text = "Daniel" ;}/// <summary> /// attributes of the random knight. (According to the refactoring practice, you can leave no comments here, because these function names are clear, annotations are cumbersome.) // </summary> void RandomSwordsmanAttribute () {var random = new Random (); int boneValue, meridianValue, flexibilityValue, savvyValue; boneValue = random. next (10); meridianValue = random. next (10); flexibilityValue = random. next (10); savvyValue = random. next (10); txtBone. text = boneValue. toString (); txtMeridian. text = meridianValue. toString (); txtFlexibility. text = flexibilityValue. toString (); txtSavvy. text = savvyValue. toString (); txtHP. text = (boneValue * 20 + 20 ). toString (); txtMP. text = (meridianValue * 10 ). toString (); txtAGI. text = (flexibilityValue * 5 + 10 ). toString (); txtExteriorAttack. text = (boneValue * 2 ). toString (); txtInsideAttack. text = (meridianValue * 3 + boneValue * 2 ). toString (); txtDodge. text = (flexibilityValue * 1.5/100 ). toString ("p ");}

Function extraction with local variables:

To extract functions that randomly generate four attributes and other attributes, temporary variables are involved. Generally, parameters are passed, and objects are transmitted when many parameters are passed.

 

/// <Summary> /// attributes of the random chivalrous guest /// </summary> void RandomSwordsmanAttribute () {var basicInfo = RandomSwordsmanBasicAttribute (); GetOtherInfoByBasicInfo (basicInfo );} /// <summary> /// basic attributes of the random chivalrous guest /// </summary> /// <returns> </returns> SwordsmanBasicInfo RandomSwordsmanBasicAttribute () {var basicInfo = new SwordsmanBasicInfo (); var random = new Random (); basicInfo. bone = random. next (10); basicInfo. meridian = random. next (10); basicInfo. flexibility = random. next (10); basicInfo. savvy = random. next (10); return basicInfo ;}/// <summary> /// obtain other attributes through the basic attributes of Chivalrous guest, and it is displayed /// </summary> /// <param name = "basicInfo"> </param> void GetOtherInfoByBasicInfo (SwordsmanBasicInfo basicInfo) {txtBone. text = basicInfo. bone. toString (); txtMeridian. text = basicInfo. meridian. toString (); txtFlexibility. text = basicInfo. flexibility. toString (); txtSavvy. text = basicInfo. savvy. toString (); txtHP. text = (basicInfo. bone * 20 + 20 ). toString (); txtMP. text = (basicInfo. meridian * 10 ). toString (); txtAGI. text = (basicInfo. flexibility * 5 + 10 ). toString (); txtExteriorAttack. text = (basicInfo. bone * 2 ). toString (); txtInsideAttack. text = (basicInfo. meridian * 3 + basicInfo. bone * 2 ). toString (); txtDodge. text = (basicInfo. flexibility * 1.5/100 ). toString ("p ");} /// <summary> /// basic attributes of Chivalrous guest /// </summary> public class SwordsmanBasicInfo {/// <summary> // root bone /// </summary> public int Bone {get; set ;}/// <summary> /// Meridian // </summary> public int Meridian {get; set ;} /// <summary> /// flexible /// </summary> public int Flexibility {get; set ;} /// <summary> /// Savvy /// </summary> public int Savvy {get; set ;}}

However, this is not enough. replace some expressions with functions:

/// <Summary> /// other attributes are obtained through the basic attributes of Chivalrous guest, and it is displayed /// </summary> /// <param name = "basicInfo"> </param> void GetOtherInfoByBasicInfo (SwordsmanBasicInfo basicInfo) {txtBone. text = basicInfo. bone. toString (); txtMeridian. text = basicInfo. meridian. toString (); txtFlexibility. text = basicInfo. flexibility. toString (); txtSavvy. text = basicInfo. savvy. toString (); txtHP. text = GetHP (basicInfo. bone ). toString (); txtMP. text = GetMP (basicInfo. meridian ). toString (); txtAGI. text = GetAGI (basicInfo. flexibility ). toString (); txtExteriorAttack. text = GetExteriorAttack (basicInfo. bone ). toString (); txtInsideAttack. text = GetInsideAttack (basicInfo. meridian, basicInfo. bone ). toString (); txtDodge. text = GetDodge (basicInfo. flexibility ). toString ("p");} int GetHP (int bone) {return bone * 20 + 20;} int GetMP (int meridian) {return meridian * 10 ;} int GetAGI (int flexibility) {return flexibility * 5 + 10;} int GetExteriorAttack (int bone) {return bone * 2;} int GetInsideAttack (int bone, int meridian) {return meridian * 3 + bone * 2;} float GetDodge (int flexibility) {return flexibility * 1.5f/100 ;}

. NET also has more interesting dynamic methods:

/// <Summary> /// other attributes are obtained through the basic attributes of Chivalrous guest, and displayed /// </summary> /// <param name = "basicInfo"> </param> void GetOtherInfoByBasicInfo (SwordsmanBasicInfo basicInfo) {SetTextBoxValue (txtBone, basicInfo. bone); SetTextBoxValue (txtMeridian, basicInfo. meridian); SetTextBoxValue (txtFlexibility, basicInfo. flexibility); SetTextBoxValue (txtSavvy, basicInfo. savvy); SetTextBoxValue (txtHP, GetHP (basicInfo. bone); SetTextBoxValue (txtMP, GetMP (basicInfo. meridian); SetTextBoxValue (txtAGI, GetAGI (basicInfo. flexibility); SetTextBoxValue (txtExteriorAttack, GetExteriorAttack (basicInfo. bone); SetTextBoxValue (txtInsideAttack, GetInsideAttack (basicInfo. meridian, basicInfo. bone); SetTextBoxPercentValue (txtDodge, GetDodge (basicInfo. flexibility);} void SetTextBoxValue (TextBox textBox, dynamic num) {textBox. text = num. toString ();} void SetTextBoxPercentValue (TextBox textBox, dynamic percent) {textBox. text = percent. toString ("p ");}

Of course this is still not enough,

Functions such as GetHP can be placed in the SwordsmanBasicInfo class. The entire code is actually divided into two Logics: computing and display. It is necessary to set the computing attribute, extract different functions from the last display attribute and put them in the class.

But here we just want to give a few examples to illustrate the extraction and Reconstruction of the function, so there is no need to continue with it. This piece of junk code will be left behind for reconstruction.

 

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.