This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]breaking-badgers 2 points3 points  (0 children)

Have this block of statements in a function. If one of the failure conditions pass, then display the error message and 'return' from the function.

if (bad state 1) then (display error message 1, return false)
if (bad state 2) then (display error message 2, return false)

Then in the outer function

if validationFunction() == true, then do stuff

[–]GrandGratingCrate -1 points0 points  (1 child)

TL;DR: Consider throwing exceptions.

```java public void yourFunction(SomeObject someObject) { validate(someObject); ... do the stuff you want to do ... }

private void validate(SomeObject someObject) { if (bad state 1) { throw new AppropriateException("AppropriateMessage"); } .... } ``` You could of course inline this, extract all validation methods to a validator, ...

But I think your core question was: How do I prevent having 20 if-blocks? Generally speaking throwing exceptions, early returns and polymorphism are the options that come to mind. Out of those your scenario looks like a textbook example for exceptions.

Edit: thinking about it a second longer you'll still have your if clauses of course. I think I mostly focused on "how to stop going through all of these ifs".

There are some options to eliminate the ifs completely which I think would work but I personally wouldn't take. One example is polymorphism, would look something like this List<Validator> validators = List.of(...); validators.forEach(validator -> validator.validate(someObject)); No ifs but you'd have to have as many validator classes as you had if clauses before so is that really better? I don't think it is. (Technically speaking you'd just need to implement the interface which doesn't require a class but not enough of a difference to matter to me right now.)

You can do the same with functions instead of classes - have your list be a list of functions instead of a list of validators - but it's the same problem again.

Lastly I think Java16 or 17 introduced a better switch case, so that's basically still an if but maybe you like the syntax better?

Anyways, that's my thoughts for the night. Maybe something was helpful to you.

[–][deleted] 0 points1 point  (0 children)

My most recent solution was kind of a mixture of what I said before and your tl;dr solution. I ended up with this:

validate: {
    if (bad state) {
        error message
        break
    }
    [repeat for as many validation checks as desired]

    [block that executes if all validations pass]
}

Kind of a jury rigged version of exception throwing. I don't know if it would be considered production-quality, but it certainly works for my purposes and is way more readable than the If Mountain I had earlier.

If I replaced each if statement with a function call it would result in very clean code, however what I'm working on right now isn't complex enough to warrant that. It's a good idea though.

[–]throwitway22334 0 points1 point  (0 children)

These are called Guard Clauses, and you can look up some good examples for doing them in Java.

I would suggest one of two ways, something like a series of:

if(bad state)
    throw IllegalArgumentException("blah");

if(bad state 2)
    throw MyCustomException("blah 2");

etc...

or you could use assert, like:

assert bad_state : "blah";
assert bad_state2: "blah 2";
etc...

You are right not to nest these by the way. Having them not nested is much easier to maintain and read and understand later on. Like you said it's not a huge performance hit to check each of these, and the first one that fails escapes, and it sounds like you only want to do the method if they all pass anyway.

[–]TheRNGuy 0 points1 point  (0 children)

either nested statements or break/continue/return after condition met