• 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

return -1. why ???

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear friends:
In while using If..else statements......if the any condition fails we are returining the statment like "return false" but in some case i have seen that peoples are using "return -1".
Please help me..
Thanks in advance.
Best Regards
K S Saravanan
 
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by saravanan ks:
Dear friends:
In while using If..else statements......if the any condition fails we are returining the statment like "return false" but in some case i have seen that peoples are using "return -1".
Please help me..
Thanks in advance.
Best Regards
K S Saravanan


Hi saravanan, and welcome to the ranch. I guess the answer to your question depends on the return type of the method in which the if-stat is located in (if it is of type int or boolean).
If you can post the question then maybe we'll be able to help more.
 
saravanan ks
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Vicken Karaoghlanian :
Thanks for immediatate response,
In the methods, return type is like this..

Thanks
K S Saravanan
[ December 30, 2003: Message edited by: Michael Ernest ]
 
High Plains Drifter
Posts: 7289
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A return of -1 is consistent with a failed system call, or in the case of Java, failure in receiving response from a stream which operates on serialized values. Since all byte streams are interpreted initially as integer values, -1 is used to express an improperly functioning or non-function one.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Returning "magic numbers" to indicate failure is a debatable practice. Sun used -1 in many places, but not everywhere. Sometimes any integer would be a valid result, so a special integer for error cannot work and they throw an exception. You also get into the whole question of what's a failure and what's normal operation. Reading from a stream returns -1 at end because that's normal ... every stream eventually comes to an end. Reading from a stream that's not open gets an exception because that's a serious error.
In your example it might be appropriate (and much easier to read) to throw a variety of exceptions. See how this gets rid of a lot if if/else clauses:

This gets even cleaner if we throw the exceptions in called methods that do the details, like allocateMemory(). Then this code might look like:

Hope that made sense and gave you some ideas!
[ December 30, 2003: Message edited by: Stan James ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic