Skip to content

Sharing iP code quality feedback [for @yyccbb] #1

@nus-se-bot

Description

@nus-se-bot

@yyccbb We did an automated analysis of your code to detect potential areas to improve the code quality. We are sharing the results below, to help you improve the iP code further.

IMPORTANT: Note that the script looked for just a few easy-to-detect problems only, and at-most three example are given i.e., there can be other areas/places to improve.

Aspect: Tab Usage

No easy-to-detect issues 👍

Aspect: Naming boolean variables/methods

No easy-to-detect issues 👍

Aspect: Brace Style

No easy-to-detect issues 👍

Aspect: Package Name Style

No easy-to-detect issues 👍

Aspect: Class Name Style

No easy-to-detect issues 👍

Aspect: Dead Code

Example from src/main/java/nihao/Nihao.java lines 23-23:

//        File myInput = new File("text-ui-test/input.txt");

Example from src/main/java/nihao/Nihao.java lines 24-24:

//        try {

Example from src/main/java/nihao/Nihao.java lines 25-25:

//            Scanner scanner = new Scanner(myInput);

Suggestion: Remove dead code from the codebase.

Aspect: Method Length

Example from src/main/java/nihao/handler/InputHandler.java lines 31-98:

    public static Action handleInput(String input) throws Exception {
        String[] parsedInput = input.split(" ");
        String commandName = parsedInput[0];
        Command command = Command.getEnum(commandName);
        String taskName;
        switch (command) {
        case BYE:
            return new ExitAction();
        case LIST:
            return new ListAction();
        case MARK:
            if (parsedInput.length != 2) {
                throw new IllegalArgumentException("mark", 1);
            }
            return new MarkAction(parsedInput[1]);
        case UNMARK:
            if (parsedInput.length != 2) {
                throw new IllegalArgumentException("unmark", 1);
            }
            return new UnmarkAction(parsedInput[1]);
        case DELETE:
            if (parsedInput.length != 2) {
                throw new IllegalArgumentException("delete", 1);
            }
            return new DeleteAction(parsedInput[1]);
        case FIND:
            if (parsedInput.length != 2) {
                throw new IllegalArgumentException("find", 1);
            }
            return new FindAction(parsedInput[1]);
        case TODO:
            if (parsedInput.length < 2) {
                throw new IllegalArgumentException("todo requires at least 1 argument");
            }
            taskName = input.substring(5);
            TodoTask todoTask = new TodoTask(taskName);
            return new TaskAction(todoTask);
        case DEADLINE:
            if (countByFlag(parsedInput) != 1) {
                throw new IllegalArgumentException("deadline requires exactly 1 /by flag");
            }
            int byIndex = input.indexOf(" /by ");
            if (byIndex < 9) {
                throw new IllegalArgumentException("illegal use of /by flag");
            }
            taskName = input.substring(9, byIndex);
            LocalDateTime by = DateTimeHandler.handleInput(input.substring(byIndex + 5));
            DeadlineTask deadlineTask = new DeadlineTask(taskName, by);
            return new TaskAction(deadlineTask);
        case EVENT:
            if (countFromFlag(parsedInput) != 1 || countToFlag(parsedInput) != 1) {
                throw new IllegalArgumentException("'event' requires exactly 1 /from flag and 1 /to flag");
            }
            int fromIndex = input.indexOf(" /from ");
            int toIndex = input.indexOf(" /to ");
            if (fromIndex < 6 || toIndex < fromIndex + 7) {
                throw new IllegalArgumentException("illegal use of flags");
                // Todo: define "  " and " " more clearly
            }
            taskName = input.substring(6, fromIndex);
            LocalDateTime from = DateTimeHandler.handleInput(input.substring(fromIndex + 7, toIndex));
            LocalDateTime to = DateTimeHandler.handleInput(input.substring(toIndex + 5));
            EventTask eventTask = new EventTask(taskName, from, to);
            return new TaskAction(eventTask);
        default:
            throw new UnknownCommandException(input);
        }
    }

