Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7007719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:38:37+00:00 2026-05-27T21:38:37+00:00

I was thinking about patterns which allow me to return both computation result and

  • 0

I was thinking about patterns which allow me to return both computation result and status:

There are few approaches which I could think about:

  • function returns computation result, status is being returned via out parameter (not all languages support out parameters and this seems wrong, since in general you don’t expect parameters to be modified).

  • function returns object/pair consisting both values (downside is that you have to create artificial class just to return function result or use pair which have no semantic meaning – you know which argument is which by it’s order).

  • if your status is just success/failure you can return computation value, and in case of error throw an exception (look like the best approach, but works only with success/failure scenario and shouldn’t be abused for controlling normal program flow).

  • function returns value, function arguments are delegates to onSuccess/onFailure procedures.

  • there is a (state-full) method class which have status field, and method returning computation results (I prefer having state-less/immutable objects).

Please, give me some hints on pros, cons and situations’ preconditions of using aforementioned approaches or show me other patterns which I could use (preferably with hints on preconditions when to use them).


EDIT:
Real-world example:
I am developing java ee internet application and I have a class resolving request parameters converting them from string to some business logic objects. Resolver is checking in db if object is being created or edited and then return to controller either new object or object fetched from db. Controller is taking action based on object status (new/editing) read from resolver. I know it’s bad and I would like to improve code design here.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T21:38:38+00:00Added an answer on May 27, 2026 at 9:38 pm

    function returns computation result, status is being returned via out
    parameter (not all languages support out parameters and this seems
    wrong, since in general you don’t expect parameters to be modified).

    If the language supports multiple output values, then the language clearly was made to support them. It would be a shame not to use them (unless there are strong opinions in that particular community against them – this could be the case for languages that try and do everything)

    function returns object/pair consisting both values (downside is that
    you have to create artificial class just to return function result or
    use pair which have no semantic meaning – you know which argument is
    which by it’s order).

    I don’t know about that downside. It seems to me that a record or class called MyMethodResult should have enough semantics by itself. You can always use such a class in an exception as well, if you are in an exceptional condition only of course. Creating some kind of array/union/pair would be less acceptable in my opinion: you would inevitably loose information somewhere.

    if your status is just success/failure you can return computation
    value, and in case of error throw an exception (look like the best
    approach, but works only with success/failure scenario and shouldn’t
    be abused for controlling normal program flow).

    No! This is the worst approach. Exceptions should be used for exactly that, exceptional circumstances. If not, they will halt debuggers, put colleagues on the wrong foot, harm performance, fill your logging system. If you create a method to test something, then the test should return a status, not an exception: to the implementation, returning a negative is not exceptional.

    Of course, if you run out of bytes from a file during parsing, sure, throw the exception, but don’t throw it if the input is incorrect and your method is called checkFile.

    function returns value, function arguments are delegates to
    onSuccess/onFailure procedures.

    I would only use those if you have multiple results to share. It’s way more complex than the class/record approach, and more difficult to maintain. I’ve used this approach to return multiple results while I don’t know if the results are ignored or not, or if the user wants to continue. In Java you would use a listener. This kind of operation is probably more accepted for functional languages.

    there is a (state-full) method class which have status field, and
    method returning computation results (I prefer having
    state-less/immutable objects).

    Yes, I prefer those too. There are producers of results and the results themselves. There is little need to combine the two and create a statefull object.

    In the end, you want to go to producer.produceFrom(x): Result in my opinion. This is either option 1 or 2a, if I’m counting correctly. And yes, for 2a, this means writing some extra code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Without thinking about it at all I just want to say I should allow
When thinking about traditional layered application design, I often think in terms of 3
I thinking about solution like this: different menus for users depend on role which
Thinking about getting into .net technology project management I've had plenty of experience with
Thinking about a Windows-hosted build process that will periodically drop files to disk to
Thinking about my other problem , i decided I can't even create a regular
Thinking about avoiding code replication, I got a question that catches me every time
Thinking about if we modify the definition of Hamiltonian path as we need to
Currently thinking about pitching the argument for us migration from vs 2005 (winforms) to
While thinking about this question and conversing with the participants, the idea came up

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.