-
Notifications
You must be signed in to change notification settings - Fork 21
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
Conversation
quotes.csv is here: https://github.com/bjsi/Random-Woz-Quote/blob/master/quotes.csv |
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 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); |
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 several scenarios where File.ReadAllLines
could throw an exception, can you please:
- handle
FileIOException
with aLogTo.Warning
, - handle
Exception
(fallback) with aLogTo.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(".") |
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 will cause an error if the file is empty
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 |
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.
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(".") |
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.
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]; |
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.
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] + "\""; |
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.
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 ")
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 ? |
Added suggestions here -> #142 |
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: