-
Notifications
You must be signed in to change notification settings - Fork 3.3k
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
Refactor StudentLogic::enrollStudents(String, String, boolean) #6557 #6609
Refactor StudentLogic::enrollStudents(String, String, boolean) #6557 #6609
Conversation
@boxin-yang there are some errors (checkstlye errors) that is causing the build to fail, could you please update it and I will set it for review when the build passes? In the mean time I am setting it to be ongoing |
… function getInvalidityInfoInEnrollLines
…ion of parameter studentList
@boxin-yang it is not necessary to merge from "update branch" especially if the build is failing! Try to fix it locally on your machine before attempting it again, it helps to not clog up the build machine |
@junhaoyap I see. Will run the local test and Checkstyle before I push in the future. Thx for the advice. |
@boxin-yang seems like it has passed this time! Is this ready to be reviewed? |
@junhaoyap Yes this PR is ready for Review |
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.
Please be careful with language implementations, Java is pass by value so studentList will remain empty at the end of line 305
Read: http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value
Without test coverage that tests for both correct / incorrect cases of what I described, I can't be 100% sure that that is what's happening, I am basing it off of the pass-by-value rule, whether it is right or wrong, I think the best route from here is to write a test case that covers it cc @wkurniawan07 to confirm the pass-by-value effects or whether I'm just conjuring crazy stuff |
It is me being crazy after all, I got confused switching around from languages to languages every day Please read instead: http://stackoverflow.com/questions/7127530/does-array-changes-in-method There is a difference between primitives and objects :-) |
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.
Other than my crazy self rambling, we are good for this :p
cc @damithc for second checking |
@junhaoyap you'd be right if the parameter is a primitive, e.g. String, Integer, but Objects are passed by reference. |
…github.com/greed-is-good/teammates into 6557-refactor-student-logic-enroll-student
@junhaoyap @damithc Ready for review |
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.
A few more things to fix. Almost there.
for (int i = 0; i < invalidInfo.size(); i++) { | ||
assertEquals(expectedInvalidInfo.get(i), invalidInfo.get(i)); | ||
private String getExceptionMessageOnCreatingStudentsList(String enrollLines, String courseId) { | ||
String invalidInfoString = null; // null will be returned if no exception is thrown |
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.
This is incorrect. Line 593 will terminate the execution if no exception is thrown.
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.
noted
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.
👍
|
||
for (int i = 0; i < invalidInfo.size(); i++) { | ||
assertEquals(expectedInvalidInfo.get(i), invalidInfo.get(i)); | ||
private String getExceptionMessageOnCreatingStudentsList(String enrollLines, String courseId) { |
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.
Write a header comment explaining what this method does. Mention that it assumes an exception will be thrown and cause a test failure if it doesn't.
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.
noted
@@ -592,12 +572,13 @@ private StudentEnrollDetails enrollStudent(StudentAttributes validStudentAttribu | |||
} | |||
|
|||
/* All empty lines or lines with only white spaces will be skipped. | |||
* The invalidity info returned are in HTML format. | |||
* The invalidity info is in HTML format. |
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.
As we are changing the method behavior, we can also fix this header comment. It should start with /**
. Describe when does it throws the exception.
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.
done
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.
Can you push the changes, we don't see the change for /**
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.
Sorry forgot to push. Just pushed
*/ | ||
private List<String> getInvalidityInfoInEnrollLines(String lines, String courseId) throws EnrollException { | ||
public List<StudentAttributes> createAndValidateStudents(String lines, String courseId) throws EnrollException { |
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 don't think Validate
should be in the method name. Methods are supposed to validate data anyway.
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.
Well, this new method combined the part on validation and creating studentList together(done separately in the original code). Also, mentioning Validate might help with understanding the purpose of this function. Meanwhile, I have explained the exception in the header comment to mention about validating. Any suggestion on this ?@wkurniawan07
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 think @damithc nailed it, this method says throws EnrollException
which pretty much tells us something is being "validated", just createStudents
is good enough
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.
Ok will update the name
I believe we will be blocked again in terms of merging again due to accountsDB failing once we merge master in.. |
…MessageOnCreatingStudentsList
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.
@damithc Ready for review
@@ -592,12 +572,13 @@ private StudentEnrollDetails enrollStudent(StudentAttributes validStudentAttribu | |||
} | |||
|
|||
/* All empty lines or lines with only white spaces will be skipped. | |||
* The invalidity info returned are in HTML format. | |||
* The invalidity info is in HTML format. |
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.
Sorry forgot to push. Just pushed
*/ | ||
private List<String> getInvalidityInfoInEnrollLines(String lines, String courseId) throws EnrollException { | ||
public List<StudentAttributes> createAndValidateStudents(String lines, String courseId) throws EnrollException { |
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.
Ok will update the name
@damithc Updated the param format in method comments as we discussed during class. Ready for review |
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.
Some comments added.
/* All empty lines or lines with only white spaces will be skipped. | ||
* The invalidity info is in HTML format. | ||
/** | ||
* This method builds the studentList from user input lines and check the validity of each student instance created. |
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.
and check the validity of each student instance created
is not needed because it is implied by the @throws
clause description.
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.
ok
/** | ||
* This method builds the studentList from user input lines and check the validity of each student instance created. | ||
* | ||
* @param lines the enrollment lines entered by the instructor.All empty lines or lines with only white |
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 should be a space after .
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 second sentence is about the method behavior, not about the parameter. It should be moved up.
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.
ok
* | ||
* @param lines the enrollment lines entered by the instructor.All empty lines or lines with only white | ||
* spaces will be skipped. | ||
* @throws EnrollException if any of the student instances created by the enrollment line is invalid.The invalidity |
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.
by the enrollment line
is not needed.
Add space after .
It's not clear what this invalidity info
you are referring to.
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.
Also, it is not clear if the exception is thrown for the first invalid student, or for all invalid students.
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.
ok
@@ -586,10 +586,18 @@ public void testEnrollLinesChecking() throws Exception { | |||
assertEquals(expectedInvalidInfoString, invalidInfoString); | |||
} | |||
|
|||
/** | |||
* This method returns the Exception message thrown when trying to build StudentList from invalid enrollment line. |
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.
No need for This method
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.
noted
private String getExceptionMessageOnCreatingStudentsList(String enrollLines, String courseId) { | ||
String invalidInfoString = null; // null will be returned if no exception is thrown |
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 parameter should be invalidEnrollLines
?
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.
yup it would be clearer
* signalFailureToDetectException(). | ||
* | ||
* @param enrollLines is assumed to be invalid | ||
* @return invalidInfoString in the Enrollment Exception in html format |
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.
invalidInfoString
is a local variable. Should not be mentioned here.
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.
noted
@@ -586,10 +586,18 @@ public void testEnrollLinesChecking() throws Exception { | |||
assertEquals(expectedInvalidInfoString, invalidInfoString); | |||
} | |||
|
|||
/** | |||
* This method returns the Exception message thrown when trying to build StudentList from invalid enrollment line. | |||
* The method assumes that an Enrollment Exception is thrown, else the method fails with |
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.
Enrollment Exception
should not have a space in the middle?
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.
noted
…t and createStudents
…github.com/greed-is-good/teammates into 6557-refactor-student-logic-enroll-student
@damithc Thx for the review and suggestion. Learnt a lot. Just made some updates and is ready for review |
* This method returns the Exception message thrown when trying to build StudentList from invalid enrollment line. | ||
* The method assumes that an Enrollment Exception is thrown, else the method fails with | ||
* signalFailureToDetectException(). | ||
* Returns the Exception message thrown when trying to build StudentList from invalid enrollment line. |
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.
Since this is describing what is returned, it can be placed in @return.
Would @return EnrollException message thrown due to invalid enrollment line in html format
be better?
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 return here is more like a 'get' for method description. @return param description is to emphasize html format.
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.
Without using 'return' here the sentence is complete and hard to understand. This method is in a way a get method, so inevitably I have to use either return or get in my method description.
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.
minor nit
* @param enrollLines is assumed to be invalid | ||
* @return invalidInfoString in the Enrollment Exception in html format | ||
* @param invalidEnrollLines is assumed to be invalid | ||
* @return the EnrollException message in html format |
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
is unnecessary
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.
thx for pointing out! done changing :)
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.
Much better now. Some more minor suggestions.
* | ||
* @param lines the enrollment lines entered by the instructor. | ||
* @throws EnrollException if some of the student instances created are invalid. The exception message contains | ||
* invalidity info for all invalid student instances in HTML format. |
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.
Looks good.
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.
:)
for (int i = 0; i < invalidInfo.size(); i++) { | ||
assertEquals(expectedInvalidInfo.get(i), invalidInfo.get(i)); | ||
/** | ||
* Returns the Exception message thrown when trying to build StudentList from invalid enrollment line. |
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.
StudentList
is an undefined term (i.e. reader has no idea what it means). I think you can instead explain that the method calls createStudents(...)
on the studentLogic
object.
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.
ok
* The method assumes that an EnrollException is thrown, else the method fails with signalFailureToDetectException(). | ||
* | ||
* @param invalidEnrollLines is assumed to be invalid | ||
* @return EnrollException message in html format |
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 html format
in not relevant here. The method simply returns whatever in the exception object.
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.
noted
for (int i = 0; i < invalidInfo.size(); i++) { | ||
assertEquals(expectedInvalidInfo.get(i), invalidInfo.get(i)); | ||
/** | ||
* Returns the Exception message thrown when trying to build StudentList from invalid enrollment line. |
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's not correct to say Exception message thrown
. The said method throws an exception, not a message.
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.
noted.
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.
A few more nits
for (int i = 0; i < invalidInfo.size(); i++) { | ||
assertEquals(expectedInvalidInfo.get(i), invalidInfo.get(i)); | ||
/** | ||
* Returns the EnrollException thrown when trying to call createStudents method on studentsLogic object with invalid |
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.
Returns the EnrollException thrown
doesn't match the return type of the method. How about Returns the error message of the EnrollException thrown ...
?
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.
noted
assertEquals(expectedInvalidInfo.get(i), invalidInfo.get(i)); | ||
/** | ||
* Returns the EnrollException thrown when trying to call createStudents method on studentsLogic object with invalid | ||
* enrollment lines. The method assumes that an EnrollException is thrown, else the method fails with |
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 be with {@code invalidEnrollLines}
(to indicate that we are referring to the parameter, not any invalid enrollment lines).
for (int i = 0; i < invalidInfo.size(); i++) { | ||
assertEquals(expectedInvalidInfo.get(i), invalidInfo.get(i)); | ||
/** | ||
* Returns the EnrollException thrown when trying to call createStudents method on studentsLogic object with invalid |
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.
Use {@code ...}
when referring to code elements. e.g. {@code studentsLogic}
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 is a javadoc syntax to refer to another method. I'm saying this in reference to createStudents
in the comment.
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 googled around and I think I should use {@link} to refer to method. Did the same for signalFailureToDetectException
* signalFailureToDetectException(). | ||
* | ||
* @param invalidEnrollLines is assumed to be invalid | ||
* @return EnrollException message |
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.
Probably no need if the same info is in the overview statement (line 590)
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.
done
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.
All good now :-)
…t list
Fixes #6557
Outline of Solution
studentList could have been built in getInvalidityInfoInEnrollLines(String, String) . Reuse the logic in getInvalidityInfoInEnrollLines to build the studentList to prevent building it twice.(and the StudentAttributes built in enrollStudents(String, String, boolean) uses String that is not sanitized. Reusing getInvalidityInfoInEnrollLines solves the issue).