• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

good coding practice?

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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]
 
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author and cow tipper
Posts: 5009
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic