Upload
duy-lam
View
259
Download
0
Embed Size (px)
DESCRIPTION
Citation preview
RefactoringGroup 1
Confidential 2
Bad Smells in Code
Building Tests
Composing Methods
Confidential 3
Bad Smells in Code
Who What When
Where Why How
Confidential 4
Confidential 5
…..<app key=“ContactEmailAddress” value=“[email protected]” />
…..
app.config (revision 1)
…..<app key=“sContactEmailAddress” value=“[email protected]” />…..
app.config (revision 2)
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>
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";}
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);}
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;}
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;}
Confidential 11
Bad Smells in Code
Building Tests
Composing Methods
Confidential 12
Self-testing Code
Confidential 13
Without Self-testing Code
Developer jobs
Fix bugs Others
Confidential 14
Bad Smells in Code
Building Tests
Composing Methods
Confidential 15
Catalog of Refactorings
Composing MethodsMoving Features
Between ObjectsOrganizing Data
Simplifying Conditional Expressions
Making Method Calls Simpler
Dealing with Generalization
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
THANK YOU