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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:19:38+00:00 2026-05-26T02:19:38+00:00

I use PMD to check my code. It gives me very useful hints in

  • 0

I use PMD to check my code. It gives me very useful hints in most cases, but I can’t figure out what could be improved in the following scenario.

The original code looks something like this:

if ((getSomething() != null && getSomethingElse() != null)
     || (getSomething() == null && getSomethingElse() == null))
{
   ...
}

PMD tells me:

Sometimes two ‘if’ statements can be consolidated by separating their
conditions with a boolean short-circuit operator.

For simplicity, let’s just use a and b as boolean variables. Then this piece of code looks like this:

if ((!a && !b) || (a && b))

This can be transformed to one of the following:

if ((!a || b) && (a || !b))
if (!(a^b))

and finally

if (a==b)

So I simplified my code to

if ((getSomething() == null) == (getSomethingElse() == null))

However, PMD keeps complaining (in fact about all three versions). Is this a false positive or is there a better way of writing the if-condition?

  • 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-26T02:19:39+00:00Added an answer on May 26, 2026 at 2:19 am

    The problem was something different. The if-statement was the only code inside another if (the code comes from a validation-method):

    if (...)
    {
       ...
    }
    else if (...)
    {
       ...
    }
    else if (...)
    {
       if ((getSomething() == null) == (getSomethingElse() == null))
       {
          ...
       }
    }
    

    What the PMD-message means, is that I could combine the conditions of the last else-if and the inner if-clause:

    if (...)
    {
       ...
    }
    else if (...)
    {
       ...
    }
    else if (... && ((getSomething() == null) == (getSomethingElse() == null)))
    {
          ...
    }
    

    However, I’m not sure, if I’ll do this, because the original version seems much clearer to understand.

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

Sidebar

Related Questions

With PMD, if you want to ignore a specific warning, you can use //
use this website a lot but first time posting. My program creates a number
I'd like to use a custom PMD ruleset file for my build. Basically, I
I do use static code analysis on a project with more than 100.000 lines
use WWW::Mechanize; my $mech = WWW::Mechanize->new; $mech->get( $url ); say $mech->text; How could I
Use case example: Client A comes to request sales information, enters their zip code
What code analysis tools do you use on your Java projects? I am interested
I need to use System.exit(0) in an application. Eclipse has the PMD plugin installed
I have taken a look at the PMD api which I want to use
use grep command to find txt files containing telephone number. telephone number format could

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.