Skip to content

Sharing iP code quality feedback [for @zuohui48] #3

@nus-se-script

Description

@nus-se-script

@zuohui48 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/duke/Duke.java lines 26-26:

            // ui.showLoadingError();

Suggestion: Remove dead code from the codebase.

Aspect: Method Length

Example from src/main/java/duke/Duke.java lines 30-76:

    public static void main(String[] args) throws DukeException, IOException {
        ui.printWelcomeMessage();
        ui.showLine();
        list = Storage.loadData();
        Scanner sc = new Scanner(System.in);
        String input = sc.nextLine();

        while (!logic.checkBye(input)) {
            if (logic.checkList(input)) {
                ui.printListMessage();
                list.list();
                ui.showLine();
            } else if (logic.checkFind(input)) {
                String word = input.split(" ")[1];
                ui.printFindMessage();
                list.find(word);
                ui.showLine();
            } else if (logic.checkMark(input)) {
                int num = Integer.parseInt(input.split(" ")[1]);
                list.mark(num);
                ui.printMarkMessage(list.get(num));
                ui.showLine();
            } else if (logic.checkUnmark(input)) {
                int num = Integer.parseInt(input.split(" ")[1]);
                list.unmark(num);
                ui.printUnmarkMessage(list.get(num));
                ui.showLine();
           } else if (logic.checkDelete(input)) {
                int num = Integer.parseInt(input.split(" ")[1]);
                ui.printDeleteMessage(list.get(num), list);
                list.removeFind(list.get(num));
                list.delete(num);
                ui.showLine();
            } else if (logic.checkTask(input)) {
                list.add(input);
                ui.printAddMessage(list.getLast(), list);
                ui.showLine();
            } else if (!logic.isValidCommand(input)) {
                ui.printInvalidCommandMessage();
                ui.showLine();
            }
            input = sc.nextLine();
        }
        Storage.saveData(list);
        ui.printByeMessage();
        ui.showLine();
    }

Example from src/main/java/duke/storage/Storage.java lines 31-121:

    public static TaskList loadData() throws IOException {
        TaskList list = new TaskList();
        if (!dataFolder.exists()) {
            System.out.println("The data folder is not found, creating new data folder");
            dataFolder.mkdir();
            System.out.println("Folder created successfully");
            File f = new File(dataFolder, "task.txt");
            try {
                f.createNewFile();
                System.out.printf("file created successfully\n");
            } catch (IOException e) {
                throw new DukeException("Error creating file: " + e.getMessage());
            }
        } else if(!data.exists()) {
            System.out.println("file does not exist, creating new file");
            File f = new File(dataFolder, "duke.txt");
            try {
                f.createNewFile();
                System.out.printf("file create successfully");
            } catch (IOException e) {
                throw new DukeException("Error creating file: " + e.getMessage());
            }
        } else {
            List<String> tmp = Files.readAllLines(Paths.get(path), StandardCharsets.UTF_8);
            Iterator<String> itr = tmp.iterator();
            Task newTask;
            while (itr.hasNext()) {
                String curr = itr.next();
                String[] currLine = curr.split(" \\| ");
                String type = currLine[0];
                String description = currLine[2];
                switch (type) {
                    case "D": {
                        String[] deadline = currLine[3].split(" ");
                        String[] date = deadline[0].split("/");
                        for (int i = 0; i < date.length; i++) {
                            if (date[i].length() < 2) {
                                date[i] = "0" + date[i];
                            }
                        }
                        String newDate = date[2] + "-" + date[1] + "-" + date[0];
                        String remarks = " | " + currLine[3];
                        if (deadline.length == 1) {
                            newTask = new Deadline(description, newDate, remarks);
                        } else {
                            String[] time = deadline[1].split("");
                            String newTime = time[0] + time[1] + ":" + time[2] + time[3];
                            newTask = new Deadline(description, newDate, newTime, remarks);
                        }
                        break;
                    }
                    case "E": {
                        String[] start = currLine[3].split(" ");
                        String[] end = currLine[4].split(" ");
                        String[] startDate = start[0].split("/");
                        String[] endDate = end[0].split("/");
                        for (int i = 0; i < startDate.length; i++) {
                            if (startDate[i].length() < 2) {
                                startDate[i] = "0" + startDate[i];
                            }
                            if (endDate[i].length() < 2) {
                                endDate[i] = "0" + endDate[i];
                            }
                        }
                        String newStartDate = startDate[2] + "-" + startDate[1] + "-" + startDate[0];
                        String newEndDate = endDate[2] + "-" + endDate[1] + "-" + endDate[0];
                        String remarks = " | " + currLine[3] + " | " + currLine[4];
                        if (start.length > 1) {
                            String[] startTime = start[1].split("");
                            String newStartTime = startTime[0] + startTime[1] + ":" + startTime[2] + startTime[3];
                            String[] endTime = end[1].split("");
                            String newEndTime = endTime[0] + endTime[1] + ":" + endTime[2] + endTime[3];
                            newTask = new Event(description, newStartDate, newEndDate, newStartTime, newEndTime, remarks);
                        } else {
                            newTask = new Event(description, newStartDate, newEndDate, remarks);
                        }
                        break;
                    }
                    default: {
                        newTask = new ToDo(description);
                    }
                }
                if (currLine[1].equals("X")) {
                    newTask.mark();
                }
                list.addFind(description, newTask);
                list.add(newTask);
            }
        }
        return list;
    }

