Skip to content
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

Conversation

boxin-yang
Copy link
Contributor

…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).

@junhaoyap
Copy link

@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

@junhaoyap junhaoyap added the s.Ongoing The PR is being worked on by the author(s) label Feb 16, 2017
@junhaoyap
Copy link

@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

@boxin-yang
Copy link
Contributor Author

@junhaoyap I see. Will run the local test and Checkstyle before I push in the future. Thx for the advice.

@junhaoyap
Copy link

@boxin-yang seems like it has passed this time! Is this ready to be reviewed?

@boxin-yang
Copy link
Contributor Author

@junhaoyap Yes this PR is ready for Review

@junhaoyap junhaoyap self-assigned this Mar 3, 2017
@junhaoyap junhaoyap self-requested a review March 3, 2017 13:39
@junhaoyap junhaoyap added s.ToReview The PR is waiting for review(s) and removed s.Ongoing The PR is being worked on by the author(s) labels Mar 3, 2017
Copy link

@junhaoyap junhaoyap left a 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

@junhaoyap junhaoyap added s.Ongoing The PR is being worked on by the author(s) and removed s.ToReview The PR is waiting for review(s) labels Mar 3, 2017
@junhaoyap
Copy link

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

@junhaoyap
Copy link

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 :-)

@junhaoyap junhaoyap added s.ToReview The PR is waiting for review(s) and removed s.Ongoing The PR is being worked on by the author(s) labels Mar 3, 2017
Copy link

@junhaoyap junhaoyap left a 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

@junhaoyap junhaoyap added s.FinalReview The PR is ready for final review and removed s.ToReview The PR is waiting for review(s) labels Mar 3, 2017
@junhaoyap
Copy link

cc @damithc for second checking

@wkurniawan07
Copy link
Member

@junhaoyap you'd be right if the parameter is a primitive, e.g. String, Integer, but Objects are passed by reference.
@boxin-yang nevertheless, this is not a really good way to solve the issue. I'll explain why soon.

@boxin-yang
Copy link
Contributor Author

@junhaoyap @damithc Ready for review

Copy link
Contributor

@damithc damithc left a 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
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

noted

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) {
Copy link
Contributor

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.

Copy link
Contributor Author

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.
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

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 /**

Copy link
Contributor Author

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 {
Copy link
Contributor

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.

Copy link
Contributor Author

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

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

Copy link
Contributor Author

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

@junhaoyap
Copy link

I believe we will be blocked again in terms of merging again due to accountsDB failing once we merge master in..

Copy link
Contributor Author

@boxin-yang boxin-yang left a 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.
Copy link
Contributor Author

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 {
Copy link
Contributor Author

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

@boxin-yang
Copy link
Contributor Author

@damithc Updated the param format in method comments as we discussed during class. Ready for review

Copy link
Contributor

@damithc damithc left a 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.
Copy link
Contributor

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.

Copy link
Contributor Author

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
Copy link
Contributor

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 .

Copy link
Contributor

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.

Copy link
Contributor Author

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
Copy link
Contributor

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.

Copy link
Contributor

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.

Copy link
Contributor Author

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.
Copy link
Contributor

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

Copy link
Contributor Author

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
Copy link
Contributor

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?

Copy link
Contributor Author

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
Copy link
Contributor

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.

Copy link
Contributor Author

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
Copy link
Contributor

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

noted

@boxin-yang
Copy link
Contributor Author

@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.
Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Contributor Author

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.

Copy link
Contributor

@ablyx ablyx left a 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the is unnecessary

Copy link
Contributor Author

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 :)

Copy link
Contributor

@damithc damithc left a 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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good.

Copy link
Contributor Author

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.
Copy link
Contributor

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.

Copy link
Contributor Author

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
Copy link
Contributor

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.

Copy link
Contributor Author

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.
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

noted.

@boxin-yang
Copy link
Contributor Author

@damithc Ready for review with suggested change. @ablyx I just updated the method description again. Do you think it is clearer this time round? (I have to mention return enrollException thrown... to explain what the method is doing, can't think of a better way to describe the method)

Copy link
Contributor

@damithc damithc left a 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
Copy link
Contributor

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 ...?

Copy link
Contributor Author

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
Copy link
Contributor

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
Copy link
Contributor

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}

Copy link
Contributor

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.

Copy link
Contributor Author

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
Copy link
Contributor

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)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

getExceptionMessageOnCreatingStudentsList and createStudents
@boxin-yang
Copy link
Contributor Author

@damithc Googled around and learnt to use {@link} and {@code}. Also updated the method comments for createStudents

Copy link
Contributor

@damithc damithc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All good now :-)

@damithc damithc added s.ToMerge The PR is approved by all reviewers including final reviewer; ready for merging and removed s.ToReview The PR is waiting for review(s) labels Mar 9, 2017
@wkurniawan07 wkurniawan07 merged commit d8ae0a3 into TEAMMATES:master Mar 9, 2017
@wkurniawan07 wkurniawan07 added this to the V5.99 milestone Mar 9, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
s.ToMerge The PR is approved by all reviewers including final reviewer; ready for merging
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants