0

I'm having an issue where my code just feels messy and I need some help on how I could structure it better.

example:

if (object.getDescription() == Status.Expected && !logEvent.equals("Expected")) {
    System.out.println("Do nothing"); // ???
} else {
    status.setChangedBy(logEvent);
}

How can i format this if in a cleaner way? I want the changedBy method to be called in every case except when getDescription == Status.Expected and logEvent is not "Expected". But I don't want an empty if statement either.

An alternative is:

if (object.getDescription() == Status.Expected) {
   if (logEvent.equals("Expected")) {
         status.setChangedBy(logEvent);
   }
} else {
    status.setChangedBy(logEvent);
}

Both examples work. But neither examples "feels right". Is there any other solution I'm not seeing?

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
robinmanz
  • 361
  • 1
  • 5
  • 17
  • 4
    If you have an `if-else` with an empty `if` like in your first example you can simply flip the conditions in the `if` (think about what the condition of the `else` really is), so you just have an `if` with no `else`. – Nexevis Nov 03 '22 at 14:10
  • `if (!(object.getDescription() == Status.Expected && !logEvent.equals("Expected")))` – Scott Hunter Nov 03 '22 at 14:12
  • 1
    @ScottHunter Can be simplified. !(A && !B) == !A || B – Michael Nov 03 '22 at 14:17
  • @Michael: True, but the "unsimplified" might be easier to understand. – Scott Hunter Nov 03 '22 at 15:12

4 Answers4

6

Invert the condition by applying the ! operator to the whole thing. Then you can put the code in the if block instead of the else block.

if (!(object.getDescription() == Status.Expected && !logEvent.equals("Expected"))) {
    status.setChangedBy(logEvent);
}

You can further simplify this using De Morgan's Law:

if (object.getDescription() != Status.Expected || logEvent.equals("Expected")) {
    status.setChangedBy(logEvent);
}

You change the status when "the description is not expected or logEvent is expected"

Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • Or if you're lazy, just dump the expression into wolfram alpha and see what the the minimal form is. https://www.wolframalpha.com/input?i=%21%28A+AND+%21B%29 – Michael Nov 03 '22 at 14:21
  • That's some black magic right there. Thanks! – robinmanz Nov 04 '22 at 08:20
1

If I understand right, what you are looking for is that :

if (object.getDescription() != Status.Expected || logEvent.equals("Expected")) {
    status.setChangedBy(logEvent);
}

I took your original comparison and reversed it.

1

I know that it takes more code, but I would go for this approach:

if (shouldChangeStatus(object, logEvent))
    status.setChangedBy(logEvent);
}

private boolean shouldChangeStatus(Object object, Object logEvent) {
    if (object.getDescription() != Status.Expected) {
        return true;
    }
    if (logEvent.equals("Expected")) {
        return true;
    }
    return false;
}

You can simplify it to something like that

if (shouldChangeStatus(object, logEvent))
    status.setChangedBy(logEvent);
}


private boolean shouldChangeStatus(Object object, Object logEvent) {
    if (object.getDescription() != Status.Expected) {
        return true;
    }
    return logEvent.equals("Expected");
}

It's the easiest way to maintain that code, also if you ever need that check in other place you can just change private to public/protected or whatever and just use it without a need to copy paste code

Morph21
  • 1,111
  • 6
  • 18
0

Minor addition to the previous answers: it's good practice to put the constant string first in a comparison like this "Expected".equals(logEvent). This way if logEvent is null, it will not throw a NPE.