Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

return -1. why ???

 
saravanan ks
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • 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
 
Vicken Karaoghlanian
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
  • 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
  • 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 ]
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • 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.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • 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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic