namespaceLosTechies.DaysOfRefactoring.PullUpMethod.Before{publicabstractclassVehicle{// other methods
}publicclassCar:Vehicle{publicvoidTurn(Directiondirection){// code here
}}publicclassMotorcycle:Vehicle{}publicenumDirection{Left,Right}}
namespaceLosTechies.DaysOfRefactoring.PullUpMethod.After{publicabstractclassVehicle{publicvoidTurn(Directiondirection){// code here
}}publicclassCar:Vehicle{}publicclassMotorcycle:Vehicle{}publicenumDirection{Left,Right}}
namespaceLosTechies.DaysOfRefactoring.PushDownMethod.Before{publicabstractclassAnimal{publicvoidBark(){// code to bark
}}publicclassDog:Animal{}publicclassCat:Animal{}}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
namespaceLosTechies.DaysOfRefactoring.PushDownMethod.After{publicabstractclassAnimal{}publicclassDog:Animal{publicvoidBark(){// code to bark
}}publicclassCat:Animal{}}
namespaceLosTechies.DaysOfRefactoring.Rename.Before{publicclassPerson{publicstringFN{get;set;}publicdecimalClcHrlyPR(){// code to calculate hourly payrate
return0m;}}}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
namespaceLosTechies.DaysOfRefactoring.Rename.After{// Changed the class name to Employee
publicclassEmployee{publicstringFirstName{get;set;}publicdecimalCalculateHourlyPay(){// code to calculate hourly payrate
return0m;}}}
namespaceLosTechies.DaysOfRefactoring.ExtractInterface.Before{publicclassClassRegistration{publicvoidCreate(){// create registration code
}publicvoidTransfer(){// class transfer code
}publicdecimalTotal{get;privateset;}}publicclassRegistrationProcessor{publicdecimalProcessRegistration(ClassRegistrationregistration){registration.Create();returnregistration.Total;}}}
namespaceLosTechies.DaysOfRefactoring.ExtractInterface.After{publicinterfaceIClassRegistration{voidCreate();decimalTotal{get;}}publicclassClassRegistration:IClassRegistration{publicvoidCreate(){// create registration code
}publicvoidTransfer(){// class transfer code
}publicdecimalTotal{get;privateset;}}publicclassRegistrationProcessor{publicdecimalProcessRegistration(IClassRegistrationregistration){registration.Create();returnregistration.Total;}}}
namespaceLosTechies.DaysOfRefactoring.BreakDependencies.Before{publicclassAnimalFeedingService{privateboolFoodBowlEmpty{get;set;}publicvoidFeed(){if(FoodBowlEmpty)Feeder.ReplenishFood();// more code to feed the animal
}}publicstaticclassFeeder{publicstaticvoidReplenishFood(){// fill up bowl
}}}
namespaceLosTechies.DaysOfRefactoring.BreakDependencies.After{publicclassAnimalFeedingService{publicIFeederServiceFeederService{get;set;}publicAnimalFeedingService(IFeederServicefeederService){FeederService=feederService;}privateboolFoodBowlEmpty{get;set;}publicvoidFeed(){if(FoodBowlEmpty)FeederService.ReplenishFood();// more code to feed the animal
}}publicinterfaceIFeederService{voidReplenishFood();}publicclassFeederService:IFeederService{publicvoidReplenishFood(){Feeder.ReplenishFood();}}publicstaticclassFeeder{publicstaticvoidReplenishFood(){// fill up bowl
}}}
usingSystem.Collections.Generic;namespaceLosTechies.DaysOfRefactoring.ExtractMethodObject.Before{publicclassOrderLineItem{publicdecimalPrice{get;privateset;}}publicclassOrder{privateIList<OrderLineItem>OrderLineItems{get;set;}privateIList<decimal>Discounts{get;set;}privatedecimalTax{get;set;}publicdecimalCalculate(){decimalsubTotal=0m;// Total up line items
foreach(OrderLineItemlineIteminOrderLineItems){subTotal+=lineItem.Price;}// Subtract Discounts
foreach(decimaldiscountinDiscounts)subTotal-=discount;// Calculate Tax
decimaltax=subTotal*Tax;// Calculate GrandTotal
decimalgrandTotal=subTotal+tax;returngrandTotal;}}}
usingSystem.Collections.Generic;namespaceLosTechies.DaysOfRefactoring.ExtractMethodObject.After{publicclassOrderLineItem{publicdecimalPrice{get;privateset;}}publicclassOrder{publicIEnumerable<OrderLineItem>OrderLineItems{get;privateset;}publicIEnumerable<decimal>Discounts{get;privateset;}publicdecimalTax{get;privateset;}publicdecimalCalculate(){returnnewOrderCalculator(this).Calculate();}}publicclassOrderCalculator{privatedecimalSubTotal{get;set;}privateIEnumerable<OrderLineItem>OrderLineItems{get;set;}privateIEnumerable<decimal>Discounts{get;set;}privatedecimalTax{get;set;}publicOrderCalculator(Orderorder){OrderLineItems=order.OrderLineItems;Discounts=order.Discounts;Tax=order.Tax;}publicdecimalCalculate(){CalculateSubTotal();SubtractDiscounts();CalculateTax();returnSubTotal;}privatevoidCalculateSubTotal(){// Total up line items
foreach(OrderLineItemlineIteminOrderLineItems)SubTotal+=lineItem.Price;}privatevoidSubtractDiscounts(){// Subtract Discounts
foreach(decimaldiscountinDiscounts)SubTotal-=discount;}privatevoidCalculateTax(){// Calculate Tax
SubTotal+=SubTotal*Tax;}}}
usingSystem;namespaceLosTechies.DaysOfRefactoring.EncapsulateConditional.Before{publicclassRemoteControl{privatestring[]Functions{get;set;}privatestringName{get;set;}privateintCreatedYear{get;set;}publicstringPerformCoolFunction(stringbuttonPressed){// Determine if we are controlling some extra function
// that requires special conditions
if(Functions.Length>1&&Name=="RCA"&&CreatedYear>DateTime.Now.Year-2)return"doSomething";}}}
usingSystem;namespaceLosTechies.DaysOfRefactoring.EncapsulateConditional.After{publicclassRemoteControl{privatestring[]Functions{get;set;}privatestringName{get;set;}privateintCreatedYear{get;set;}privateboolHasExtraFunctions{get{returnFunctions.Length>1&&Name=="RCA"&&CreatedYear>DateTime.Now.Year-2;}}publicstringPerformCoolFunction(stringbuttonPressed){// Determine if we are controlling some extra function
// that requires special conditions
if(HasExtraFunctions)return"doSomething";}}}
usingSystem.Collections.Generic;usingSystem.Linq;namespaceLosTechies.DaysOfRefactoring.SampleCode.BreakMethod.Before{publicclassCashRegister{publicCashRegister(){Tax=0.06m;}privatedecimalTax{get;set;}publicvoidAcceptPayment(Customercustomer,IEnumerable<Product>products,decimalpayment){decimalsubTotal=0m;foreach(Productproductinproducts){subTotal+=product.Price;}foreach(Productproductinproducts){subTotal-=product.AvailableDiscounts;}decimalgrandTotal=subTotal*Tax;customer.DeductFromAccountBalance(grandTotal);}}publicclassCustomer{publicvoidDeductFromAccountBalance(decimalamount){// deduct from balance
}}publicclassProduct{publicdecimalPrice{get;set;}publicdecimalAvailableDiscounts{get;set;}}}
usingSystem.Collections.Generic;namespaceLosTechies.DaysOfRefactoring.SampleCode.BreakMethod.After{publicclassCashRegister{publicCashRegister(){Tax=0.06m;}privatedecimalTax{get;set;}privateIEnumerable<Product>Products{get;set;}publicvoidAcceptPayment(Customercustomer,IEnumerable<Product>products,decimalpayment){decimalsubTotal=CalculateSubtotal();subTotal=SubtractDiscounts(subTotal);decimalgrandTotal=AddTax(subTotal);SubtractFromCustomerBalance(customer,grandTotal);}privatevoidSubtractFromCustomerBalance(Customercustomer,decimalgrandTotal){customer.DeductFromAccountBalance(grandTotal);}privatedecimalAddTax(decimalsubTotal){returnsubTotal*Tax;}privatedecimalSubtractDiscounts(decimalsubTotal){foreach(ProductproductinProducts){subTotal-=product.AvailableDiscounts;}returnsubTotal;}privatedecimalCalculateSubtotal(){decimalsubTotal=0m;foreach(ProductproductinProducts){subTotal+=product.Price;}returnsubTotal;}}publicclassCustomer{publicvoidDeductFromAccountBalance(decimalamount){// deduct from balance
}}publicclassProduct{publicdecimalPrice{get;set;}publicdecimalAvailableDiscounts{get;set;}}}
namespaceLosTechies.DaysOfRefactoring.SampleCode.ParameterObject.Before{publicclassRegistration{publicvoidCreate(decimalamount,Studentstudent,IEnumerable<Course>courses,decimalcredits){// do work
}}}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
usingSystem.Collections.Generic;namespaceLosTechies.DaysOfRefactoring.SampleCode.ParameterObject.After{publicclassRegistrationContext{publicdecimalAmount{get;set;}publicStudentStudent{get;set;}publicIEnumerable<Course>Courses{get;set;}publicdecimalCredits{get;set;}}publicclassRegistration{publicvoidCreate(RegistrationContextregistrationContext){// do work
}}}
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingMicrosoft.Contracts;namespaceLosTechies.DaysOfRefactoring.SampleCode.DesignByContract.After{publicclassCashRegister{publicdecimalTotalOrder(IEnumerable<Product>products,Customercustomer){if(customer==null)thrownewArgumentNullException("customer","Customer cannot be null");if(products.Count()==0)thrownewArgumentException("Must have at least one product to total","products");decimalorderTotal=products.Sum(product=>product.Price);customer.Balance+=orderTotal;if(orderTotal==0)thrownewArgumentOutOfRangeException("orderTotal","Order Total should not be zero");returnorderTotal;}}}
usingSystem.Collections.Generic;usingLosTechies.DaysOfRefactoring.SampleCode.BreakMethod.After;namespaceLosTechies.DaysOfRefactoring.SampleCode.DoubleNegative.Before{publicclassOrder{publicvoidCheckout(IEnumerable<Product>products,Customercustomer){if(!customer.IsNotFlagged){// the customer account is flagged
// log some errors and return
return;}// normal order processing
}}publicclassCustomer{publicdecimalBalance{get;privateset;}publicboolIsNotFlagged{get{returnBalance<30m;}}}}
usingSystem.Collections.Generic;usingLosTechies.DaysOfRefactoring.SampleCode.BreakMethod.After;namespaceLosTechies.DaysOfRefactoring.SampleCode.DoubleNegative.After{publicclassOrder{publicvoidCheckout(IEnumerable<Product>products,Customercustomer){if(customer.IsFlagged){// the customer account is flagged
// log some errors and return
return;}// normal order processing
}}publicclassCustomer{publicdecimalBalance{get;privateset;}publicboolIsFlagged{get{returnBalance>=30m;}}}}
usingSystem.Collections.Generic;usingLosTechies.DaysOfRefactoring.EncapsulateCollection.After;usingLosTechies.DaysOfRefactoring.SampleCode.BreakMethod.After;usingCustomer=LosTechies.DaysOfRefactoring.BreakResponsibilities.After.Customer;namespaceLosTechies.DaysOfRefactoring.SampleCode.RemoveGodClasses.Before{publicclassCustomerService{publicdecimalCalculateOrderDiscount(IEnumerable<Product>products,Customercustomer){$$// do work
}publicboolCustomerIsValid(Customercustomer,Orderorder){// do work
}publicIEnumerable<string>GatherOrderErrors(IEnumerable<Product>products,Customercustomer){// do work
}publicvoidRegister(Customercustomer){// do work
}publicvoidForgotPassword(Customercustomer){// do work
}}}
usingSystem.Collections.Generic;usingLosTechies.DaysOfRefactoring.EncapsulateCollection.After;usingLosTechies.DaysOfRefactoring.SampleCode.BreakMethod.After;usingCustomer=LosTechies.DaysOfRefactoring.BreakResponsibilities.After.Customer;namespaceLosTechies.DaysOfRefactoring.SampleCode.RemoveGodClasses.After{publicclassCustomerOrderService{publicdecimalCalculateOrderDiscount(IEnumerable<Product>products,Customercustomer){// do work
}publicboolCustomerIsValid(Customercustomer,Orderorder){// do work
}publicIEnumerable<string>GatherOrderErrors(IEnumerable<Product>products,Customercustomer){// do work
}}publicclassCustomerRegistrationService{publicvoidRegister(Customercustomer){// do work
}publicvoidForgotPassword(Customercustomer){// do work
}}}
usingLosTechies.DaysOfRefactoring.BreakResponsibilities.After;namespaceLosTechies.DaysOfRefactoring.SampleCode.RenameBooleanMethod.Before{publicclassBankAccount{publicvoidCreateAccount(Customercustomer,boolwithChecking,boolwithSavings,boolwithStocks){// do work
}}}
usingLosTechies.DaysOfRefactoring.BreakResponsibilities.After;namespaceLosTechies.DaysOfRefactoring.SampleCode.RenameBooleanMethod.After{publicclassBankAccount{publicvoidCreateAccountWithChecking(Customercustomer){CreateAccount(customer,true,false);}publicvoidCreateAccountWithCheckingAndSavings(Customercustomer){CreateAccount(customer,true,true);}privatevoidCreateAccount(Customercustomer,boolwithChecking,boolwithSavings){// do work
}}}
usingLosTechies.DaysOfRefactoring.PullUpField.After;namespaceLosTechies.DaysOfRefactoring.SampleCode.RemoveMiddleMan.Before{publicclassConsumer{publicAccountManagerAccountManager{get;set;}publicConsumer(AccountManageraccountManager){AccountManager=accountManager;}publicvoidGet(intid){Accountaccount=AccountManager.GetAccount(id);}}publicclassAccountManager{publicAccountDataProviderDataProvider{get;set;}publicAccountManager(AccountDataProviderdataProvider){DataProvider=dataProvider;}publicAccountGetAccount(intid){returnDataProvider.GetAccount(id);}}publicclassAccountDataProvider{publicAccountGetAccount(intid){// get account
}}}
usingLosTechies.DaysOfRefactoring.PullUpField.After;namespaceLosTechies.DaysOfRefactoring.SampleCode.RemoveMiddleMan.After{publicclassConsumer{publicAccountDataProviderAccountDataProvider{get;set;}publicConsumer(AccountDataProviderdataProvider){AccountDataProvider=dataProvider;}publicvoidGet(intid){Accountaccount=AccountDataProvider.GetAccount(id);}}publicclassAccountDataProvider{publicAccountGetAccount(intid){// get account
}}}
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingLosTechies.DaysOfRefactoring.SampleCode.BreakMethod.After;namespaceLosTechies.DaysOfRefactoring.SampleCode.ReplaceWithPolymorphism.Before{publicabstractclassCustomer{}publicclassEmployee:Customer{}publicclassNonEmployee:Customer{}publicclassOrderProcessor{publicdecimalProcessOrder(Customercustomer,IEnumerable<Product>products){// do some processing of order
decimalorderTotal=products.Sum(p=>p.Price);TypecustomerType=customer.GetType();if(customerType==typeof(Employee)){orderTotal-=orderTotal*0.15m;}elseif(customerType==typeof(NonEmployee)){orderTotal-=orderTotal*0.05m;}returnorderTotal;}}}
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingLosTechies.DaysOfRefactoring.SampleCode.BreakMethod.After;namespaceLosTechies.DaysOfRefactoring.SampleCode.ReplaceWithPolymorphism.After{publicabstractclassCustomer{publicabstractdecimalDiscountPercentage{get;}}publicclassEmployee:Customer{publicoverridedecimalDiscountPercentage{get{return0.15m;}}}publicclassNonEmployee:Customer{publicoverridedecimalDiscountPercentage{get{return0.05m;}}}publicclassOrderProcessor{publicdecimalProcessOrder(Customercustomer,IEnumerable<Product>products){// do some processing of order
decimalorderTotal=products.Sum(p=>p.Price);orderTotal-=orderTotal*customer.DiscountPercentage;returnorderTotal;}}}