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

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

Open
nus-se-bot opened this issue Feb 11, 2023 · 0 comments
Open

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

nus-se-bot opened this issue Feb 11, 2023 · 0 comments

Comments

@nus-se-bot
Copy link

@leeyi45 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

Example from src/main/java/duke/task/Task.java lines 19-19:

    private boolean done;

Suggestion: Follow the given naming convention for boolean variables/methods (e.g., use a boolean-sounding prefix).You may ignore the above if you think the name already follows the convention (the script can report false positives in some cases)

Aspect: Brace Style

No easy-to-detect issues 👍

Aspect: Package Name Style

Example from src/main/java/duke/commands/indexedCommand/DeleteCommand.java lines 1-1:

package duke.commands.indexedCommand;

Example from src/main/java/duke/commands/indexedCommand/IndexedCommand.java lines 1-1:

package duke.commands.indexedCommand;

Example from src/main/java/duke/commands/indexedCommand/MarkCommand.java lines 1-1:

package duke.commands.indexedCommand;

Suggestion: Follow the package naming convention specified by the coding standard.

Aspect: Class Name Style

No easy-to-detect issues 👍

Aspect: Dead Code

Example from src/main/java/duke/Utils.java lines 48-48:

                //   System.out.format("Couldn't match %s with %s\n", str, pattern);

Suggestion: Remove dead code from the codebase.

Aspect: Method Length

Example from src/main/java/duke/Utils.java lines 64-113:

    public final static LocalDateTime parseDateTime(String str0, String str1) {
        List<String> dateFormats = List.of(
            "dd/MM" 
        );

        List<String> timeFormats = List.of(
            "hh:mma",
            "kk:mm",
            "kkmm"
        );

        Function<String, Optional<MonthDay>> dateParser = createParser(dateFormats, MonthDay::parse);
        Function<String, Optional<LocalTime>> timeParser = createParser(timeFormats, LocalTime::parse);

        if (str0 == null && str1 == null) {
            throw new IllegalArgumentException("Both str0 and str1 cannot be null!", null);
        } else if (str0 == null && str1 != null) {
            return parseDateTime(str1, null);
        } 
        
        LocalDateTime currentTime = LocalDateTime.now();

        if (str0 != null && str1 == null) {
            Optional<MonthDay> dateValue = dateParser.apply(str0);
            if (dateValue.isEmpty()) {
                Optional<LocalTime> timeValue = timeParser.apply(str0.toUpperCase());

                if (timeValue.isEmpty()) { 
                    throw new DateTimeParseException("Invalid date/time string!", str0, 0);
                }
                return LocalDateTime.of(currentTime.toLocalDate(), timeValue.get());
            }
            return LocalDateTime.of(dateValue.get().atYear(currentTime.getYear()), currentTime.toLocalTime());
        } else {
            Optional<MonthDay> date = dateParser.apply(str0);
            Optional<LocalTime> time = timeParser.apply(str1.toUpperCase());

            if (date.isEmpty() || time.isEmpty()) {
                date = dateParser.apply(str1);
                time = timeParser.apply(str0.toUpperCase());
            }

            if (date.isEmpty() || time.isEmpty()) {
                throw new DateTimeParseException("Invalid date/time string!", str1, 0);
            }

            LocalDate dateValue = date.get().atYear(currentTime.getYear());
            return LocalDateTime.of(dateValue, time.get());
        }
    }

