-3

So, let's say I have a method:

public Object getResult(Long A, Long B){...}

And I want to check all possible states, so I've done:

if (A == null && B == null){
    return all objects;
}
if else (A != null && B == null){
    return objects by A;
}
if else (A == null && B != null){
    return single object by B;
}
if else (A != null && B != null){
    throw Exception;
}

I've been looking for a better solution, but found nothing. Any clues to make it actually look better? Maybe a switch? Or nested IFs?

closed as unclear what you're asking by gnat, Bart van Ingen Schenau, Jörg W Mittag, Greg Burghardt, BobDalgleish Jan 8 at 17:55

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    Possible duplicate of Approaches to checking multiple conditions? – gnat Jan 8 at 7:52
  • 1
    Smells like an XY problem to me. If you wanted to test all possible states, you'd be performing 2^64 * 2^64 * 4 checks, not just 4. – Neil Jan 8 at 7:57
  • I'd say it's a lil over-interpretation since a parameter can be null or not null, so 2 parameters with 2 states each should give 4 possible outcomes. The question is whether to use if else, switch or any other structure to check it. – Maciaz Jan 8 at 8:00
  • What happens if the value of the parameter changes inside the funtion? – Pieter B Jan 8 at 8:25
  • Nothing, it doesn't matter. What matters is whether it's null or not. – Maciaz Jan 8 at 8:27
0

What's missing to answer this question is the usage of the method: whether the callers usually pass constants or not.  If the callers usually invoke via getResults(null, null), getResult(a, null) and/or getResult(null, b) then we don't really need multiple parameters, and you should have multiple methods instead of a single method.

Even if callers occasionally do getResult(a,b), but more often one of the prior forms passing null, you should have multiple methods.

In my opinion, the ease and simplicity for the consuming client programmer — even if just you — is the primary consideration (vs. the underlying implementation).  Teasing these invocations apart would make the consuming client easier to understand, easier for the IDE to isolate the different use cases, easier to refactor, etc...  It will also eliminate those conditionals in those cases of passing nulls.

If these are separate use cases from the client's perspective (as per passing null as constants), then there is no reason to conflate the cases in the first place (e.g. into a god method).

0

Assuming you want to keep this behavior in a single method, here is a slight simplification, that additionally checks the exceptional condition first:

if (A!=null && B!=null) {
    throw Exception;
}
if (A == null) {
    return single object by B;
}
if (B == null) {
    return objects by A;
}
return all objects;
0

This is a logic problem.

A and B could each be in one of two states == null or != null.

There is an excluded middle here, A or B cannot individually be both == null and != null, but they also cannot be neither == null nor != null. So if we know A is say == null then we also know that A is not != null.

if encapsulates this with the else. if (A == Null) { /*A is null*/ } else { /*A is not null*/ }.

We can use this to extract a check, once its been established we can proceed with further checks knowing it to be one way, or the other.

if (A == null){
  //A is null, B is ?
  if (B == null)
  {
       //A is null, and B is null
       ...
  }
  else
  {
       //A is null, and B is not null
       ...
  }
}else{
  //A is not null, B is ?
  if (B == null)
  {
       //A is not null, and B is null
       ...
  }
  else
  {
       //A is not null, and B is not null
       ...
  }
}
  • 1
    Can you explain why your code solves the original problem? How is it more effective, efficient or readable than the original proposal? – BobDalgleish Jan 8 at 17:50

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