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
  • Never used JAXB? Check out a simple usage pattern that pairs #JAXB’s data binding capabilities with JPA - http://t.co/Ki9G04HV5e
    July 24, 2014 at 9:53 AM
  • Guess what today is? Tell An Old Joke Day - http://t.co/835ORWMX6N! So, why do programmers always confuse Halloween & Xmas? 31 Oct = 25 Dec
    July 24, 2014 at 8:45 AM
  • MT @midwestio: Posted another #midwestio talk recording to our YouTube channel: @MinaMarkham on modular CSS. Watch: http://t.co/aU3LpfUoi4
    July 24, 2014 at 8:25 AM
  • We just posted pictures from our National Hot Dog Day Lunch Cookout. Check them out - http://t.co/To06plaw1C
    July 23, 2014 at 4:14 PM
  • Good free cheat sheet - #Java Performance Optimization Refcard from @DZone: http://t.co/7vBgsmqy08
    July 23, 2014 at 10:48 AM
  • Did you know today is a holiday? It's National Hot Dog Day! We're gearing up for our team lunch hot dog cookout & can't wait to celebrate.
    July 23, 2014 at 9:43 AM
  • Check out our newest blog: #JAXB – A Newcomer’s Perspective, Part 1 http://t.co/Ki9G04HV5e
    July 22, 2014 at 1:22 PM
  • New post on the Keyhole blog by Mark Adelsberger: #JAXB – A Newcomer’s Perspective, Part 1 http://t.co/Ki9G04HV5e
    July 21, 2014 at 2:27 PM
  • If you're a Java dev, you're likely familiar with Annotations. But have you created your own #Java Annotations? Ex - http://t.co/BgCsYjxZKF
    July 18, 2014 at 12:10 PM
  • RT @gamasutra: Don't Miss: Unconventional Tips for Improving your Programming Skills http://t.co/6TFox7CKHU
    July 16, 2014 at 3:20 PM
  • We're about to send out our free monthly tech newsletter. Dev tips/articles via email. Not on the list yet? Sign up - http://t.co/F8h0NSiicZ
    July 15, 2014 at 11:57 AM
  • Have you ever tried creating your own #Java annotations? See a situation where it was beneficial - http://t.co/BgCsYjxZKF
    July 15, 2014 at 8:36 AM
  • There's a new post on the Keyhole blog by @jhackett01: Creating Your Own #Java Annotations - http://t.co/BgCsYjxZKF
    July 14, 2014 at 1:43 PM
  • We love development! Have you seen our weekly team blog? We show how to be successful with the tech we use. See it - http://t.co/nlRtb1XNQH
    July 12, 2014 at 2:35 PM
  • Rapid appdev has a bad rep, but there are ways to bring development time down the right way. Don't Fear the Rapid - http://t.co/aTPcAKOj0r
    July 11, 2014 at 3:10 PM
  • Automated Testing is great for dev, but does bring a set of challenges (especially for #agile teams). Success tips: http://t.co/1acl1ngO7i
    July 11, 2014 at 12:16 PM
  • This is fantastic - One small step for Google, one giant leap for empowering girls to code: http://t.co/R90V5DBkv1
    July 10, 2014 at 2:52 PM
  • #RabbitMQ: messaging software built on AMQP protocol. Learn relevant concepts & how to avoid common "gotchas" here: http://t.co/ZwMXlhKyX8
    July 10, 2014 at 9:31 AM
  • A huge welcome to Brian Wetzel who joins the Keyhole team today!
    July 10, 2014 at 8:50 AM
  • Seen our newest blog? @joshuarob01 talks the challenges #Agile teams face in automated testing & how to solve them: http://t.co/1acl1ngO7i
    July 9, 2014 at 3:51 PM
Keyhole Software
8900 State Line Road, Suite 455
Leawood, KS 66206
ph: 877-521-7769
© 2014 Keyhole Software, LLC. All rights reserved.