6.8. Overloading Functions

In many languages, method names can be overloaded. Overloading allows two methods to use the same name, if they can be distinguished by their signature. A method's signature is defined by the number of its parameters and types. For example, you could have the following:

        search(int an_int)
        search(String a_string)

Why not use this overloading feature? If you use a text search to find one of the search methods in source code or other documents, you would wind up with matches that call all the methods with the same name.[*] You would have to examine each call closely to see if the call represents the method you are actually looking for. If you spell out the type of search in the name of each method, you can easily search for a particular method.[†]

[*] Eric M. Burke, a reviewer, noted that a decent integrated development environment (IDE), including IntelliJ IDEA and Eclipse, provides a "find usages" function that searches for methods based on the structure of the code.

[†] One reviewer noted that you could have a single search method that takes a filter as its parameter. A filter denotes a function that is called by a search method to determine if an object is in the set of desired objects.

In addition, making the search method name explicit permits searching for different attributes that have the same data type. For example, you could use the following:

        search_for_first_name(CommonString a_string)
        search_for_last_name(CommonString a_string)

Now notice ...

Get Prefactoring now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.