nus-cs2113-AY2122S2 / forum

3 stars 2 forks source link

Is it absolutely necessary to include exceptions when it introduces way more LOC? #31

Closed BradenTeo closed 2 years ago

BradenTeo commented 2 years ago

I'm referring to Level-5 increment of the iP, where we are expected to use an exception to handle invalid inputs. image

In my code, I use a switch block to decide what to do with the inputs (e.g. list, mark, unmark). The default of the switch block will just print out a message similar to the one shown in the picture above. It is very short and I feel that modifying my code to include the exception would reduce the readability by a lot.

The website says that the minimal requirement is to handle the stated types of errors, but I'm hesitant to, and would thus like a second opinion on this. Thank you :)

shunyao643 commented 2 years ago

How about putting it within a method or as a string constant?

I think it's important to tell your user what's wrong with their input and how to correct it.

okkhoy commented 2 years ago

The exercise requires you to use exceptions. Not sure what you are referring to here?

BradenTeo commented 2 years ago

Sorry, I think it is easier to explain with some code. This code block is in my Main class:

while (!input.equals("bye")) {            //input refers to the user's input
     String[] arrOfInputStrings = input.split(" ");

     switch(arrOfInputStrings[0]) {
     case "list":
           displayList(tasks, taskCounter);
           break;
     //other cases
     default:
           invalid_input();
           break;
     }

     input = in.nextLine();
}

^So the invalid_input() would take care of invalid inputs that are not specific to the different commands. (I have done up exceptions for the other commands like list and todo).

I can't think of a nice way to implement exceptions that wouldn't bloat the code.

wli-linda commented 2 years ago

It seems like your invalid_input() method already corresponds to the second type of errors that should be handled? As for the first type of error-handling required for Level-5, which catches the case where no descriptions supplied for Todo commands, couldn't you just implement it within the "todo" case of your switch statement? And if you're worried about code quality, you can always consider refactoring your code by extracting a method from the "todo" case.

BradenTeo commented 2 years ago

Yep, that's what I did. Thanks for all your suggestions. I'll mark this thread as resolved now.