This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

good coding practice?

 
prasha prata
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Passing objects to a method when you need to return more than one variable.

Is this a good coding practice?
 
Abhay Agarwal
Ranch Hand
Posts: 1375
Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sort of yes.

foreg -



here, if later on, we want to return one more element from testMethod then we can modify ReturnObject class and testMethod
for eg


i would personally appreciate returning the Collection from a method rather than instant of "ReturnObject" class.
for eg


Using arrayList , we can completely do away with RetrunObject class.

Now for eg - if we need to return one more value from method then our code will be
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 33694
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch!

I think the return object is better than a collection of values as the meaning is in the code. With the collection approach, you have to know what the order is and enforce it manually. The return object approach is clear, more readable and gives gyou compile time safety.

[ edited to fix typos]
 
Gaurav Raje
Ranch Hand
Posts: 136
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i would say it depends..... On a number of factors. But most importantly, does the resulting object mean anything?
If you already have an object which has the two values, then it is fine.
Always remember, you are affecting readability by such a method. Side effects should be a biproduct, not the sole product as much as possible. The methods with sideeffects should exist for the sole purpose and should exist because thats how it is. I am sure most of the designs can be refactored to accomodate individual returns. But there are some places where objects are required as the others have shown.

Where to do what.
Ask yourself what seems to be the natural answer. Not the simpler, natural answer. Ask yourself what should this method return if i just read it aloud.
For eg,
assume your method name is getPoint(). Then it may return an integer or a Point class object. If your method reads set or adjust point. It may have reference variables.
This being said, you can find a lot of books OOAD which advocate the use of returns instead of side effect based reference variables. There are a few people who feel otherwise. Both have their advantages.

I personally believe most of the times, you can refactor your code to suit the no side effect rule. However, this might not be the case always.


This being said, if the method is returning a value, make sure it is not having too many side effects. The thumb rule says method objectives should be atomic. So if it is doing 1 thing, it should ideally not be doing another thing unless it is required and cannot be avoided.
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's really the only programming practice. After all, you can only return one thing from a method. If you need to return 'two things' or 'two variables' as you say, they need to be returned as an object. Whether that object is created in the method, or passed into the method, it all depends. There is nothing wrong with a method that takes no arguments but returns an object.

-Cameron McKenzie
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic