Click here to Skip to main content
15,886,873 members
Articles
Technical Blog
(untagged)

A Few Thoughts About Method Implementation

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
1 Oct 2013CPOL3 min read 12K   2   5
Practices in development that, while they don’t explicitly violate any standards (whether unwritten or not), they leave behind a "stench" of inexperience or lack of discipline.

In chapter 17 of "Clean Code" by Robert C. Martin, the author describes the idea of code smells, practices in development that, while they don’t explicitly violate any standards (whether unwritten or not), they leave behind a "stench" of inexperience or lack of discipline. I like this idea; it seems to me that I encounter these "code smells" fairly often (given the diverse teams I often work with) and they can make code difficult to decipher and maintain.

Take, for instance, the number of parameters on a method. According to Martin, optimally, there would be zero parameters, but any number up to and including three is acceptable. Three is an arbitrary number selected by the author, of course, but I think my own restrictions would fall within the same range, though it would certainly sometimes depend on the conditions. Sometimes it is just not possible to put such a restriction on the parameter list size, especially when working on an already established code base, but I do have some suggestions.

  1. Do a significant number of the parameters originate from the same object instance? Try passing in the containing object instead and refer to the attributes as needed inside the method.
  2. Alternatively, implement the method on the object itself, thus eliminating the need to refer to another object within the method.
  3. Sometimes, altering the parameter list is just not possible without a significant re-factor of the involved code. If the risk of the re-factor is too high (usually due to time constraints), one option is to build a transient object that encapsulates all of the attributes needed for the completion of the method’s function, passing this single object to the method, or again, alternatively, having the method exist on the transient object itself.

Another "smelly" practice is the idea of "output arguments". There is an expectation that parameters should be merely an input to the method, and to not be altered by the logic of the method. It is suggested that this practice is holdover from pre-OOP days where output arguments would be necessary, but this is no longer the case. It is now better practice to think of the reserved word “this” as the output argument. Again, the best way to express this practice is to have the object affected by the logic implement the method on itself.

Flag arguments are booleans that are passed to the method, signifying that the behavior might be performing two separate functions. The better alternative to this is to implement the logic into two methods, one implementing the logic executed if the parameter is set to true, the other implementing the logic executed if the parameter is set to false. This forces you to also give more meaningful function names to both implementations, making the intention of each much more clear to whomever needs to use them. Sometimes a flag argument is used in this manner because both logic paths share a significant number of logic steps before the diverge from each other. If this is the case, you would be better off to create a third method that implements the common logic path, while still keeping the divergent logic in the other two methods.

One idea that the author did not mention, but which I felt was relevant to this topic, was the usage of a local method variable as the return value to a method as opposed to returning the object itself. To clarify, see the two implementation examples below. The first simply returns the number if the condition is met, while the second sets the number on a local variable and returns the variable at the end of the method.

C#
int getNumber(){
    if(condition1){
        return 1;
    } else if(condition2){
        return 2;
    } else {
        return null;
    }
}

int getNumber(){
    int returnValue = null;
    if(condition1){
        returnValue = 1;
    else if(condition2){
        returnValue = 2;
    }
    return returnValue;
}

The second implementation I feel is superior for at least one reason. It is always abundantly clear where the method exits because it can only exit in one place. This makes it much easier to understand and maintain.

Finally, and simply, there is the "dead function", the function that is not called anywhere in the system. Source control exists for a reason; use it!

– Robert Rice, asktheteam@keyholesoftware.com

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Keyhole Software
United States United States
Keyhole is a software development and consulting firm with a tight-knit technical team. We work primarily with Java, .NET, and Mobile technologies, specializing in application development. We love the challenge that comes in consulting and blog often regarding some of the technical situations and technologies we face. Kansas City, St. Louis and Chicago.
This is a Organisation

3 members

Comments and Discussions

 
QuestionMore than 3 parameters on a constructor Pin
GuyThiebaut2-Oct-13 0:22
professionalGuyThiebaut2-Oct-13 0:22 
AnswerRe: More than 3 parameters on a constructor Pin
John Brett3-Oct-13 2:32
John Brett3-Oct-13 2:32 
GeneralRe: More than 3 parameters on a constructor Pin
GuyThiebaut3-Oct-13 3:09
professionalGuyThiebaut3-Oct-13 3:09 
GeneralRe: More than 3 parameters on a constructor Pin
Bruce Patin9-Oct-13 4:55
Bruce Patin9-Oct-13 4:55 
GeneralRe: More than 3 parameters on a constructor Pin
GuyThiebaut9-Oct-13 4:58
professionalGuyThiebaut9-Oct-13 4:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.