5

I'm writing a function that takes a request object, there are several ways that the request could be configured, but only one valid form, so before I perform my function I'd like to test how the object is configured. If I were doing this in java, I'd write something like this:

static void func(Request request) {
  if (condition1)
    return false

  if (condition 2)
    return false

  if (condition 3)
    return false

  request.process()
  return true
}

In scala though, in trying to avoid using return, I usually end up with this:

static void func(Request request) {
    if (!condition1) {
        if (!condition 2) {
            if (!condition 3) {
                request.process()
                return true
            }
            else
                return false
        }
        else
            return false
    }
    else
      return false
}

This is a simplified form as usually there are things that I need to do after condition 1, and then after condition 2, which mean that I can't just combine the conditions into a single if statement (e.g. condition 1 checks that something isn't null, then condition 2 checks if the value of that thing is valid).

What I'm wondering is how do I make my code clean and readable whilst still having these checks in place?

2
  • 2
    what do you mean by " in trying to avoid using return"? I see there are return statements in the second example also. Also, are your conditions involve some common variable by any chance? Commented May 2, 2019 at 11:21
  • You may want to compose your conditions (using something like Validated). However, if you are not familiar with FP, and terms like Applicative or Monad, you may need to study those things first. Commented May 2, 2019 at 11:53

3 Answers 3

4

you can use pattern matching

request match {
    case _ if (condition1 || condition2 || condition3) => false
    case _ => request.process(); true
}
Sign up to request clarification or add additional context in comments.

3 Comments

He has stated as part of the question that he can't combine the conditions as he need to handle some specific logic per condition.
In that case , he has to add 2 more case statement. I think that is not a big deal.
I could add the extra cases, but suppose the logic to get condition2 is like 10-20 lines of code. Is a case statement still visually the best way to represent that? I could put the manipulation code into a function and then just call that from the case, but that will hide functionality (which could be good or bad).
4

In Scala it is usual to return Option[value] rather than returning true/false and relying on side effects. And a Request object would normally be pure data, with processing done by an external function.

So a typical Scala function might look like this:

def func(req: Request): Option[Result] =
  req match {
    case Request(a, _, _) if condition1(a) => None
    case Request(_, b, _) if condition2(b) => None
    case _ =>
      Some(processRequest(req))
  }

3 Comments

Maybe worth exploring Either[X,Y] to return a valid result OR an error message
I would vote for Try rather than Either for error handling, but both are good options if you want to preserve the reason for failure.
The only reason I used true/false than an option was to illustrate a reponse. The actual code I've adapted returns a monix task, as it's all async.
2

You can just do a simple if else simplifying your conditions.

def func(request: Request) {
    if (condition1 || condition2 || condition3)
        false
    else {
        request.process()
        true
    }
}

2 Comments

As stated in the question, I can't combine the conditions together in my case. For example suppose I need to check condition1 before I check condition2, because condition2 cannot be valid before checking that condition1 is valid.
The || operator will make sure that checks are done in order, and will skip later conditions if earlier conditions return true.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.