-
Notifications
You must be signed in to change notification settings - Fork 438
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
[Shulong] iP #467
base: master
Are you sure you want to change the base?
[Shulong] iP #467
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi, those are my PR review (on coding standard)!
If I mentioned "Reference", it means it was taken from the Java coding standard from CS2103T website.
src/main/java/Duke.java
Outdated
int location = 0; | ||
boolean exist = false; | ||
for (int i = 0; i < string.length(); i++){ | ||
if(string.charAt(i) == '/'){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should there be whitespace after "if"? I noticed the same issue in several other places too.
Reference: "Java reserved words should be followed by a white space." from CS2103 Java coding standard web page
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks Dahfin, I will change that!
src/main/java/Duke.java
Outdated
// Find out the position of the first '/' | ||
public int firstSlashTracker (String string){ | ||
int location = 0; | ||
boolean exist = false; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should the name of the boolean be "isExist"? I also noticed the same issue in other boolean variables too.
Reference: "Boolean variables/methods should be named to sound like booleans"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good point!
src/main/java/Duke.java
Outdated
@@ -1,10 +1,268 @@ | |||
import java.util.ArrayList; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi, should there be a package statement above this line?
Reference: "Every class should be part of some package."
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so how does your package look like?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it actually depends on how the directory in your project looks like. one of my package statements is "package command"
src/main/java/Duke.java
Outdated
System.out.println("oops, the deadline description cannot be empty"); | ||
}else { | ||
int number = firstSlashTracker(order); | ||
Task item = new Task(1, 0,order.substring(8, number) + "(by: " + |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should the plus sign at the end of the line be placed below instead (i.e. after the break)?
Reference: "Break before an operator."
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ohh I will take note !
src/main/java/Task.java
Outdated
@@ -0,0 +1,10 @@ | |||
public class Task { | |||
public int status; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be declared as non-public? I also see the same issue on some other variables.
Reference: "Class variables should never be declared public unless the class is a data class with no behavior."
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
behaviour here means methods?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not 100% sure, but I think it means methods
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code seems to be working fine and satisfies the requirements of the iP. The quality of the code in terms of readability could be improved on, and try to keep your if/else and try/catch blocks as concise as possible. Jiayou for the rest of your iP!
src/main/java/Duke.java
Outdated
var correct = true; | ||
var deleted = false; | ||
var done = order.length() >= 4 && order.substring(0 , 4).equals("done"); | ||
var delete = order.length() >= 6 && order.substring(0 , 6).equals("delete"); | ||
var todo = order.length() >= 4 && order.substring(0 , 4).equals("todo"); | ||
var deadline = order.length() >= 8 && order.substring(0, 8).equals("deadline"); | ||
var event = order.length() >= 5 && order.substring(0, 5).equals("event"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe you can consider specifying the data type of your variables instead of just using 'var' for all of them? I'm not sure about the technical benefits of using int, boolean, String etc but it would definitely improve the readability of your code which will be helpful for your tP! Also, it seems like all of them are boolean values so you could consider naming them isCorrect, isDeleted, isInputDone etc to follow the coding standard.
src/main/java/Duke.java
Outdated
var done = order.length() >= 4 && order.substring(0 , 4).equals("done"); | ||
var delete = order.length() >= 6 && order.substring(0 , 6).equals("delete"); | ||
var todo = order.length() >= 4 && order.substring(0 , 4).equals("todo"); | ||
var deadline = order.length() >= 8 && order.substring(0, 8).equals("deadline"); | ||
var event = order.length() >= 5 && order.substring(0, 5).equals("event"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if you already know but order.substring(0 , 4).equals("done") could be replaced with order.startsWith("done"), just to improve readability but you will get the same result either way.
src/main/java/Duke.java
Outdated
if(orders.get(i).status == 0) { | ||
if(orders.get(i).category == 0){ | ||
System.out.println(i + 1 + ". " + " [T][x] "+ orders.get(i).command); | ||
}else if(orders.get(i).category == 1){ | ||
System.out.println(i + 1 + ". " + " [D][x] " + orders.get(i).command); | ||
}else if(orders.get(i).category == 2){ | ||
System.out.println(i + 1 + ". " + " [E][x] " + orders.get(i).command); | ||
}else { | ||
System.out.println(i + 1 + ". " + " [x] " + orders.get(i).command); | ||
} | ||
}else { | ||
if(orders.get(i).category == 0){ | ||
System.out.println(i + 1 + ". " + " [T][v] "+ orders.get(i).command); | ||
}else if(orders.get(i).category == 1){ | ||
System.out.println(i + 1 + ". " + " [D][v] " + orders.get(i).command); | ||
}else if(orders.get(i).category == 2){ | ||
System.out.println(i + 1 + ". " + " [E][v] " + orders.get(i).command); | ||
}else { | ||
System.out.println(i + 1 + ". " + " [v] " + orders.get(i).command); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One possible way to make the printing of the tasks out neater could be by overriding the toString method of your Task class so that you can just call System.out.println(i + 1 + ". " + orders.get(i)) instead. It is the same logic as what you are doing here, just that the checking of status and category is done inside the Task class and this will help with abstraction.
src/main/java/Duke.java
Outdated
if(correct && !done && !deleted && !order.equals("task list")) { | ||
if(todo){ | ||
if(order.length() <= 4){ | ||
System.out.println("oops, the todo description cannot be empty"); | ||
}else { | ||
Task item = new Task(0, 0, order.substring(4) ); | ||
orders.add(item); | ||
} | ||
}else if(deadline){ | ||
if(order.length() <= 8){ | ||
System.out.println("oops, the deadline description cannot be empty"); | ||
}else { | ||
int number = firstSlashTracker(order); | ||
Task item = new Task(1, 0,order.substring(8, number) + "(by: " + | ||
order.substring(number + 3) + ")"); | ||
orders.add(item); | ||
} | ||
}else if(event){ | ||
if(order.length() <= 5){ | ||
System.out.println("oops, the event description cannot be empty"); | ||
}else { | ||
int number = firstSlashTracker(order); | ||
Task item = new Task(2, 0, order.substring(5, number) + "(at: " + | ||
order.substring(number + 3) + ")"); | ||
orders.add(item); | ||
} | ||
|
||
}else { | ||
Task item = new Task(4, 0, order); | ||
orders.add(item); | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure why you decided to make another if block to check for todo, event, and deadline since you already checked for the booleans at lines 117, 126 and 137. Would combining the bodies of the 2 if blocks of todo change the output in any way?
src/main/java/Task.java
Outdated
@@ -0,0 +1,10 @@ | |||
public class Task { | |||
public int status; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does the status(0, 1) refer to whether or not the task is done? If that's the case, changing it to a boolean(eg. isDone) will be helpful in terms of readability. It might reduce confusion as your teammates will not have to guess which variable does what when they read your code for tP/ other group projects in the future.
src/main/java/Task.java
Outdated
public String command; | ||
Task(int category, int status, String command){ | ||
this.category = category; | ||
this.status = status; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The status of the task is always set to 0 when you initialize a new Task object, so the constructor may not have to take in a parameter for status and this.status = 0; could be used instead?
src/main/java/Duke.java
Outdated
try { | ||
File myFile = new File(filePath); | ||
Scanner myReader = new Scanner(myFile); | ||
while (myReader.hasNextLine()) { | ||
String data = myReader.nextLine(); | ||
System.out.println(data); | ||
} | ||
myReader.close(); | ||
} catch (FileNotFoundException e) { | ||
System.out.println("Creating the file to save"); | ||
try { | ||
String folderPath = "./data"; | ||
Path path = Paths.get(folderPath); | ||
|
||
Files.createDirectories(path); | ||
|
||
System.out.println("Directory is created!"); | ||
|
||
//Files.createDirectory(path); | ||
|
||
} catch (IOException error) { | ||
System.err.println("Failed to create directory!" + error.getMessage()); | ||
} | ||
try { | ||
File fileCreator = new File(filePath); | ||
if (fileCreator.createNewFile()) { | ||
System.out.println("File created: " + fileCreator.getName()); | ||
} else { | ||
System.out.println("File already exists."); | ||
} | ||
} catch (IOException ex) { | ||
System.out.println("An error occurred."); | ||
e.printStackTrace(); | ||
} | ||
} | ||
try { | ||
File myFile = new File(filePath); | ||
Scanner myReader = new Scanner(myFile); | ||
while (myReader.hasNextLine()) { | ||
String data = myReader.nextLine(); | ||
System.out.println(data); | ||
} | ||
myReader.close(); | ||
} catch (FileNotFoundException e) { | ||
System.out.println("Creating the file to save"); | ||
} | ||
|
||
try { | ||
FileWriter myWriter = new FileWriter(filePath); | ||
//myWriter.write("Files in Java might be tricky, but it is fun enough!"); | ||
myWriter.close(); | ||
System.out.println("All changes successfully saved to the storage!"); | ||
} catch (IOException e) { | ||
System.out.println("An error occurred."); | ||
e.printStackTrace(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm sorry but I don't really understand this part. Would it be possible to combine all the try blocks together into one and have multiple catch blocks for different errors instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In general, the code has some further progress to make. I think you would be on the right track after trying to refactor the code as mentioned.
Potentially, you could implement an iterative approach to this. Instead of trying to solve all the problems at once, try to categorize the issues into UI related, encapsulation or abstraction issues. Try solving them one by one and it might be easier to implement in that respect since many other reviewers are also leaving their comments. Hope that helps you!
src/main/java/Duke.java
Outdated
break; | ||
} | ||
} | ||
if(exist) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are some recurring inconsistencies with usage of white space. It might be good to review to keep it consistent with the rest of the code base.
for example: if (exist) would better adhere to coding styles than if(exist)
src/main/java/Duke.java
Outdated
// A-trial run | ||
// program to be added | ||
// Find out the position of the first '/' | ||
public int firstSlashTracker (String string){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Names representing methods must be verbs and written in camelCase. For example, getName(), computeTotalWidth()
So, if the intention is for you to get the value of first slash tracker, the method name could be getFirstSlashTracker instead
src/main/java/Duke.java
Outdated
System.out.println(" Yup, this is now added! " + " [E][x] " + order.substring(5, number) + "(at: " + | ||
order.substring(number + 3) + ")"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems like you are making a line break in order to follow the rule of line length not exceeding 120 and that is great!
However, line breaks should be done before an operator. One possible way could be to change this line to the following format :
System.out.println(" Yup, this is now added! " + " [E][x] "
- order.substring(5, number) + "(at: "
- order.substring(number + 3) + ")");
the operator should be a part of the new line when wrapping lines. Hope this example clarifies 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wow! Thank you for pointing it!!
Branch a assertions
Branch a code quality
C find duplicates
Working in process