Programming with Castalia : Refactoring : Rename Local Variable
Previous  Top  Next

A local variable's name does not adequately describe the data represented by the variable.

Change the variable's name to something more appropriate.



function CalculateAge(Birthday: TDateTime): Integer;  
var  
  I: Integer;  
begin  
  I := Trunc(Now - Birthday);  
  if I < 18 then  
    raise EUnderageCustomer.CreateFmt('Customer is only %d', [I]);  
  Result := I;  
end;  
 
downarrow  
 
function CalculateAge(Birthday: TDateTime): Integer;  
var  
  CustomerAge: Integer;  
begin  
  CustomerAge := Trunc(Now - Birthday);  
  if CustomerAge < 18 then  
    raise EUnderageCustomer.CreateFmt('Customer is only %d', [CustomerAge]);  
  Result := CustomerAge;  
end;  
 
To use Rename Local Variable, place the cursor in any instance the variable to be renamed and invoke Rename Local Variable from the Castalia Refactoring menu. Castalia will identify all instances of the variable and allow you to modify the name of the variable, propgating your changes to all instances of the variable. To finish, press Enter.


See Also: Refactoring Overview, Inline Temporary Variable Refactoring, Split Temporary Variable Refactoring