My Question is-
I have two string variables site_inclusion and site_exclusion. If site_inclusion has a value, then I don’t care what values site_exclusion contains. That is to say that site_inclusion overrides site_exclusion. If, however, site_inclusion is null and site_exclusion has a value, then I want to examine site_exclusion.
To be more precise:
- If
site_inclusionandsite_exclusionare bothnullthen setuseTheSynthesizerastrue; - If
site_inclusionis notnulland it matches with theregexPatternthen setuseTheSynthesizerastrue. And I don’t care what values are there insite_exclusion. - If
site_inclusionisnullandsite_exclusionis notnullandsite_exclusiondoes not match theregexPatternthen setuseTheSynthesizerto true.
I wrote the below code but somehow I think, I am repeating some stuff here in the if/else loop. Any code improvements will be appreciated that fulfill my conditions.
String site_inclusion = metadata.getSiteInclusion();
String site_exclusion = metadata.getSiteExclusion();
// fix for redundant data per site issue
if(site_inclusion != null && site_inclusion.matches(regexPattern)) {
useTheSynthesizer = true;
} else if(site_exclusion != null && !(site_exclusion.matches(regexPattern))) {
useTheSynthesizer = true;
} else if(site_inclusion == null && site_exclusion == null ) {
useTheSynthesizer = true;
}
nulltest.if(test == true) flag = truestatement. You can simply sayflag = test.My recommendation would be:
You could also do it in a oneliner:
But I find that sort of obnoxious to read.
(Note, I made the assumption that
useTheSynthesizerwas otherwisefalse. This isn’t explicit in your code or explanation, but I think this assumption was safe.)