Rename method
Encyclopedia
Rename Method is a refactoring
that changes a name of a method
into a new one, that better reveals its purpose.
To have clearer, more understandable code, programmers would optimally want to change method names to reflect exactly what the method does. For example:
public int foo(int x){
return x+1;
}
would be better renamed as:
public int addOneTo(int x){
return x+1;
}
The function operates the same, but is now more easily readable for developers looking through the code. Note that in some cases there is little necessity for documentation of a simple function if the function or variable name is correct. In these cases the documentation is redundant and may be confusing as it may not match the variable or function name - leading the developer to be unsure as to which (the name or the documentation) is correct. Martin Fowler
lists this as one of his refactorings at refactoring.com.
If a codebase has provided an external API that including the existing method name then the existing function should not be removed, but 'deprecated' over time and finally removed at a future date. In this case the "Copy Method" refactoring can be considered as an alternative.
Refactoring
Code refactoring is "disciplined technique for restructuring an existing body of code, altering its internal structure without changing its external behavior", undertaken in order to improve some of the nonfunctional attributes of the software....
that changes a name of a method
Method (computer science)
In object-oriented programming, a method is a subroutine associated with a class. Methods define the behavior to be exhibited by instances of the associated class at program run time...
into a new one, that better reveals its purpose.
To have clearer, more understandable code, programmers would optimally want to change method names to reflect exactly what the method does. For example:
public int foo(int x){
return x+1;
}
would be better renamed as:
public int addOneTo(int x){
return x+1;
}
The function operates the same, but is now more easily readable for developers looking through the code. Note that in some cases there is little necessity for documentation of a simple function if the function or variable name is correct. In these cases the documentation is redundant and may be confusing as it may not match the variable or function name - leading the developer to be unsure as to which (the name or the documentation) is correct. Martin Fowler
Martin Fowler
-Online presentations:* at RailsConf 2006* at JAOO 2006* at QCon London 2007 * at QCon London 2008 * at ThoughtWorks Quarterly Technology Briefing, October 2008...
lists this as one of his refactorings at refactoring.com.
Take care
Although it seems to be very easy to change a method name, code can be broken if refactoring tools (built into many modern IDE's) are not used to also change all code that refers to the existing method.If a codebase has provided an external API that including the existing method name then the existing function should not be removed, but 'deprecated' over time and finally removed at a future date. In this case the "Copy Method" refactoring can be considered as an alternative.
- Java 5.0 provides the annotationJava annotationAn annotation, in the Java computer programming language, is a special form of syntactic metadata that can be added to Java source code. Classes, methods, variables, parameters and packages may be annotated...
@Override to help protect against mistakes such as this.