|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
Java的B/s开发是通常是javaweb开发,又叫J2EE开发,J2SE是手机开发。C#的C/s和B/s开发是说.net和Asp开发。。u在这里说明一点;资深一点的Java和C#程序员都明白一点
5.IntroduceExplainingVariableIfyouhaveacomplicatedexpression,puttheresultoftheexpression,orpartsoftheexpression,inatemporaryvariablewithanamethatexplainsthepurpose.IntroduceExplainingVariableisparticulalyvaluablewithconditionallogicinwhichitisusefultotakeeachclauseofaconditionandexplainwhattheconditionmeansawell-namedtemp.Anothercaseisalongalgorithm,inwhicheachstepinthecomputationcanbeexplainedwithatemp.Mechanics(1)Declareafinaltemporaryvariable,andsetittotheresultofpartofthecomplexexpression.(2)Replacetheresultpartoftheexpressionwiththevalueofthetemp.Before:doubleprice(){returnquantity*itemPrice-Math.max(0,quantity-500)*itemPrice*0.05+Math.min(quantity*itemPrice*0.1,100.0);}After:doubleprice(){finaldoublebasePrice=quantity*itemPrice;finaldoublequantityDiscount=Math.max(0,quantity-500)*itemPrice*0.05;finaldoubleshipping=Math.min(quantity*itemPrice*0.1,100.0);returnbasePrice-quantityDiscount+shipping;}
Appendix:usingExtractMethodtocompleterefactoring.ThebenefitofusingExtractMethodisthatthesesmethodologiesareavailabletoanyotherpartoftheobjectthatneedsthem.Althoughtheyareprivateatfirst,butyoucanrelaxthatifanotherobjectneedsthem.
doubleprice(){returnbasePrice()-quantityDiscount()+shipping();}privateDoublebasePrice(){returnquantityDiscount*itemPrice;}privateDoublequantityDiscount(){returnMath.min(quantity*itemPrice*0.1,100.0);}privateDoubleshipping(){returnMath.min(quantity*itemPrice*0.1,100.0);}
6.SplitTemporaryVariableIfatemporaryvariableassignedtomorethanonce,butisnotaloopvariablenoracollectingtemporaryvariable(Ifthelaterassignmentsareoftheformi=i+someexpression,thatindicatesthatitisacollcetingtemporaryvariable),youshoulduseSplitTemporaryMethod.Anyvariablewithmorethanoneresponsibilityshouldbereplacedwithatempforeachresponsibility.Michanics:(1)Changethenameofatempatitsdeclarationanditsfirstassignment.(2)Declarethenewtempasfinal.(3)Changeallreferencesofthetempuptoitssecondassignment.(4)Declarethetempatitssecondassignment.Before:doublegetDistanceTravelled(inttime){doubleresult;doubleacc=primaryForce/mass;intprimaryTime=Math.min(time,delay);result=0.5*acc*primaryTime*primaryTime;intsecondaryTime=time-delay;if(secondaryTime>0){doubleprimaryVel=acc*delay;acc=(primaryForce+secondaryForce)/mass;result+=primaryVel*secondaryTime+0.5*acc*secondaryTime*secondaryTime;}returnresult;}After:doublegetDistanceTravelled(inttime){doubleresult;finaldoubleprimaryAcc=primaryForce/mass;intprimaryTime=Math.min(time,delay);result=0.5*primaryAcc*primaryTime*primaryTime;intsecondaryTime=time-delay;if(secondaryTime>0){doubleprimaryVel=primaryAcc*delay;finaldoublesecondaryAcc=(primaryForce+secondaryForce)/mass;result+=primaryVel*secondaryTime+0.5*secondaryAcc*secondaryTime*secondaryTime;}returnresult;}
Appendix:CompleterefactoringdoublegetDistanceTravelled(inttime){doubleresult;result=0.5*getPrimaryAcc()*Math.pow(getPrimaryTime(time),2);if(getSecondaryTime(time)>0){doubleprimaryVel=getPrimaryAcc()*delay;finaldoublesecondaryAcc=(primaryForce+secondaryForce)/mass;result+=primaryVel*getSecondaryTime(time)+0.5*secondaryAcc*Math.pow(getSecondaryTime(time),2);}returnresult;}privateintgetPrimaryTime(inttime){returnMath.min(time,delay);}privatedoublegetPrimaryAcc(){returnprimaryForce/mass;}privateintgetSecondaryTime(inttime){returntime-delay;}
7.RemoveAssignmentstoParametersBefore:intdiscount(intinputVal,intquantity,intyearToDate){if(inputVal>50)inputVal-=2;if(quantity>100)inputVal-=1;if(yearToDate>10000)inputVal-=4;returninputVal;}
After:intdiscount(intinputVal,intquantity,intyearToDate){intresult=inputVal;if(inputVal>50)result-=2;if(quantity>100)result-=1;if(yearToDate>10000)result-=4;returnresult;}
8.ReplaceMethodwithMethodObjectIfthereisalongmethodthatuseslocalvariablesinsuchawaythatyoucannotapplyExtractMethod.Turnthemothodintoitsownobjectsothatallthelocalvariablesbecamefieldsonthatobject.Youcanthendecomposethemethodintoothermethodsonthesameobject.Michanics:(1)Createanewclass,nameitafterthemethod.(2)Givethenewclassafinalfieldfortheobjectthathostedtheoriginalmethodandafieldforeachtemporaryvariableandeachparameterintthemethod.(3)Givethenewclassaconstructorthattakesthesourceobjectandeachparameter.(4)Givethenewclassamethodnamed“compute”andcopythebodyoftheoriginalmethodintoit.
Before:classAccount{intgamma(intinputVal,intquantity,intyearToDate){intimportantVal1=(inputVal*quantity)+delta();intimportantVal2=(inputVal*yearToDate)+100;if((yearToDate-importantVal1>100))importantVal2-=20;intimportantVal3=importantVal2*7;returnimportantVal3-2*importantVal1;}}
After:classAccount{intgamma(intinputVal,intquantity,intyearToDate){returnnewGamma(this,inputVal,quantity,yearToDate);}}
classGamma{publicGamma(Accountaccount,intinputVal,intquantity,intyearToDate){account=account;inputVal=inputVal;quantity=quantity;yearToDate=yearToDate;}finalAccountaccount;intimportantVal1;intimportantVal2;intimportantVal3;intinputVal;intquantity;intyearToDate;
intcompute(){intimportantVal1=(inputVal*quantity)+account.delta();intimportantVal2=(inputVal*yearToDate)+100;if((yearToDate-importantVal1>100))importantVal2-=20;intimportantVal3=importantVal2*7;returnimportantVal3-2*importantVal1;}}
9.SubstitueAlgorithmReplaceanalgorithmwithonethatisclearer.
net程序员的大部门代码都靠控件拖拽完成的,虽然java也有,但是无论从美观和速度上都没发和.net比。java程序员都是代码完成的,所以java程序员常戏称.net程序员是操作员,呵呵。 |
|