Example from src/main/java/nihao/util/TaskTypeAdapter.java lines 49-99:

    public Task read(JsonReader jsonReader) throws IOException {
        Task ret = null;
        jsonReader.beginObject();
        String fieldName = null;
        JsonToken token = jsonReader.peek();
        if (token.equals(JsonToken.NAME)) {
            fieldName = jsonReader.nextName();
        }
        token = jsonReader.peek();
        String type = jsonReader.nextString();
        token = jsonReader.peek();
        fieldName = jsonReader.nextName();
        token = jsonReader.peek();
        String taskName = jsonReader.nextString();
        token = jsonReader.peek();
        fieldName = jsonReader.nextName();
        token = jsonReader.peek();
        boolean isCompleted = jsonReader.nextBoolean();

        switch (type) {
        case "TodoTask":
            ret = new TodoTask(taskName);
            ret.setIsCompleted(isCompleted);
            break;
        case "DeadlineTask":
            token = jsonReader.peek();
            if (token.equals(JsonToken.NAME)) {
                fieldName = jsonReader.nextName();
            }
            token = jsonReader.peek();
            LocalDateTime by = DateTimeHandler.deserialize(jsonReader.nextString());
            ret = new DeadlineTask(taskName, by);
            ret.setIsCompleted(isCompleted);
            break;
        case "EventTask":
            token = jsonReader.peek();
            fieldName = jsonReader.nextName();
            token = jsonReader.peek();
            LocalDateTime from = DateTimeHandler.deserialize(jsonReader.nextString());
            token = jsonReader.peek();
            fieldName = jsonReader.nextName();
            token = jsonReader.peek();
            LocalDateTime to = DateTimeHandler.deserialize(jsonReader.nextString());
            ret = new EventTask(taskName, from, to);
            ret.setIsCompleted(isCompleted);
            break;
        }

        jsonReader.endObject();
        return ret;
    }

Example from src/test/java/nihao/handler/InputHandlerTest.java lines 53-102:

    void handleInput_exceptionThrown() {
        try {
            InputHandler.handleInput("waibi babu");
            fail();
        } catch (Exception e) {
            assertEquals("UnknownCommandException: 'waibi babu' is unknown.", e.getMessage());
        }

        try {
            InputHandler.handleInput("mark 3 4");
            fail();
        } catch (Exception e) {
            assertEquals("IllegalArgumentException: mark expects 1 arguments.", e.getMessage());
        }

        try {
            InputHandler.handleInput("mark ");
            fail();
        } catch (Exception e) {
            assertEquals("IllegalArgumentException: mark expects 1 arguments.", e.getMessage());
        }

        try {
            InputHandler.handleInput("todo");
            fail();
        } catch (Exception e) {
            assertEquals("IllegalArgumentException: todo requires at least 1 argument.", e.getMessage());
        }

        try {
            InputHandler.handleInput("deadline /by /by");
            fail();
        } catch (Exception e) {
            assertEquals("IllegalArgumentException: deadline requires exactly 1 /by flag.", e.getMessage());
        }

        try {
            InputHandler.handleInput("deadline ddl /by");
            fail();
        } catch (Exception e) {
            assertEquals("IllegalArgumentException: illegal use of /by flag.", e.getMessage());
        }

        try {
            InputHandler.handleInput("event /from /to 09/02/2024 2000");
            fail();
        } catch (Exception e) {
            assertEquals("IllegalArgumentException: illegal use of flags.", e.getMessage());
        }
    }

Suggestion: Consider applying SLAP (and other abstraction mechanisms) to shorten methods e.g., extract some code blocks into separate methods. You may ignore this suggestion if you think a longer method is justified in a particular case.

Aspect: Class size

No easy-to-detect issues 👍

Aspect: Header Comments

Example from src/main/java/nihao/handler/DataHandler.java lines 92-97:

    /**
     * Mark the Task at the given index as completed.
     *
     * @param index Index of the task to be marked as completed.
     * @throws IndexOutOfBoundsException When the index provided is more than the length of tasks.
     */

Example from src/main/java/nihao/handler/DataHandler.java lines 105-110:

    /**
     * Mark the Task at the given index as uncompleted.
     *
     * @param index Index of the task to be marked as uncompleted.
     * @throws IndexOutOfBoundsException When the index provided is more than the length of tasks.
     */

Example from src/main/java/nihao/handler/DataHandler.java lines 118-123:

    /**
     * Delete the Task at the given index.
     *
     * @param index Index of the task to be deleted.
     * @throws IndexOutOfBoundsException When the index provided is more than the length of tasks.
     */

Suggestion: Ensure method/class header comments follow the format specified in the coding standard, in particular, the phrasing of the overview statement.

Aspect: Recent Git Commit Message

No easy-to-detect issues 👍

Aspect: Binary files in repo

Suggestion: Avoid committing binary files (e.g., *.class, *.jar, *.exe) or third-party library files in to the repo.


ℹ️ The bot account used to post this issue is un-manned. Do not reply to this post (as those replies will not be read). Instead, contact cs2103@comp.nus.edu.sg if you want to follow up on this post.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions