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 7075175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:08:46+00:00 2026-05-28T06:08:46+00:00

First some background: I already have a Result class that I’ve implemented to carry

  • 0

First some background:

I already have a Result class that I’ve implemented to carry result information. They were implemented particularly for failures, a success result generally carries no additional information. I use them in public methods at the outside of a core API in one of my projects, or in methods one or two layers deep where I need to carry detailed information back up to the public API. This API is consumed in-container and a command line client and was previously very checked exception happy. The alternative was adding this failure context information to the exceptions, and adding several more distinct exception classes, where they just didn’t belong. I guess this reflects my general approach:

http://blogs.atlassian.com/2011/05/exceptions_are_bad

Result has a supporting enum and interface:

  1. ResultStatus: SUCCESS, FAILURE, CONDITIONAL_SUCCESS etc. Holds int return codes and generic messages (eg “Operation was successful”)
  2. ResultCode: Empty interface for tagging enum result codes, FILE_NOT_FOUND, FILE_INVALID, FILE_INCOMPATIBLE. These are mostly for unit and functional testing, but are also used for online help (Maven’s help on failure is functionally similar to what I’m doing)

Result provides static factory methods for the status, and builder methods to set the other key fields and is immutable. Here’s what building a result looks like:

Result.success().withCode( ResultCode ).withMessage( "" );

And evaluating the returned Result:

result.isSuccessful();
result.hasCode( ResultCode );
result.getMessage();

My problem:

The approach has been very successful, particularly making functional tests a breeze, however I have one major gap: while in the majority of cases I only care about the Result, in some critical areas I need to return a value with the result.

I’m not happy with my first attempt: ResultPair<T> which contains the result and the value where T is the value’s type. Unless I duplicate all of the Result methods, using the class is verbose and clumsy: get a Result, add it to a ResultPair, and fish the result and value out before evaluating. At first glance, inheritance won’t work because of the builder pattern and I can’t quite think of a way of getting clear, clean code that Result allows without duplicating the Result class completely.

Ideally, the approach would allow Result to return a value optionally, but I can’t think of a way of doing it in a type safe way that ensures method signatures clearly indicate the type of the value, but avoids having a meaningless generic parameter everywhere I don’t return a value.

I really don’t mind having two classes and it’s not the end of the world to duplicate the Result builder and evaluation code, it just feels wrong and I feel like a lack of experience is getting the better of me and I’m missing an (obvious or not so obvious) solution. I’m using Guava and I’ll want to disallow null values in a friendly way, so may additionally wrap all values in a Optional, but I’d still need generic parameters (and compose it into the class to avoid result.value().get()… Okay, I’m thinking out loud now. I should ask a question…).

Is there a way of meeting these apparently mutually exclusive requirements?

  • 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-28T06:08:46+00:00Added an answer on May 28, 2026 at 6:08 am

    You could create your ResultPair<T> as a base class to the void result class:

    public class Result extends ResultPair<Void> { ... }
    

    All relevant methods would be declared in ResultPair<T>.

    UPDATE:

    Better yet, have just one type Result<T>, and use Result<Void> whenever you don’t want to have a return value. Or rather, if using Void as a type argument looks weird to you, create a new uninstantiatable (or singleton) type that means “there’s no result”, and use it instead: Result<Unit>, for example.

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

Sidebar

Related Questions

First some background: VB.NET 2005 Application that accesses a MS-SQL back-end, using multiple Web
As usual, some background information first: Database A (Access database) - Holds a table
Background information first: We are using a strongly-typed DataSet to manage transaction files. That
First some background. I have a file, for simplicity's sake, let's call it test.txt.
First some background. I inherited a project where the build file was already written
First some background: I'm working on an application and I'm trying to follow MVVM
First, some background: I'm developing a web application using Python. All of my (text)
i am a newbie on web development, so i have some problems first of
When I need some complex algorithm I first check if there's anything relevant already
First some background. I'm parsing a simple file format, and wish to re-use the

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.