Two Laptop Bag*
The moose likes Associate Certification (SCJA,OCAJ 5/6) and the fly likes good coding practice? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Associate Certification (SCJA,OCAJ 5/6)
Bookmark "good coding practice?" Watch "good coding practice?" New topic
Author

good coding practice?

prasha prata
Greenhorn

Joined: Jul 15, 2010
Posts: 1
Passing objects to a method when you need to return more than one variable.

Is this a good coding practice?
Abhay Agarwal
Ranch Hand

Joined: Feb 29, 2008
Posts: 1067
    
    1

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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30050
    
149

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]


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Gaurav Raje
Ranch Hand

Joined: Jul 23, 2010
Posts: 136
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

Joined: Aug 26, 2006
Posts: 4968
    
    1

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: good coding practice?
 
Similar Threads
Is it not good?
null pointer exception
Coding
labels in java gui components
why there is no argument thread constructor?