This week's book giveaway is in the OCP forum.
We're giving away four copies of OCP Java SE 8 Programmer II Exam Study Guide and have Kathy Sierra, Bert Bates, & Elizabeth Robson on-line!
See this thread for details.
Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How make return two things by one function?  RSS feed

 
Ranch Hand
Posts: 92
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want wrote my Queue class with function Pop getting element from queue. In Pascal declaration is:

function TIntQueue.Pop(PCanPop: PBoolean=nil): integer;


if parameter is null then is exception when queue is empty, if parameter not null - parameter set if is not empty.
But Java have not reference parameter nor pointers. How I can do function which default throw exception and can not throw.
To make it smartly I think about two functions: TryPop not throw exceptions, Pop call TryPop and call exceptions. TryPop should return two things: element and boolean.
 
Rancher
Posts: 42975
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not following. Why can't you write a method that throws an exception under some circumstances, and returns a result under other circumstances?
 
Andrzej Borucki
Ranch Hand
Posts: 92
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I probably write two methods: Pop() and TryPop(). TryPop not return exceptions, but must return two things:
- element
- boolean if TryPop successed or not
Similar for example may be function TryConvert - converting text to integer or real - must return two things - result and boolean.
 
Ulf Dittmer
Rancher
Posts: 42975
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I repeat my previous question. A method can return an object reference (which may be null), and also throw an exception if need be.
 
Andrzej Borucki
Ranch Hand
Posts: 92
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't want exception but boolean, for example simple loop:

while (TryPop())


In other side - exceptions can be more convenient, programmer must not remember check every time error code
Thanks, I write one function - with exceptions.
 
Ulf Dittmer
Rancher
Posts: 42975
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, if the method returns a boolean then it can't return some other object. You could pass in an object reference as parameter, and the method could manipulate the contents of that object as a way of returning a result.
 
Java Cowboy
Sheriff
Posts: 16083
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would design my queue class differently.

Instead of having a TryPop() method which should return a boolean and an element if it exists, I would make a method called for example isNotEmpty that just returns a boolean saying if there's anything on the queue or not. Then I'd use it like this:

You're making it difficult by trying to make a method that does too many things at once.
 
Andrzej Borucki
Ranch Hand
Posts: 92
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I now think, exceptions are better than error codes as was in C. Can check isNotEmpty in loop or try-catch if accidentally this do.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!