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
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.
- Do not try and bend the spoon. That's impossible. Instead, only try to realize the truth. <br />- What truth? <br />- That there is no spoon!!!
Joined: Jul 10, 2003
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 ]
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.
Make visible what, without you, might perhaps never have been seen. - Robert Bresson
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 ]
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi