Skip to content

Sharing iP code quality feedback [for @wpx12011] - Round 2 #4

@nus-se-bot

Description

@nus-se-bot

@wpx12011 We did an automated analysis of your code to detect potential areas to improve the code quality. We are sharing the results below, so that you can avoid similar problems in your tP code (which will be graded more strictly for code quality).

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

No easy-to-detect issues 👍

Aspect: Method Length

Example from src/main/java/duke/Parser.java lines 44-134:

    public static Command parse(String fullCommand, Ui ui) throws DukeException {
        int firstWord = fullCommand.length() - 1;
        String commandName;
        try {
            firstWord = fullCommand.indexOf(' ');
            commandName = fullCommand.substring(0, firstWord);
        } catch (StringIndexOutOfBoundsException e) {
            commandName = fullCommand.trim();
        }

        if (!isValidCommand(commandName)) {
            throw new DukeException("Please enter a valid command\n"
                    + "You may enter [help] for more information :D");
        }

        assert !commandName.equals("");

        String command = fullCommand.substring(firstWord + 1);

        if (commandName.equals("todo")) {
            command = fullCommand.substring(4).trim();
        }

        switch (commandName) {
        case "bye":
        case "list":
        case "help":
        case "add": {
            return new Command(commandName);
        }
        case "mark":
        case "unmark":
        case "delete": {
            try {
                int index = Integer.parseInt(command.trim());
                return new UpdateCommand(commandName, index);
            } catch (NumberFormatException e) {
                return new ErrorCommand(ui.showTaskNoError());
            }
        }
        case "find": {
            return new FindCommand("find", command.trim());
        }
        case "todo": {
            String name = command.trim();
            if (name.isEmpty()) {
                return new ErrorCommand(ui.todoFormatAlert());
            } else {
                return new AddCommand("todo", name, null, null, null);
            }
        }
        case "deadline": {
            try {
                int firstSlash = command.indexOf('/');
                String name = command.substring(0, firstSlash);
                String dateInfo = command.substring(firstSlash + 1);
                if (name.isEmpty() || dateInfo.isEmpty()) {
                    throw new DukeException("the name or date information cannot be empty");
                } else {
                    DateTimeFormatter dateformatter = DateTimeFormatter.ofPattern("ddMMuuuu HHmm");
                    LocalDateTime by = LocalDateTime.parse(dateInfo, dateformatter);
                    return new AddCommand("deadline", name, by, null, null);
                }
            } catch (DateTimeParseException | StringIndexOutOfBoundsException | DukeException e) {
                return new ErrorCommand(ui.deadlineFormatAlert());
            }
        }
        case "event": {
            try {
                int firstSlash = command.indexOf('/');
                int lastSlash = command.lastIndexOf('/');
                String name = command.substring(0, firstSlash);
                String timeFrom = command.substring(firstSlash + 1, lastSlash - 1);
                String timeTo = command.substring(lastSlash + 1);
                if (name.isEmpty() || timeFrom.isEmpty() || timeTo.isEmpty()) {
                    throw new DukeException("the name or time information cannot be empty");
                } else {
                    DateTimeFormatter dateformatter = DateTimeFormatter.ofPattern("ddMMuuuu HHmm");
                    LocalDateTime from = LocalDateTime.parse(timeFrom, dateformatter);
                    LocalDateTime to = LocalDateTime.parse(timeTo, dateformatter);
                    return new AddCommand("event", name, null, from, to);
                }
            } catch (DateTimeParseException | StringIndexOutOfBoundsException | DukeException e) {
                return new ErrorCommand(ui.eventFormatAlert());
            }
        }
        default: {
            return new ErrorCommand("May I know what type of task this is?");
        }
        }
    }

Example from src/main/java/duke/Storage.java lines 39-76:

    public ArrayList<Task> load() throws DukeException {
        ArrayList<Task> taskList = new ArrayList<>();
        try {
            if (!savedTasks.createNewFile()) {
                // Load the exist file
                Scanner fileScanner = new Scanner(savedTasks);
                while (fileScanner.hasNextLine()) {
                    String record = fileScanner.nextLine();
                    String[] fields = record.split("\\|");
                    Task currTask;
                    if (fields[0].equals("T")) {
                        currTask = new Todo(fields[2]);
                    } else if (fields[0].equals("D")) {
                        DateTimeFormatter dateformatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm");
                        LocalDateTime by = LocalDateTime.parse(fields[3], dateformatter);
                        currTask = new Deadline(fields[2], by);
                    } else if (fields[0].equals("E")) {
                        DateTimeFormatter dateformatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm");
                        LocalDateTime from = LocalDateTime.parse(fields[3], dateformatter);
                        LocalDateTime to = LocalDateTime.parse(fields[4], dateformatter);
                        currTask = new Event(fields[2], from, to);
                    } else {
                        currTask = new Task("");
                    }

                    if (fields[1].equals("1")) {
                        currTask.setDone();
                    } else {
                        currTask.setNotDone();
                    }
                    taskList.add(currTask);
                }
            }
        } catch (IOException | DateTimeParseException | ArrayIndexOutOfBoundsException e) {
            throw new DukeException("Loading failed");
        }
        return taskList;
    }

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/duke/Duke.java lines 29-31:

    /**
     * Start the programme
     */

Example from src/main/java/duke/Ui.java lines 36-39:

    /**
     * Print greetings
     * @return greetings
     */

Example from src/main/java/duke/Ui.java lines 150-153:

    /**
     * Print messages to alert user when input task index exceeds the length of task list.
     * @param index Input task index.
     */

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 (Subject Only)

No easy-to-detect issues 👍

Aspect: Binary files in repo

No easy-to-detect issues 👍

❗ You are not required to (but you are welcome to) fix the above problems in your iP, unless you have been separately asked to resubmit the iP due to code quality issues.

ℹ️ 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