Method Implementation

A Few Thoughts About Method Implementation

by on September 30, 2013 8:39 am

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.

	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

  • Share:

One Response to “A Few Thoughts About Method Implementation”

  1. Phil says:

    Back in the olden days of OOP, the idea was that you had self-contained objects that communicated with each other through messaging. I think I might revise Uncle Bob’s preferences to each parameter ideally have 0 or 1 arguments. It should definitely not be a goal to have parameterless methods, because that way of thinking will lead you down the path of blocks of code managing their own dependencies (and cats and dogs sleeping together).

    The flip side, of course, is that these messages were supposed to contain everything a method would need to do its job, so it could be a primitive type all the way to a many-faceted object. Long chains of parameters are definitely a code smell no matter how you look at it and at the very least are signs that your logic is probably too complex to implement in a single method as opposed to encapsulating the different scenarios.

Leave a Reply

Things Twitter is Talking About
  • New to #JavaScript prototypal inheritance? Here are some notes to help you along the way - http://t.co/NTIDZS6Uhy
    July 5, 2015 at 7:55 AM
  • ICYMI: we've released a demo version of #GrokOla which is open to the public. Try out its features & capabilities - http://t.co/O4ladowmFU
    July 4, 2015 at 3:05 PM
  • Happy 4th of July from the Keyhole team! We hope that you have a happy and safe holiday with your family and friends.
    July 4, 2015 at 9:55 AM
  • Let's talk testing. Here are common challenges #Agile teams face when writing automated tests & how to overcome them: http://t.co/DrKbNZJcE0
    July 3, 2015 at 11:06 AM
  • #GrokOla users get free educational tutorials. But lucky you, we've released some to the public. #JavaScript primer - http://t.co/nIR9XiWY6O
    July 3, 2015 at 10:55 AM
  • Being able to isolate debugging techniques can help make you a better debugger. Here's Time-Oriented #Debugging http://t.co/UplJgP4VzC
    July 2, 2015 at 10:50 AM
  • RT @zachagardner: @zachagardner has declared it is @ChipotleTweets day at @KeyholeSoftware . You have been warned 🐓🐖🐄
    July 2, 2015 at 10:09 AM
  • Current state of random number generation & the differences in how #Java & #JavaScript approach it - http://t.co/5tBKNXnu8T #security
    July 1, 2015 at 2:45 PM
  • Woohoo - 600 followers! Thanks, everyone. We'd love to ask you - what type of tweets / dev content would you like to see more of from us?
    July 1, 2015 at 10:38 AM
  • We would like to welcome Dallas Monson to the team today! Dallas is a Senior Architect focused on UI/UX and #JavaScript. Welcome, Dallas!
    July 1, 2015 at 8:35 AM
  • Good introduction to TypeScript - http://t.co/0N22fVpAHt Plus, how to approach modularization in #TypeScript - http://t.co/wxRWGBj3Uh
    June 30, 2015 at 3:25 PM
  • .@mrbristopher just delivered a new S911 Night Drone to James Hayes, winner of our #kcdc15 giveaway! Congrats, James! http://t.co/RriJIxubH2
    June 30, 2015 at 11:35 AM
  • It feels like primitives could have been left out of the initial implementation of #Java. See why - http://t.co/A8ChCBHXJO
    June 29, 2015 at 4:05 PM
  • Developers in a bounce house! I repeat, developers in a bounce house! We had a blast at our 1st company picnic. Pics: http://t.co/XIqs7ECUst
    June 29, 2015 at 1:40 PM
  • New #SpringBatch tutorial from @jhackett01: Spring Batch – Replacing XML Job Configuration With JavaConfig http://t.co/PmdXnriKQu #java
    June 29, 2015 at 11:46 AM
  • We had such a fun time at the Keyhole company picnic! Pictures to come, including some of our developers in the bounce house. #loveourteam
    June 29, 2015 at 8:41 AM
  • In #JavaScript, how do we harness the power of callbacks without the confusing mess of nested functions? Promises - http://t.co/j1gAJ9hi3D
    June 29, 2015 at 8:40 AM
  • .@zachagardner We are so happy that your family attended! This will definitely need to be repeated every year!
    June 28, 2015 at 8:14 PM
  • Thank you to all on the Keyhole team who came to our first inaugural company picnic! Wonderful food, family and bounce house fun!
    June 28, 2015 at 7:50 PM
  • Debugging is a challenging part of being a programmer. We have a tutorial series to help, with a #JavaScript focus - http://t.co/rfhjJo64P7
    June 27, 2015 at 1:45 PM