Monday, May 18, 2009

Two return values?

Hey ! I happened to think about the following possibility in Java and just wondered how good or bad is to do it - both return a value from a method and also modified the mutable contents of an object referred to by the reference passed as a parameter.

Team whoWonTheMatch(Team pCSK, Team pKKR) {

...
//some look up
...
pCSK.setWon(true);
...
return pKKR;
//btw the match wasn't a tie!

}

Now look at this ... can you get the point. I came across a code today - We wanted to build database queries dynamically. User can pass any condition by which he wants to limit the rows dynamically (we say he can pass filters to the query). This method (that let to this post) modified a StringBuilder passed to it by adding the where clauses and returned a Map that had key value pairs that held the name=value pairs to be filled in those where clauses. So 5 minutes in the code review the presentees were wondering "yeah...the method returns the Map as per user's preferences (his choices of filters) but where's the Query built"...it was already done in side the same method...did the user return two values?

Is this a good design. Am I missing something. Should we adapt to the above model. Should this be discussed at all. Correct me if I am wrong.

BTW, last night Kolkata Knight Riders (KKR) just stunned the Chennai Super Kings (CSK) by chasi ng 189 of the last ball.
http://content.cricinfo.com/ipl2009/content/story/404882.html

1 comment: