2
\$\begingroup\$

I have two string variables site_inclusion and site_exclusion. If site_inclusion has some values, then I don't care what values site_exclusion contains. This means site_inclusion overrides site_exclusion if site_inclusion has some values. But if site_inclusion is null and site_exclusion has some values, then go into the site_exclusion loop.

Requirements:

  1. If site_inclusion and site_exclusion both are null then set useTheSynthesizer as true;
  2. If site_inclusion is not null and it matches with the regexPattern then set useTheSynthesizer as true. And I don't care what values are there in site_exclusion.
  3. If site_inclusion is null and site_exclusion is not null and site_exclusion does not match the regexPattern, then set useTheSynthesizer to true.

I wrote the below code but somehow I think I am repeating some stuff here in the if/else loop. Any code improvements that fulfill my conditions will be appreciated.

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;
}
\$\endgroup\$
  • \$\begingroup\$ Your question on SO should probably be merged into this one. \$\endgroup\$ – Kevin K Jun 7 '12 at 22:10
2
\$\begingroup\$

Please spinning it to a separate function. Here is what I think is a little more simpler implementation.

public boolean checkInclusion(String site_i, String site_e, Regex re) {
    if (site_i != null)
        return site_e.matches(re);
    else if(site_e != null)
        return !site_e.matches(re)))
    else
        return true;
}

In your implementation, what happens when the site inclusion has values but does not match, and site_exclusion is null? It seems to not match any of the conditions, which is not in sync with the requirements from my reading.

Also what happens when the site inclusion has values but does not match, and site_exclusion has values that does not match? In your implementation, it seems like site exclusion takes precedence. But that does not seem to be the requirement.

Perhaps I am reading either requirement wrongly or your implementation conditions wrongly?

A rather terse implementation that is same as above is,

public boolean checkInclusion(String site_i, String site_e, Regex re) {
    if (site_i != null) return site_i.matches(re);
    return site_e == null || !site_e.matches(re)
}
\$\endgroup\$
  • \$\begingroup\$ Could you explain how OR works on return statement ? \$\endgroup\$ – latusaki Jun 10 '12 at 21:54
  • \$\begingroup\$ It is just a return of a boolean value. So first site_e == null is evaluated. If it is true, then true is returned. If not, site_e.matches(re) is evaluated and its negation is returned. \$\endgroup\$ – rahul Jun 11 '12 at 2:38
  • \$\begingroup\$ The statement is same as [ bool x = site_e == null || !site_e.matches(re); return x; ] \$\endgroup\$ – rahul Jun 11 '12 at 2:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.