-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix bug where task commands work with room index instead of room numbers #115
Fix bug where task commands work with room index instead of room numbers #115
Conversation
a13a1c6
to
6c253cc
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good job and thank you for refactoring the messages! Can see that you tried to add some assertions in your code. However, if I am not mistaken, some of your assertions are used in cases when user input is invalid. In such cases, I am not sure if assertions are appropriate since assertions cause the application to crash. (Imagine keying in a room number of -5 and the app crashes ><)
|
||
if (roomIndex.getZeroBased() >= rooms.size()) { | ||
throw new CommandException(Messages.MESSAGE_INVALID_ROOM_INDEX); | ||
if (roomNumber < 0) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually now I'm wondering if you do your assertion early on in the constructor, then the app will directly crash and will never reach here :X
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup that's true. Good observation here 😄
That said, assertions may not necessarily be enabled, especially when an application goes into production.
The roomNumber < 0
check is to prevent the application from crashing if this method somehow gets called when assertions are off.
} | ||
|
||
@Override | ||
public CommandResult execute(Model model) throws CommandException { | ||
requireNonNull(model); | ||
if (roomNumber < 0) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same issue, if you assert in constructor, will this Message Invalid room number be received by user?
Task taskToDelete = tasks.get(taskIndex.getZeroBased()); | ||
Optional<Task> optionalTask = model.getTaskFromRoomWithTaskIndex(taskIndex, room); | ||
Task taskToDelete = optionalTask.orElseThrow(() -> | ||
new CommandException(Messages.MESSAGE_INVALID_TASK_INDEX)); | ||
assert taskToDelete != null : "The task to delete should never be null."; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the task to delete is null, the app will just crash?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, I think the app is meant to crash in that case. model.getTaskFromRoomWithTaskIndex(taskIndex, room)
should return either an empty Optional
or an Optional
-wrapped task. It should never be null
here.
The app will crash with a NullPointerException
if taskToDelete
is null
either way.
return roomList.containsRoom(room); | ||
} | ||
|
||
@Override | ||
public Optional<Room> getRoomWithRoomNumber(int roomNumber) { | ||
assert (roomNumber > 0) : "Room number should be greater than 0."; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will this crash the app?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, it will crash the app provided assertions are enabled. If getRoomWithRoomNumber(...)
is called with a roomNumber
greater than 0, it would be a programming error. Crashing in that case is helpful for debugging.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm! However, do read the comments about the assertions!
requireAllNonNull(task, roomIndex); | ||
public AddTaskCommand(Task task, int roomNumber) { | ||
requireNonNull(task); | ||
assert roomNumber > 0 : "Room number should be greater than 0."; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think you should be asserting if the roomNumber
is null
? Because if the roomNumber is <0 then it should be the user fault and not the system fault no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
roomNumber
cannot actually be null
though, because it is a primitive (i.e. int
).
addtask
,deletetask
, andedittask
now work with room number instead of index.