Skip to content

Sharing iP code quality feedback [for @Benjumpin] #2

@soc-se-bot-red

Description

@soc-se-bot-red

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

No easy-to-detect issues 👍

Aspect: Method Length

Example from src/main/java/benbot/BenBot.java lines 40-190:

    public String getResponse(String input) {
        assert input != null: "Input string to getResponse cannot be null";
        String niceInputString = input.trim();
        String[] inputStringArray = niceInputString.split(" ", 2);
        assert inputStringArray.length > 0: "input parsing failed to produce any tokens";
        String command = inputStringArray[0];
        Command com = getCommand(command);
        assert com != null: "getCommand should return a Command enum";
        try {
            switch (com) {
                case BYE:
                    return "Bye. Hope to see you again soon!";

                case LIST:
                    StringBuilder inputList = new StringBuilder();
                    for (int i = 0; i < ls.size(); i++) {
                        inputList.append(i + 1 + ". " + ls.get(i) + "\n");
                    }
                    return inputList.toString();

                case MARK:
                    String[] strings = niceInputString.split(" ");
                    Task itemToMark = ls.get(Integer.parseInt(strings[1]) - 1);
                    itemToMark.markDone();
                    saveTask(ls);
                    return "Nice! I've marked this task as done:\n " + itemToMark;

                case UNMARK:
                    String[] strings2 = niceInputString.split(" ");
                    Task itemToMark2 = ls.get(Integer.parseInt(strings2[1]) - 1);
                    itemToMark2.markUndone();
                    saveTask(ls);
                    return "OK, I've marked this task as not done yet:\n " + itemToMark2;

                case TODO:
                    if (niceInputString.length() <= 5) {
                        throw new BenBotExceptions("Empty description");
                    }
                    String todoItem = input.substring(5).trim();
                    Task t = new Todo(todoItem);
                    ls.add(t);
                    saveTask(ls);
                    return "Got it. I've added this task:\n  " + t +
                            "\nNow you have " + ls.size() + " tasks in the list.";

                case DEADLINE:
                    String[] deadlineItem = input.split(" /by ");
                    if (deadlineItem.length < 2) {
                        throw new BenBotExceptions("not valid deadline, use /by to specify date.\n");
                    }
                    if (deadlineItem[0].length() <= 9) {
                        throw new BenBotExceptions("Empty description");
                    } else {
                        String desc = deadlineItem[0].substring(9).trim();
                        String by = deadlineItem[1].trim();

                        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
                        LocalDateTime datetime = LocalDateTime.parse(by, formatter);

                        if (datetime.isBefore(LocalDateTime.now())) {
                            throw new BenBotExceptions("You cannot set a deadline in the past!");
                        }

                        Task td = new Deadline(desc, by);
                        ls.add(td);
                        saveTask(ls);
                        return "Got it. I've added this task:\n  " + td +
                                "\nNow you have " + ls.size() + " tasks in the list.";
                    }

                case EVENT:
                    String[] eventItem = input.split(" /from | /to ");
                    if (eventItem.length < 3) {
                        throw new BenBotExceptions("not valid event, use /from and /to to specify date\n");
                    }
                    if (eventItem[0].length() <= 6) {
                        throw new BenBotExceptions("Empty description");
                    } else {
                        String desc = eventItem[0].substring(6).trim();
                        String from = eventItem[1].trim();
                        String to = eventItem[2].trim();

                        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
                        LocalDateTime fromDate = LocalDateTime.parse(from, formatter);
                        LocalDateTime toDate = LocalDateTime.parse(to, formatter);

                        if (fromDate.isBefore(LocalDateTime.now())) {
                            throw new BenBotExceptions("You cannot set a date in the past!");
                        }

                        if (toDate.isBefore(fromDate)) {
                            throw new BenBotExceptions("Can't set end date before start date!");
                        }

                        Task te = new Event(desc, from, to);
                        ls.add(te);
                        saveTask(ls);
                        return "Got it. I've added this task:\n  " + te +
                                "\nNow you have " + ls.size() + " tasks in the list.";
                    }

                case DELETE:
                    String[] deleteItem = input.split(" ");
                    if (deleteItem.length < 2) {
                        throw new BenBotExceptions("Please provide number");
                    }
                    try {
                        int index = Integer.parseInt(deleteItem[1]) - 1;

                        if (index < 0 || index >= ls.size()) {
                            throw new BenBotExceptions("No such item in list");
                        }
                        Task tdel = ls.remove(index);
                        saveTask(ls);
                        return "Noted. I've removed this task:\n  " + tdel +
                                "\nNow you have " + ls.size() + " tasks in the list.";
                    } catch (NumberFormatException e) {
                        throw new BenBotExceptions("Please enter a number");
                    }

                case FIND:
                    if (niceInputString.length() <= 5) {
                        throw new BenBotExceptions("Enter task to search using find ___");
                    }

                    String keyword = niceInputString.substring(5).trim();
                    StringBuilder foundTasks = new StringBuilder("Here are the matching tasks:\n");
                    int matchCount = 0;

                    for (int i = 0; i < ls.size(); i++) {
                        Task task = ls.get(i);
                        if (task.toString().contains(keyword)) {
                            matchCount++;
                            foundTasks.append(matchCount + "." + task + "\n");
                        }
                    }

                    if (matchCount == 0) {
                        return "No task found";
                    } else {
                        return foundTasks.toString();
                    }

                default:
                    throw new BenBotExceptions("stop blabbering!");
            }

        } catch (Exception e) {
            return 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

No easy-to-detect issues 👍

Aspect: Recent Git Commit Messages

possible problems in commit 44760d1:


Added assertion to verify interal state

Integrate assert statement into BenBot.java abd Task.java to verify input strings are not empty and state transitions occur correctly

These assert statements are used as sanity checks and help check bugs


  • Not in imperative mood (?)
  • body not wrapped at 72 characters: e.g., Integrate assert statement into BenBot.java abd Task.java to verify input strings are not empty and state transitions occur correctly

Suggestion: Follow the given conventions for Git commit messages for future commits (do not modify past commit messages as doing so will change the commit timestamp that we used to detect your commit timings).

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