Skip to content

Sharing iP code quality feedback [for @NihaalManaf] - Round 3 #7

Description

@soc-se-bot

@NihaalManaf 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/optimusprime/OptimusPrime.java lines 18-129:

    public String getResponse(String input) {

        enum CommandType {
            BYE,
            MARK,
            UNMARK,
            LIST,
            TASK,
            DELETE,
            FIND,
            SORT,
            UNKNOWN;

            public static CommandType runCommand(String input) {
                if (input == null) {
                    return UNKNOWN;
                }
                return switch (input.toLowerCase()) {
                    case "bye" -> BYE;
                    case "mark" -> MARK;
                    case "unmark" -> UNMARK;
                    case "list" -> LIST;
                    case "todo" -> TASK;
                    case "deadline" -> TASK;
                    case "event" -> TASK;
                    case "delete" -> DELETE;
                    case "find" -> FIND;
                    case "sort" -> SORT;
                    default -> UNKNOWN;
                };
            }
        }

        TaskList tasks;
        try {
            tasks = DatabaseHandler.readDatabase();
        } catch (Exception e) {
            tasks = new TaskList();
        }

        String inputCommand = input.split(" ")[0];
        CommandType commandType = CommandType.runCommand(inputCommand);

        switch (commandType) {
            case BYE -> {
                return "Autobots, Roll Out!";
            }
            case UNMARK -> {
                char itemToAdd = input.charAt(input.length() - 1);
                int item = itemToAdd - '0';
                Task task = tasks.markIncomplete(item);
                DatabaseHandler.writeDatabase(tasks);
                return "OK, I've marked this task as not done yet:\n" + task;
            }
            case MARK -> {
                char itemToAdd = input.charAt(input.length() - 1);
                int item = itemToAdd - '0';
                Task task = tasks.markComplete(item);
                DatabaseHandler.writeDatabase(tasks);
                return "Nice! I've marked this task as done:\n" + task;
            }
            case LIST -> {
                return tasks.getTasks(tasks);
            }
            case TASK -> {
                String taskName = input.split(" ")[0];
                String metaData = input.replaceAll(taskName, "").trim();

                try {
                    String response = tasks.createTask(taskName, metaData);
                    DatabaseHandler.writeDatabase(tasks);
                    return response;
                } catch (InvalidArgumentException e) {
                    return e.getMessage();
                } catch (Exception e) {
                    System.out.println("Uh oh...The Decepticons are coming...\nLet's add your task later");
                }
            }
            case DELETE -> {
                try {
                    int toDelete = Integer.parseInt(input.split(" ")[1]);
                    String response = tasks.deleteTask(toDelete);
                    DatabaseHandler.writeDatabase(tasks);
                    return response;
                } catch (InvalidArgumentException e) {
                    return e.getMessage();
                }
            }
            case FIND -> {
                try {
                    String parsedInput = Parser.parseKeyword(input);
                    return tasks.findTasks(parsedInput);
                } catch (Exception e) {
                    return "Please enter an argument after 'find'";
                }
            }
            case SORT -> {
                try {
                    String[] parsedInput = Parser.parseTwoKeywords(input);
                    return tasks.sortTasks(parsedInput);
                } catch (Exception e) {
                    System.out.println(e.getMessage());
                    return "Please enter ascending or descending after 'sort'";
                }
            }
            default -> {
                return "Human... Please enter a valid command...";
            }
        }

        return "";
    }

Example from src/main/java/optimusprime/gui/MainWindow.java lines 55-113:

    public void start(Stage stage) {
        try {
            userImage = new Image(this.getClass().getResourceAsStream("/optimusprime/gui/resources/images/DaUser.png"));
            dukeImage = new Image(this.getClass().getResourceAsStream("/optimusprime/gui/resources/images/DaDuke.png"));
        } catch (Exception e) {
            System.err.println("Failed to load images: " + e.getMessage());
            e.printStackTrace();
        }

        scrollPane = new ScrollPane();
        dialogContainer = new VBox();
        scrollPane.setContent(dialogContainer);
        sendButton = new Button("Send");
        userInput = new TextField();
        AnchorPane mainLayout = new AnchorPane();

        stage.setTitle("Optimus Prime");
        stage.setResizable(false);
        stage.setMinHeight(600.0);
        stage.setMinWidth(400.0);

        mainLayout.setPrefSize(400.0, 600.0);

        scrollPane.setPrefSize(385, 535);
        scrollPane.setHbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);
        scrollPane.setVbarPolicy(ScrollPane.ScrollBarPolicy.ALWAYS);

        scrollPane.setVvalue(1.0);
        scrollPane.setFitToWidth(true);

        dialogContainer.setPrefHeight(Region.USE_COMPUTED_SIZE);
        dialogContainer.heightProperty().addListener((observable) -> scrollPane.setVvalue(1.0));

        userInput.setPrefWidth(325.0);

        sendButton.setPrefWidth(55.0);

        AnchorPane.setTopAnchor(scrollPane, 1.0);

        AnchorPane.setBottomAnchor(sendButton, 1.0);
        AnchorPane.setRightAnchor(sendButton, 1.0);

        AnchorPane.setLeftAnchor(userInput, 1.0);
        AnchorPane.setBottomAnchor(userInput, 1.0);

        sendButton.setOnMouseClicked((event) -> {
            handleUserInput();
        });
        userInput.setOnAction((event) -> {
            handleUserInput();
        });

        mainLayout.getChildren().addAll(scrollPane, userInput, sendButton);

        Scene scene = new Scene(mainLayout); // Setting the scene to be our Label

        stage.setScene(scene); // Setting the stage to show our scene
        stage.show(); // Render the stage.
    }

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/optimusprime/tasks/TaskList.java lines 208-214:

    /**
     * Private method to help with TaskList::sortTasks. Takes in a task and returns
     * the date of the relevant subclass types
     *
     * @param task an object of the class Task
     * @return a LocalDate object of the input Task
     */

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 Messages

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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