Skip to content

Conversation

@mdeimel
Copy link
Collaborator

@mdeimel mdeimel commented Nov 20, 2018

No description provided.




public static final List<String> ALL_STUDY_GUIDES = Arrays.asList(
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Two things here:

  1. A List would be the preferred approach here. Although a Set does work, the data lends itself closer to a List, partly because you are actually listing it out here, and the List type will keep the order when you process the results, unlike the Set.
  2. As of Java 8 you can create an inline List by using the Arrays.asList() method, which will take a variable length of parameters, and return them all in a List object.

@@ -1,232 +0,0 @@
package com.base64.d;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Other than the name change, this file was changed in the following ways:

  1. Remove all commented out code that is not needed.
  2. Remove blank lines when there are more than one consecutive blank lines.
  3. Cleanup import statements, by removing unused imports.


public class WriteToFile {

String date = new Date().toString();
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

By defining the object here, you are specifying an instance variable. This will be set for each instance of WriteToFile. You typically want to push variables into the smallest scope possible. The scope going from "class" to "instance" to "method". So I made this a method level variable by placing it inside the method. This keeps the variables as close to the place that they are used.

// Find all links within a Study Guide
try {
//
// Connect to the study guide url
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It's good to have code comments, especially any area where the functionality is not obvious. The double forward slashes for these comments appear to be created with the "comment out code" shortcut in IntelliJ, which starts the slashes at the first character of the line. This is more useful when commenting out a block of code (to really help draw attention to the full block that is commented out, since all of the characters are on the left side of the screen, which is usually blank). What is preferred for code comments is to have the slashes justified with the rest of the code. You can see below how the two forward slashes have been moved to be more inline with the code.

Document wraperDocument = Jsoup.connect(studyGuideLink).get();

// grab the iframe
Element iframe = doc3.select("iframe").first();
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

A lot of the variables have more descriptive names, which is helpful. I've renamed doc3 and link2 to be more descriptive of what they will actually hold. Good naming is a best practice in general, and is really helpful when others read your code.

String jsScripts = scriptTag.toString();

// match any string of characters over 100 in length.
String patternString = "(\\w{100,}.+)";
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This regex definitely works, and although it is unlikely to have more than 100 characters together in this class, it would be considered brittle, because some data could be included that would cause problems.
I've changed it to match on the JavaScript variable name, and then reference the double quotes that will be around this object. This would be considered more robust because it is much less likely to fail if the data changes (it's unlikely that a second window.courseData object will be created in this class).

Copy link
Owner

Choose a reason for hiding this comment

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

I couldn't get either version of that regex to work, so have left the 100 character one.

}


// Test all links in a Study Guide
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

There are two big blocks of code in this method. And at first I was confused what they were doing, so I just added a comment before the second block here, and also the block above. This is the type of thing that could be extracted into separate methods, to improve readability. But this would also require restructuring the code, because it currently is gathering all of the links in a study guide (first block), and then processing all links for the study guide (second block). It's possible that restructuring might be to check each link after it is retrieved (and just keep track of links that have already been checked, to prevent checking duplicates).

.statusCode();
// Although the `linkContents` is never utilized, it is a
// required call. This is because Jsoup expects that `get()`
// is called before the `response()` object is available.
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Because I've been cleaning deleting all unused variables, I added a comment here to provide an explanation for why this one exists. Since I initially deleted this variable and then found that response() is never populated unless you first call get(). Which I then remembered we talked about over IM a while back 🤦 :haha:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants