Skip to content
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

Merged
merged 7 commits into from
Oct 23, 2020

Conversation

w-yeehong
Copy link
Collaborator

addtask, deletetask, and edittask now work with room number instead of index.

@w-yeehong w-yeehong added this to the v1.3 milestone Oct 21, 2020
@w-yeehong w-yeehong linked an issue Oct 21, 2020 that may be closed by this pull request
@w-yeehong w-yeehong added Priority.High Type.BugFix Patches or fixes for a bug labels Oct 21, 2020
@w-yeehong w-yeehong changed the title Fixes #102 Modify task commands to work with room numbers instead of indexes Oct 21, 2020
@w-yeehong w-yeehong changed the title Modify task commands to work with room numbers instead of indexes Fix bug where task commands work with room index instead of room numbers Oct 21, 2020
Copy link

@chiamyunqing chiamyunqing left a 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) {

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

Copy link
Collaborator Author

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) {

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.";

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?

Copy link
Collaborator Author

@w-yeehong w-yeehong Oct 23, 2020

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.";

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?

Copy link
Collaborator Author

@w-yeehong w-yeehong Oct 23, 2020

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.

Copy link
Collaborator

@LeeMingDe LeeMingDe left a 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.";
Copy link
Collaborator

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?

Copy link
Collaborator Author

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).

@w-yeehong w-yeehong merged commit 5c43b68 into AY2021S1-CS2103T-W12-1:master Oct 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Priority.High Type.BugFix Patches or fixes for a bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Task commands use index of room instead of room number
3 participants