• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Having Object as a return type a good or bad practice

 
Ranch Hand
Posts: 91
IntelliJ IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

This is more of a design question. I have a function which has Object as the return type. Based on the outcome of the function, I will return either my custom object or I will return an exception, so that I will be able to guide to code accordingly. E.G. In the below code, the function foo is calling myCustomFunction which is returning an Object. Now based on the Object's instance I am deciding the subsequent actions. Is the below design a good, bad, or depends on the situation, but try to avoid kind?

 
Bartender
Posts: 7645
178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would say that's bad design. Exceptions are meant to be thrown - why return them as objects? If myCustomFunction can't handle exceptions, why is it catching them? Let foo handle them.
 
Vineeth Menon
Ranch Hand
Posts: 91
IntelliJ IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Tim,

Thanks for your reply. On a side note, if I were to return the object myCustomObject or myCustomObject2 based on some condition inside myCustomFunction, would that be a bad design as well? My main concern is that at a later point of time the whoever maintains the code needs to have strict standards, else she/he can ideally return any object from myCustomFunction and that sounds like danger to me.
 
Rancher
Posts: 4801
50
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And you shouldn't be flinging NullPointerExceptions around.
You should be preventing them, not catching them.
 
Tim Moores
Bartender
Posts: 7645
178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

myCustomObject or myCustomObject2


Can they be made to implement some interface? Then that could be the return type.
 
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Isn't your if-else structure already mimicking a multi try catch block ?


Why do you need instance-of if there is already a mechanism to handle multiple exceptions ? Also if your method declares that it throws an exception, it's self documenting as opposed to it returning either some object or an exception.

Next, if we want to talk about returning Object, let's discuss with some real examples instead of "myCustomFunction" and "foo". Some APIs e.g. Hibernate have methods which return Object or List<Object>. After the use of generics, many such methods are no longer needed.
 
Sheriff
Posts: 22821
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Moores wrote:I would say that's bad design. Exceptions are meant to be thrown - why return them as objects?


There is just one reason to return exceptions, and that is when using factory methods. That can be particularly useful if there is some logic needed to create the exception (or its message / arguments). Most of the time, the first thing you do with the result is throw it. For example:
 
There are no more "hours", it's centi-days. They say it's better, but this tiny ad says it's stupid:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic