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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:20:16+00:00 2026-05-27T10:20:16+00:00

I have a history in programming, but not much in software development. I’m currently

  • 0

I have a history in programming, but not much in software development. I’m currently writing a piece of software for the company I work at, and I’ve come to challenge myself on the readability of my code.

I want to know whether this is a “valid” alternative to embedded if statements, or if there is anything better I could use.

Let’s say I have the following method:

public void someMethod()
{
    if (some condition)
    {
        if (some condition 2)
        {
            if (some condition 3)
            {
                // ...etc all the way until:
                doSomething();
            }
            else
            {
                System.err.println("Specific Condition 3 Error");
            }
        }
        else
        {
            System.err.println("Specific Condition 2 Error");
        }
    }
    else
    {
        System.err.println("Specific Condition 1 Error");
    }
}

Now the first thing I should point out is that in this instance, combining the conditions (with &&) isn’t possible, since each one has a unique error that I want to report, and if I combined them I wouldn’t be able to do that (or would I?). The second thing I should point out before anyone screams “SWITCH STATEMENT!” at me is that not all of these conditions can be handled by a switch statement; some are Object specific method calls, some are integer comparisons, etc.

That said, is the following a valid way of making the above code more readable, or is there a better way of doing it?

public void someMethod()
{
    if (!some condition)
    {
        System.err.println("Specific Condition 1 Error");
        return;
    }

    if (!some condition 2)
    {
        System.err.println("Specific Condition 2 Error");
        return;
    }

    if (!some condition 3)
    {
        System.err.println("Specific Condition 3 Error");
        return;
    }

    doSomething();
}

So basically, instead of checking for conditions and reporting errors in else blocks, we check for the inverse of the condition and return if it is true. The result should be the same, but is there a better way of handling this?

  • 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-27T10:20:17+00:00Added an answer on May 27, 2026 at 10:20 am

    If I was being particularly pedantic I would use something like this.

    boolean c1, c2, c3;
    
    public void someMethod() {
      boolean ok = true;
      String err = "";
    
      if (ok && !(ok &= c1)) {
        err = "Specific Condition 1 Error";
      }
    
      if (ok && !(ok &= c2)) {
        err = "Specific Condition 2 Error";
      }
    
      if (ok && !(ok &= c3)) {
        err = "Specific Condition 3 Error";
      }
    
      if ( ok ) {
        doSomething();
      } else {
        System.out.print(err);
      }
    }
    

    You are now single-exit AND flat.

    Added

    If &= is difficult for you, use something like:

      if (ok && !c3) {
        err = "Specific Condition 3 Error";
        ok = false;
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First, we currently have the behavior that's desired, but it's not trivial to maintain
Rails is a great platform, but it just doesn't have the history that Perl
I'm new to OS X programming but generally liking it. I have the following
I know this is the most common problem in the history of programming but
I have multiple models that need to have their history kept pretty much indefinitely
I just start using Mercurial yesterday (I don't have much programming experiences). I noticed,
I have a History Table in SQL Server that basically tracks an item through
In history-books you often have timeline, where events and periods are marked on a
We have a SharePoint list setup with history enabled so the Comments field keeps
I have 2 tables product and history product table: id name type price location

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.