The document discusses refactoring code. It covers bad smells in code, building tests, and composing methods. It then provides details on self-testing code and catalogs common refactoring techniques like extracting methods, replacing temporary variables, and simplifying conditional expressions.
6. // I decided not to use built-in feature to
convert string to boolean
if ( is_string($switch) ) {
switch ($switch) {
Public Function Save() As Boolean case 'false' :
Try case 'down' :
SaveMeeting() case 'off' :
Catch ex As Exception case 'not' :
Throw ex case '0' :
End Try case '' :
End Function $switch = false;
break;
default :
$switch = true;
break;
}
}
<a href="/the/path/to/the/url"
onclick="window.open(this.getAttribute('href'),'_blank');return false;">
link text
</a>
Confidential 6
7. // Translates Roman Numbers to
Decimal Numbers
Function GetNewGuid() public string rom2num(string r)
Dim cnGuid, rsGuid {
if (r == "I") return "1";
Set cnGuid = CreateObject("ADODB.Connection") if (r == "II") return "2";
Set rsGuid = CreateObject("ADODB.Recordset") if (r == "III") return "3";
if (r == "IV") return "4";
cnGuid.Open = _ if (r == "V") return "5";
"Provider=SQLOLEDB.1;" + if (r == "VI") return "6";
"Data Source=<production server>; " + if (r == "VII") return "7";
"Initial Catalog=<production DB> " + if (r == "VIII") return "8";
"user id = '********';" + if (r == "IX") return "9";
"password='*********'"" //
// Snipped LOTS of "code" here
rsGuid.Open "SELECT newid() as Guid", cnGuid //
if (r == "MMVIII") return "2008";
If Not rsGuid.EOF Then if (r == "MMIX") return "2009";
GetNewGuid = rsGuid("Guid").Value if (r == "MMX") return "2010";
End If if (r == "MMXI") return "2011";
End Function return "E";
}
Confidential 7
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 8
10. # Find the last 200 transactions
logging.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 10
11. Bad Smells in Code
Building Tests
Composing Methods
Confidential 11
14. Bad Smells in Code
Building Tests
Composing Methods
Confidential 14
15. Catalog of Refactorings
Composing Methods Simplifying Conditional
Moving Features Expressions
Between Objects Making Method Calls
Organizing Data Simpler
Dealing with
Generalization
Confidential 15
16. Inline Method
Extract Method
Substitute Algorithm
Replace Temp with Query Inline Temp
Introduce Explaining Variable
Replace Method with Method Object
Split Temporary Variable
Remove Assignments to Parameters
Confidential 16
Who is it about? other presentsWhat happened? other presentsWhen did it take place?Where did it take place? computerWhy did it happen? other presentsHow did it happen? other presentsGive audience some real example indications that there is trouble that can be solved by a refactoringhttp://thedailywtf.com/Articles/FAIL-FAIL,FAIL-FAIL,FAIL-FAIL-and-More.aspxhttp://thedailywtf.com/Comments/The-Bit-Setter.aspxhttp://thedailywtf.com/Comments/Mybad.aspxhttp://thedailywtf.com/Articles/Self-Documenting.aspxhttp://thedailywtf.com/Articles/Roman-Enumeration.aspx
Add a Hungarian Notation prefix to a configuration file setting, in case we weren't sure that 'ContactEmailAddress' was a string
This is to convert a string to false value and don’t want to use built-in language feature
self-document code
what the link between the 3 numbers is
Vaccine is kind of alike to self-testing code. It operates the system (virus) on real environment (body) in safe manner so that issues can be found at very soon
Some time is spent figuring out what ought to be going on, some time is spent designing, but most time is spent debugging. Fixing the bug is usually pretty quick, but finding it is a nightmare
Sample coffee script to demonstrate : extract method, introduce explaining variable, replace temp with queryStoryBoard = require './storyboard'Constants = require './core/Constants'class ViewerScreen extends GameScreenplayerMovesInChineseChess: (response) -> if response.group == Global.GROUP.CHINESE_CHESS and response.type == Global.TYPE.PLAYER_MOVES_IN_CHINESE_CHESS_TABLE if response.code == Global.CODE.BROADCASTING_REQUEST if response.data.player == Constants.HOST_ROLE @view.startTopTimer() @view.stopBottomTimer() else response.data.player == Constants.GUEST_ROLE @view.stopTopTimer() @view.startBottomTimer()killChineseChess: (response) -> if response.group == Global.GROUP.CHINESE_CHESS and response.type == Global.TYPE.KILL_CHINESE_CHESS_TABLE if response.code == Global.CODE.BROADCASTING_REQUEST data = response.data if @match.status < Global.GAME_STATUS.PLAYING @msgBar.showdata.username + ' ' + Global.MUI.HAS_QUITEDhostKicksGuestOutOfChineseChess : (response) -> if response.group == Global.GROUP.CHINESE_CHESS and response.type == Global.TYPE.KICKS_GUEST_OUT_OF_CHINESE_CHESS_TABLE if response.code == Global.CODE.BROADCASTING_REQUEST data = response.data @msgBar.show @match.host.username + ' ' + Global.MUI.KICKS + ' ' + @match.guest.usernamedetailsOfTableCallback: (response) -> if response.group == Global.GROUP.CHINESE_CHESS and response.type == Global.TYPE.GET_DETAILS_OF_CHINESE_CHESS_TABLE if response.code == Global.CODE.SUCCESSFUL_REQUEST data = response.data if @match.status >= Global.GAME_STATUS.WAITING then# ... else if response.code == Global.CODE.FAILED_REQUEST# ...