2

I have the following endpoint with a nested (if) else if statement

@Patch("/games/:id")
  @HttpCode(200)
  async updateGame(@Param("id") id: number, @Body() update: Partial<Game>) {

    const updatedGame = await Game.findOne(id);
    if (!updatedGame) {
      throw new NotFoundError("HTTP 404 Not Found: No Games Here");
    } else if (update.color && !validColor(update.color)) {
      throw new BadRequestError("HTTP 400 Bad Request: No Such Color");
    } else if (
      update.board !== undefined &&
      moves(update.board, updatedGame.board) > 1
    ) {
      throw new BadRequestError(
        "HTTP 400 Bad Request:  Only one move allowed. Wait your turn"
      );
    } else {
      console.log("Game has been updated");
      return Game.merge(updatedGame, update).save();
    }
  }

And I would like to convert it to a switch statement for readability I've tried several attempts but somehow the values of the constant variables aren't being read.

the following is my switch statement attempt to replace the else if list

switch(updatedGame){
  case (!updatedGame):
  throw new NotFoundError("HTTP 404 Not Found: No Games Here")
  case(update.color && !validColor(update.color)):
  throw new BadRequestError("HTTP 400 Bad Request: No Such Color")
  case(update.board !== undefined && moves(update.board, updatedGame.board) > 1):
  throw new BadRequestError("HTTP 400 Bad Request:  Only one move allowed. Wait your turn")
  default : console.log("Game has been updated");
   Game.merge(updatedGame, update).save();


}

the if else if list work as it should but the switch statement gives errors

7
  • 1
    btw no else needed after throwing an exception ... Commented Jul 7, 2018 at 19:22
  • Why do you really need to change? ...and of course it errors, as the argument in your switch is what the case evaluates, and how can it with e.g. ´case(update.color && !validColor(update.color))´ when neither is related to updateGame? Commented Jul 7, 2018 at 19:23
  • That doesn't make any sense as a switch, they're not all taking the same result in the conditional. Commented Jul 7, 2018 at 19:25
  • @NinaScholz If I don't then the values provided aren't being updated in the database Game.merge(updatedGame, update).save Commented Jul 7, 2018 at 19:26
  • @LGSon, mainly for readability. I find switch statements very readable but I suck at writing them. Commented Jul 7, 2018 at 19:27

1 Answer 1

3

For better readability, you could omit the else parts, because a thrown exception, which is not caught inside of the function ends the function.

if (!updatedGame) {
    throw new NotFoundError("HTTP 404 Not Found: No Games Here");
}
if (update.color && !validColor(update.color)) {
    throw new BadRequestError("HTTP 400 Bad Request: No Such Color");
}
if (update.board !== undefined && moves(update.board, updatedGame.board) > 1) {
    throw new BadRequestError("HTTP 400 Bad Request:  Only one move allowed. Wait your turn");
}
console.log("Game has been updated");
return Game.merge(updatedGame, update).save();
Sign up to request clarification or add additional context in comments.

3 Comments

How does this improve readability? Please explain. Code given with if..else in question is more readable then this.
it is the early exit paradigm with single if. after an exit by exception or simply a return statement, nothing more happens.
Ah, okay, that makes sense! I somehow figured a switch statement would be cleaner. I guess that wouldn't be best practice Your code makes more sense and looks a lot cleaner! Thanks!

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.