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

adds random quotes to the collection selection menu #68

Closed
wants to merge 3 commits into from
Closed

adds random quotes to the collection selection menu #68

wants to merge 3 commits into from

Conversation

bjsi
Copy link
Member

@bjsi bjsi commented Feb 11, 2020

Selects a random quote from a text file of ~300 Woz quotes. The file is assumed to be kept in the SMA root directory. Let me know if this is acceptable and I will add the file.

Example screenshot:

image

@bjsi
Copy link
Member Author

bjsi commented Feb 11, 2020

@bjsi bjsi linked an issue Feb 11, 2020 that may be closed by this pull request
@alexis- alexis- self-requested a review February 12, 2020 06:56
Copy link
Member

@alexis- alexis- left a comment

Choose a reason for hiding this comment

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

There are a few issues with your code, can you please commit a fix ?

Beautiful job on pulling all the Woz quotes, very creative.


if (QuoteFile.Exists())
{
var Lines = File.ReadAllLines(QuoteFile.FullPath);
Copy link
Member

@alexis- alexis- Feb 12, 2020

Choose a reason for hiding this comment

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

There are several scenarios where File.ReadAllLines could throw an exception, can you please:

  • handle FileIOException with a LogTo.Warning,
  • handle Exception (fallback) with a LogTo.Error (this will file a bug report as well as log to file)
  • let the try... catch encompass the whole if code branch (under the if), for safety
  • gracefully continue the code if an exception is thrown (don't rethrow or exit)

// Field 1 = quote author
// Field 2 = quote URL
// Field 3 = quote title
if (!SplitQuoteLine[0].EndsWith(".")
Copy link
Member

Choose a reason for hiding this comment

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

This will cause an error if the file is empty

@bjsi
Copy link
Member Author

bjsi commented Feb 14, 2020

Ok @alexis- I added exception handling and a check for an empty file. I also added a hyperlink to the title of the quote. I changed the file to a .tsv because github can prettify .tsv files - maybe this can allow the community to add some of their own favourite Woz quotes or quotes from supermemo lore / the discord.

Here is a link to the updated quotes file: https://github.com/bjsi/Random-Woz-Quote/blob/master/quotes.tsv

@alexis- alexis- added this to the SMA 2.1.0 "Open-Beta" milestone Feb 15, 2020
Copy link
Member

@alexis- alexis- left a comment

Choose a reason for hiding this comment

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

Excellent. You only need to fix the possible exception I mentioned and then this is all good as far as I'm concerned.

// Field 1: Author
// Field 2: Url
// Field 3: Title
if (!SplitQuoteLine[0].EndsWith(".")
Copy link
Member

@alexis- alexis- Feb 16, 2020

Choose a reason for hiding this comment

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

Just nitpicking, but you could implement the condition this way to make the code more easy to read and maintain:

private static readonly string[] SentenceEndingPunctuation = new[] { ".", "!", "?" };
[...]
if (SentenceEndingPunctuation[0].None(p => SplitLineQuoteLine.EndsWith(p)))

}

QuoteBodyTextBlock.Text = "\"" + SplitQuoteLine[0] + "\"";
QuoteAuthorTextBlock.Text = SplitQuoteLine[1];
Copy link
Member

Choose a reason for hiding this comment

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

That and subsequent array accesses [2] [3] will throw an exception if QuoteLine isn't correctly formatted (i.e. if it isn't a \t separated line as you expect).

It is very important to always assume your inputs are going to be somehow faulty (or even malicious), and to add the necessary code to deal with that.

SplitQuoteLine[0] += ".";
}

QuoteBodyTextBlock.Text = "\"" + SplitQuoteLine[0] + "\"";
Copy link
Member

Choose a reason for hiding this comment

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

FYI, there is a neat feature in C# called string interpolation. It's not very useful in this case, but often it will make it easier to read strings with embedded variables. Either:

  • $"\"{SplitQuoteLine[0]}\""
  • $@"""{SplitQuoteLine[0]}""" (double "" equals escaped ")

@alexis-
Copy link
Member

alexis- commented Mar 12, 2020

Can you merge upstream to resolve the possible conflict, and add the clause for the possible exception so we can add this feature to next version ?

@bjsi
Copy link
Member Author

bjsi commented Mar 15, 2020

Added suggestions here -> #142

@bjsi bjsi closed this Mar 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Development

Successfully merging this pull request may close these issues.

Quote of the day
2 participants