17
Refactoring Group 1

Refactoring group 1 - chapter 3,4,6

  • Upload
    duy-lam

  • View
    259

  • Download
    0

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Refactoring   group 1 - chapter 3,4,6

RefactoringGroup 1

Page 2: Refactoring   group 1 - chapter 3,4,6

Confidential 2

Bad Smells in Code

Building Tests

Composing Methods

Page 3: Refactoring   group 1 - chapter 3,4,6

Confidential 3

Bad Smells in Code

Who What When

Where Why How

Page 4: Refactoring   group 1 - chapter 3,4,6

Confidential 4

Page 5: Refactoring   group 1 - chapter 3,4,6

Confidential 5

…..<app key=“ContactEmailAddress” value=“[email protected]” />

…..

app.config (revision 1)

…..<app key=“sContactEmailAddress” value=“[email protected]” />…..

app.config (revision 2)

Page 6: Refactoring   group 1 - chapter 3,4,6

Confidential 6

// I decided not to use built-in feature to convert string to boolean

if ( is_string($switch) ) { switch ($switch) { case 'false' : case 'down' : case 'off' : case 'not' : case '0' : case '' : $switch = false; break;

default : $switch = true; break; }}

Public Function Save() As Boolean Try SaveMeeting() Catch ex As Exception Throw ex End TryEnd Function

<a href="/the/path/to/the/url" onclick="window.open(this.getAttribute('href'),'_blank');return false;"> link text</a>

Page 7: Refactoring   group 1 - chapter 3,4,6

Confidential 7

Function GetNewGuid() Dim cnGuid, rsGuid Set cnGuid = CreateObject("ADODB.Connection") Set rsGuid = CreateObject("ADODB.Recordset")

cnGuid.Open = _ "Provider=SQLOLEDB.1;" + "Data Source=<production server>; " + "Initial Catalog=<production DB> " + "user id = '********';" + "password='*********'""

rsGuid.Open "SELECT newid() as Guid", cnGuid

If Not rsGuid.EOF Then GetNewGuid = rsGuid("Guid").Value End IfEnd Function

// Translates Roman Numbers to Decimal Numberspublic string rom2num(string r){ if (r == "I") return "1"; if (r == "II") return "2"; if (r == "III") return "3"; if (r == "IV") return "4"; if (r == "V") return "5"; if (r == "VI") return "6"; if (r == "VII") return "7"; if (r == "VIII") return "8"; if (r == "IX") return "9"; // // Snipped LOTS of "code" here // if (r == "MMVIII") return "2008"; if (r == "MMIX") return "2009"; if (r == "MMX") return "2010"; if (r == "MMXI") return "2011"; return "E";}

Page 8: Refactoring   group 1 - chapter 3,4,6

Confidential 8

function checkdata() { dataok = true; t1 = document.forms.signup.firstName.value; t2 = document.forms.signup.lastName.value; t9 = document.forms.signup.locale.options.selectedIndex; t10 = document.forms.signup.currency.options.selectedIndex; t11 = document.forms.signup.timezone.options.selectedIndex; t12 = document.forms.signup.packetType.options.selectedIndex; t13 = document.forms.signup.captcha_code.value; if (t1 == '' || t2 == '‘ ) { alert("Please fill-up all the fields"); dataok = false; return(dataok);} if (t4 != t5) { alert("Please enter the password again"); dataok = false; return(dataok);} if (t9 == 0) { alert("Please select a locale"); dataok = false; return(dataok);} if (t10 == 0) { alert("Please select a currency"); dataok = false; return(dataok);} return(dataok);}

Page 9: Refactoring   group 1 - chapter 3,4,6

Confidential 9

// Self-document code

private bool TrueBecauseThisEventDoesNotRegistrictBasedUponActivityType(){ return true;}

private bool FalseBecauseNoActivityTypesAreAvailableForUsersWhoAreNotLoggedIn(){ return false;}

private bool NoActivityTypesAreAttachedToThisEvent(IEnumerable<ActivityType> activityTypes){ return activityTypes.Any() == false;}

Page 10: Refactoring   group 1 - chapter 3,4,6

Confidential 10

# Find the last 200 transactionslogging.debug( "Finding the last 600 transactions" )ConnMysql.query("select Id from Transactions order by ts desc limit 0,10000 ")

private void EditTDemensions(bool blnYes) { //bln is the introvert of blnYes bool bln = (blnYes ? false : true);

//Display or Hide buttons btnDemensionsEdit.Visible = bln; btnDemensionsSave.Visible = blnYes; btnDemensionsCancel.Visible = blnYes;}

Page 11: Refactoring   group 1 - chapter 3,4,6

Confidential 11

Bad Smells in Code

Building Tests

Composing Methods

Page 12: Refactoring   group 1 - chapter 3,4,6

Confidential 12

Self-testing Code

Page 13: Refactoring   group 1 - chapter 3,4,6

Confidential 13

Without Self-testing Code

Developer jobs

Fix bugs Others

Page 14: Refactoring   group 1 - chapter 3,4,6

Confidential 14

Bad Smells in Code

Building Tests

Composing Methods

Page 15: Refactoring   group 1 - chapter 3,4,6

Confidential 15

Catalog of Refactorings

Composing MethodsMoving Features

Between ObjectsOrganizing Data

Simplifying Conditional Expressions

Making Method Calls Simpler

Dealing with Generalization

Page 16: Refactoring   group 1 - chapter 3,4,6

Confidential 16

Extract Method Inline Method

Inline TempReplace Temp with Query

Introduce Explaining Variable

Split Temporary Variable

Remove Assignments to Parameters

Replace Method with Method Object

Substitute Algorithm

Page 17: Refactoring   group 1 - chapter 3,4,6

THANK YOU