Avoid over-streamlining temporary variables-refactoring

Source: Internet
Author: User


Avoid over-streamlining temporary variables-refactoring

 

During the refactoring process, a problem occurred in my code and an endless loop occurred!

An important means of refactoring is to remove the hateful temporary variables and change them to method calls. This is an idea that Martin Fowler emphasizes.

A major means and goal of refactoring is to extract methods.
The code of a large segment is divided into many micro functions. A major obstacle to function extraction is the appearance of variables everywhere, especially temporary variables that sometimes do not use much.

As a result, I took the temporary variable as a thorn in the eye, and tried every means to delete it. I used multiple methods to call these temporary variables instead.

 

However, a fatal endless loop occurs during indirect code execution!

 

Please refer to this method:

Public Boolean
Isnonerepeatinlist (list excelmodellist) throws exception {

/*

* Do not use more than one method. It is still required for appropriate temporary variables!

*
Note: Some methods are still factory methods. If you use them together, it will lead to the problem of creating an instance every time!

**/

Od1adsluserforecastmodelservice Service
= (Od1adsluserforecastmodelservice)
Getbean (systemconstantssdl. od1adsluserforecastmodel_service );


While (excelmodellist. iterator (). hasnext ()){

Od1adsluserforecastmodel
Tmpmodel = (od1adsluserforecastmodel) excelmodellist. iterator (). Next ();

List
Repeatmodellist = service. querybysectionnamesubstationnamenodenameforecastyear1forecastquarter1name (tmpmodel );

If (repeatmodellist. Size ()> 1 ){

// Delete this batch of records deletebybatchnum (string
Batchnum)

// Public void
Deletebybatchnum (datadeclarequerymodeladslforecast model) throws exception


Service. deletebybatchnum (tmpmodel );

Throw new telecomexception ("info. howdoiferror", "Sorry, the Excel file you uploaded contains the entered data. Please check your Excel file! ");

}

}

Return true;

}

 

In this method, I use the code excelmodellist. iterator (). hasnext () and excelmodellist. iterator (). Next.

Excelmodellist. iterator () actually creates an object.

The above two sentences lead to a new object every time, and then call its. hasnext (). In this way, even if list has only one object, it can always have. hasnext () = true!

 

This is the problem!

Therefore, the following conclusions can be drawn:

1,
Do not use multiple methods consecutively. Only one method can be called at a time. To avoid mistakes!

2,
Do not exclude the use of temporary variables. Temporary variables make the code easier to understand.
Although sometimes they provide redundant information, this is much better than the error implementation caused by insufficient information!

------- Do not miss it!

3,
In fact, the temporary variables we oppose are the temporary variables that oppose the multiple meanings of the word.

That is, we oppose a temporary variable being assigned multiple times in different statements-representing multiple semantics.

4. The solution is to ensure that the variable value is assigned once. Adding final to the variable can explicitly prevent the variable from being assigned multiple times!

 

Enclose the correct code:

Public Boolean isnonerepeatinlist (list
Excelmodellist) throws exception {


/*


* Do not use more than one method.
Appropriate temporary variables are required!


* Note: Some methods are still factory methods. If you use them together, it will lead to the problem of creating an instance every time!


**/


Od1adsluserforecastmodelservice service =
(Od1adsluserforecastmodelservice)
Getbean (systemconstantssdl. od1adsluserforecastmodel_service );


Iterator = excelmodellist. iterator ();


While (iterator. hasnext ()){


Od1adsluserforecastmodel
Tmpmodel = (od1adsluserforecastmodel) iterator. Next ();


List
Repeatmodellist = service. querybysectionnamesubstationnamenodenameforecastyear1forecastquarter1name (tmpmodel );


If (repeatmodellist. Size ()> 1 ){

// Delete this batch of records deletebybatchnum (string
Batchnum)

// Public void
Deletebybatchnum (datadeclarequerymodeladslforecast model) throws exception



Service. deletebybatchnum (tmpmodel );

Throw new
Telecomexception ("info. howdoiferror", "Sorry, the Excel file you uploaded contains the entered data. Please check your Excel file! ");


}


}


Return true;

}

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.