7 Replies - 759 Views - Last Post: 10 July 2011 - 06:53 AM Rate Topic: -----

#1 kiwi_steve  Icon User is offline

  • D.I.C Head

Reputation: 31
  • View blog
  • Posts: 109
  • Joined: 26-September 09

multiple return statements in short methods?

Posted 09 July 2011 - 02:50 AM

Ok, so I am doing a group project and one of the team members has created a pile of code with a lot of short methods with multiple returns like this:
public static int doSomething(Object object) {
    try {
        return otherObject.add(object);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return -1;
}


A couple of these ones I have changed to read as follows:
public static int doSomething(Object object) {
    int result = -1; // default 'fail' value
    try {
        result = otherObject.add(object);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}


I just prefer single points of return - but is this being a bit anal? And for a short method is it really worth it? More to the point, if it was returning a SortedSet is it worth the hassle of creating a temp SortedSet just to have a return value? I know there is no right or wrong answer here, but I want a feel from seasoned programmers if its worth refactoring these methods for the sake of readability (this code will be judged by professionals when its done, which is part of my concern as well).

Cheers

Steve

This post has been edited by kiwi_steve: 09 July 2011 - 02:51 AM


Is This A Good Question/Topic? 0
  • +

Replies To: multiple return statements in short methods?

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2194
  • View blog
  • Posts: 5,222
  • Joined: 10-September 10

Re: multiple return statements in short methods?

Posted 09 July 2011 - 04:26 AM

Are the two versions always equivalent? Will your version always return -1 when the team version returns -1?

Other than that, your approach creates an unnecessary variable, result, which may be judged by professionals to be wasteful. You prefer the readability and clarity of your approach, but personal preferences, or opinions, will nearly equal the number of people involved. That's part of the challenge of working with a team. This particular challenge can be mitigated to some degree by a comprehensive guide like a coding standard, list of best practices, or "dos and don'ts," etc.

This post has been edited by GregBrannon: 09 July 2011 - 04:30 AM

Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10179
  • View blog
  • Posts: 37,584
  • Joined: 27-December 08

Re: multiple return statements in short methods?

Posted 09 July 2011 - 07:19 AM

I apply the criterion "does it work?" when looking at refactoring. Is it functional? Is it modular and reusable/can you integrate the component into your design easily? If you answer yes to both of those, then I wouldn't really refactor. Imo, that's being a little anal in terms of refactoring, no offense. You can refactor everything to death. The idea with programming is to make things simple and effective.

Hope this helps some. :)
Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5641
  • View blog
  • Posts: 12,359
  • Joined: 16-October 07

Re: multiple return statements in short methods?

Posted 09 July 2011 - 09:52 AM

There are times you need the value to exist outside the block:
void doSomething() {
	Connection conn = null;
	try {
		conn = getConnection();
	} catch (Connectionerror e) {
		e.printStackTrace();
	} finaly {
		if (conn!=null) { conn.close(); }
	}
}



Or, you just might need to work on the value outside the catch block. Here, since you need the variable anyway, your form would make sense. In the two you offered, I'd prefer the first. There are some who loath early exits from functions, in which case the second is required. It's more style, than anything else.

Honestly, I'm more concerned with th rest of the example. :P
// static?
// only main need ever be static.  There should be a damn good reason for static.
public static int doSomething(Object object) {
	try {
		// I'm calling the add method of otherObject?
		// why am I a wrapper method?
		// again, why the hell a static wrapper method?
		return otherObject.add(object);
		// catch Exception?  what exception?
		// catching all of them is a bad thing to do
		// you intentionally want to hide all errors in your code?
	} catch (Exception e) {
		e.printStackTrace();
	}
	return -1;
}



You may say it's only an example. But if it's a representative example, then it's far from "professional."
Was This Post Helpful? 0
  • +
  • -

#5 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 561
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: multiple return statements in short methods?

Posted 09 July 2011 - 11:52 AM

Quote

- but is this being a bit anal?

My mind is so immature!!!! :bigsmile:

I personally use a double exit strategy, with the correct valur being returned within the block, and a null or incorrect value being expressed through a returned -1. It all depends what you are doing in these methods though, as you may want two returns sometimes. For instance, a lot of search methods use a double return, to represent whether a value has been found (return value) or not (return -1).
Was This Post Helpful? 0
  • +
  • -

#6 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7291
  • View blog
  • Posts: 12,093
  • Joined: 19-March 11

Re: multiple return statements in short methods?

Posted 09 July 2011 - 12:24 PM

Quote

I just prefer single points of return - but is this being a bit anal?


Yes, in my opinion. If it's really important to you, you need to get together with your team and make a case for your preferred style, and put it in your group style guidelines, along with line length and brace style and other trivia.

But really, I'd probably work on the whole "acceptance" thing. It's going to be okay, I promise.

(If you get any methods that are long enough that you can lose the points of return, feel free to decompose them into smaller methods)
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: multiple return statements in short methods?

Posted 09 July 2011 - 12:39 PM

Question of taste
I know shops were it is a coding convention to have only a single return at the end of a method

I have seen horrible code there whit if imbricated up to 15 levels to respect that "coding convention" despite a a few simple return well placed would have avoid that horror.

Question of taste and abuse is bad in all rirection
Was This Post Helpful? 0
  • +
  • -

#8 kiwi_steve  Icon User is offline

  • D.I.C Head

Reputation: 31
  • View blog
  • Posts: 109
  • Joined: 26-September 09

Re: multiple return statements in short methods?

Posted 10 July 2011 - 06:53 AM

ok, I'll address a few things without massive quoting...

Yes, it works identically to the original code, I would not have changed it otherwise...

Yes, its an almost pointless example, but the method is required regardless (and is slightly more complex than shown). And no, I generalised the exception - its actually an SQLException in real life.

Yes, we have an unwritten coding style guideline which states single point of return (but this should and will be mixed with common sense), but no-one has enforced it in the interests of getting it done. Yes, I've made my case to some of the team... and no-one else seems overly concerned with it (and on this basis alone, I may just drop the subject).

Its good to get some feedback, and I appreciate it. I will just leave it alone for now :)

Thanks guys
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1