Refactoring Learning 1-Renaming methods, classes, parameters, etc.

Source: Internet
Author: User

View myselfArticleIndex Via http://www.cnblogs.com/seesea125/archive/2012/04/17/2453256.html

 

Motivation: Rename is the most basic and simple refactoring of refactoring, But This refactoring is also very important.CodeThe key first step for readability is "to become a real programmer, the level of name is crucial". If you see a function name, it cannot express its purpose very well, it should be modified immediately. Remember, your code is first written for humans, and then for computers. And people need functions with good names ."

Method: first consider how to annotate the function, and then try to annotate the function name.

Note:

Do not be afraid of long names. length is not a problem. The key lies in the semantic distance between the function name and the function ontology.

2. You must name the method name, including the class name, parameter name, control name, And webform, however, each team should have a unified specification.

Example:

 ///   <Summary>  ///  Determine if it is a technology department  ///   </Summary> ///   <Returns> </returns>  Public   Bool   Isscience ( Int Departmentid, String  ID) {Datatable dt = Attendance. getdepmentid (ID );  Bool   DTT  = ( From Column In DT. asenumerable () Select Column [ "  ID  "  ]). Contains (dimension mentid );  If  (DTT ){  Return   True  ;}  Else  {  Return   False  ;}} 

 

First, isscience made others unable to understand what it meant, and another string ID parameter did not know what it meant, and DTT did not know what it meant.

The code after reconstruction is as follows:

 ///   <Summary>  ///  Determine if it is a technology department  ///   </Summary>  ///   <Returns> </returns>  Public   Bool   Ifmintech ( Int Departmentid, String Departmentidlist) {Datatable dt = Getmintechallinclumentid (inclumentidlist );  Bool I Sdepartmentid = ( From Column In DT. asenumerable () Select Column [ "  ID  "  ]). Contains (dimension mentid );  If (ISdepartmentid){  Return   True  ;}  Else  {  Return   False  ;}} 

Although the Refactoring is simple, the effect is obvious. Remember that we write code for people, instead of writing code for computers. If the name does not reflect the intention of the code, you can immediately get rid of it, otherwise, it will waste you countless times to read and understand the code. What's more annoying is that you have read and understood the code today, and you have to read and understand it again in a few days.

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.