Example from src/main/java/duke/storage/TaskList.java lines 83-168:

    public void add(String input) {
        Task newTask;
        String[] inputs = input.split(" ");
        String type = inputs[0];
        switch(type) {
            case "todo": {
                if (inputs.length < 2) {
                    throw new DukeException("☹ OOPS!!! The description of a todo cannot be empty.");
                }
                String name = input.split(" ", 2)[1];
                newTask = new ToDo(name);
                break;
            }
            case "deadline": {
                if (inputs.length < 2) {
                    throw new DukeException("☹ OOPS!!! The description of a deadline cannot be empty.");
                }
                String[] nameAndDeadline = input.split(" ", 2)[1].split(" /by ");
                if (nameAndDeadline.length < 2) {
                    throw new DukeException("☹ OOPS!!! The deadline of a deadline cannot be empty.");
                }
                String name = nameAndDeadline[0];
                String[] deadline = nameAndDeadline[1].split(" ");
                String[] date = deadline[0].split("/");
                String remarks = " | " + nameAndDeadline[1];
                for (int i = 0; i < date.length; i++) {
                    if (date[i].length() < 2) {
                        date[i] = "0" + date[i];
                    }
                }
                String newDate = date[2] + "-" + date[1] + "-" + date[0];
                if (deadline.length == 1) {
                    newTask = new Deadline(name, newDate, remarks);
                } else {
                    String[] time = deadline[1].split("");
                    String newTime = time[0] + time[1] + ":" + time[2] + time[3];
                    newTask = new Deadline(name, newDate, newTime, remarks);
                }
                break;
            }
            case "event": {
                if (inputs.length < 2) {
                    throw new DukeException("☹ OOPS!!! The description of a event cannot be empty.");
                }
                String[] nameAndStart = input.split(" ", 2)[1].split(" /from ");
                if (nameAndStart.length < 2) {
                    throw new DukeException("☹ OOPS!!! The start of a event cannot be empty.");
                }
                String name = nameAndStart[0];
                String[] startAndEnd = nameAndStart[1].split(" /to ");
                if (startAndEnd.length < 2) {
                    throw new DukeException("☹ OOPS!!! The end of a event cannot be empty.");
                }
                String[] start = startAndEnd[0].split(" ");
                String[] startDate = start[0].split("/");
                String[] end = startAndEnd[1].split(" ");
                String[] endDate = end[0].split("/");
                for (int i = 0; i < startDate.length; i++) {
                    if (startDate[i].length() < 2) {
                        startDate[i] = "0" + startDate[i];
                    }
                    if (endDate[i].length() < 2) {
                        endDate[i] = "0" + endDate[i];
                    }
                }
                String newStartDate = startDate[2] + "-" + startDate[1] + "-" + startDate[0];
                String newEndDate = endDate[2] + "-" + endDate[1] + "-" + endDate[0];
                String remarks = " | " + startAndEnd[0] + " | " + startAndEnd[1];
                if (start.length > 1) {
                    String[] startTime = start[1].split("");
                    String newStartTime = startTime[0] + startTime[1] + ":" + startTime[2] + startTime[3];
                    String[] endTime = end[1].split("");
                    String newEndTime = endTime[0] + endTime[1] + ":" + endTime[2] + endTime[3];
                    newTask = new Event(name, newStartDate, newEndDate, newStartTime, newEndTime, remarks);
                } else {
                    newTask = new Event(name, newStartDate, newEndDate, remarks);
                }
                break;
            }
            default: {
                throw new DukeException("☹ OOPS!!! I'm sorry, but I don't know what that means :-(");
            }
        }
        addFind(newTask.getDescription(), newTask);
        list.add(newTask);
    }

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

No easy-to-detect issues 👍

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.

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