Example from src/main/java/duke/commands/taskCommand/EventCommand.java lines 16-62:

    protected Event getTask(String[] args, Duke instance) throws ValidationException {
        int fromIndex = -1, toIndex = -1;
        for (int i = 1; i < args.length; i++) {
            if (args[i].equalsIgnoreCase("/from")) {
                fromIndex = i;
            } else if (args[i].equalsIgnoreCase("/to")) {
                toIndex = i;
            }

            if (fromIndex != -1 && toIndex != -1) {
                break;
            }
        }

        validate(fromIndex != -1, "Expected a /from directive!");
        validate(toIndex != -1, "Expected a /to directive!");
        int fromLength, toLength;
        if (fromIndex > toIndex) {
            fromLength = args.length - fromIndex - 1;
            toLength = fromIndex - toIndex  - 1;
        } else {
            toLength = args.length - toIndex - 1;
            fromLength = toIndex - fromIndex  - 1;
        }

        validate(Math.min(fromIndex, toIndex) > 1, "Expected a task!");
        validate(toLength > 0, "Expected a time after /to!");
        validate(fromLength > 0, "Expected a time after /from!");

        try {
            LocalDateTime fromTime = Utils.parseDateTime(
                args[fromIndex + 1],
                fromLength < 2 ? null : args[fromIndex + 2]
            );
            LocalDateTime toTime = Utils.parseDateTime(
                args[toIndex + 1], 
                toLength < 2 ? null : args[toIndex + 2]
            );

            validate(fromTime.isBefore(toTime), " I can't create an event that ends before it starts!");

            String taskStr = Utils.stringJoiner(args, 1, Math.min(fromIndex, toIndex));
            return new Event(taskStr, fromTime, toTime);
        } catch (DateTimeParseException e) {
            throw new ValidationException("Failed to parse the date you've given: %s\n", e.getParsedString());
        }
    }

Example from src/main/java/duke/ui/AddTaskWindow.java lines 40-90:

    public static Dialog<Optional<Task>> getAddTaskDialog() throws IOException {
        AddTaskWindow controller = new AddTaskWindow();
        DialogPane pane = Utils.loadFxmlFile(AddTaskWindow.class.getResource("/view/AddTaskWindow.fxml"), controller);
        Dialog<Optional<Task>> diag = new Dialog<>();
        diag.setDialogPane(pane);
        diag.setResultConverter((buttonType) -> {
            if (buttonType == ButtonType.OK) {
                Task newTask = controller.createTask();
                return Optional.of(newTask);
            } else {
                return Optional.empty();
            }
        });
        pane.getButtonTypes().addAll(
            ButtonType.OK,
            ButtonType.CANCEL
        );
        pane.lookupButton(ButtonType.OK).addEventFilter(ActionEvent.ACTION, event -> {
            String error = null;

            String typeToAdd = controller.taskTypeComboBox.getValue();
            if (typeToAdd == null || typeToAdd.isBlank()) {
                error = "Select a type for the task!";
            }

            String taskDesc = controller.taskDescField.getText();
            if (taskDesc == null || taskDesc.isBlank()) {
                error = "A valid description for the task must be provided!";
            }

            if (typeToAdd.equals("Deadline")) {
                if (controller.endDatePicker.getValue() == null) {
                    error = "Need a deadline!";
                }
            } else if (typeToAdd.equals("Event")) {
                if (controller.startDatePicker.getValue() == null) {
                    error = "Need a start time for event!";
                } else if (controller.endDatePicker.getValue() == null) {
                    error = "Need an end time for event!";
                }
            }

            if (error != null) {
                Alert alert = new Alert(AlertType.ERROR, error, new ButtonType[] { ButtonType.OK });
                alert.show();
                event.consume();
            } 
        });

        return diag;
    }

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/Utils.java lines 23-30:

    /**
     * Recombine an array of strings into a singular string, starting from the provided
     * start index to the end index exclusive.
     * @param args Array containing strings to join
     * @param from From index, inclusive
     * @param to To index, exclusive
     * @return Recombined string
     */

Example from src/main/java/duke/Utils.java lines 55-63:

    /**
     * Parse the strings for date and time information. Passing null for one of the arguments indicates
     * that only either date or time information was available. Passing both will create a LocalDateTime
     * with both time and date information. If there is a missing component, it will be filled with LocalDateTime.now().
     * @param str0 Null or string to parse for either date or time
     * @param str1 Null or string to parse for either date or time
     * @return LocalDateTime containing either the parsed date and time or the parsed arguments
     * combined with the current time.
     */

Example from src/main/java/duke/commands/Command.java lines 28-33:

    /**
     * Throw {@link #ValidationException ValidationException} if the provided condition is false. 
     * @param condition Condition to check
     * @param errMsg Error message to return to the user
     * @throws ValidationException
     */

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)

possible problems in commit 2a00f01:

Added a UI to Duke

  • Not in imperative mood (?)

Suggestion: Follow the given conventions for Git commit messages for future commits (no need to modify past commit messages).

Aspect: Binary files in repo

No easy-to-detect 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant