2
\$\begingroup\$

After my Dialogue parser has parsed a dialogue, it needs to be validated. To that end, I have several checker classes that go through the lines of the Dialogue.

These checker classes get huge as I add more and more features to them. Eventually they get intertwined with checks that they're not really meant to be doing, and I end up with functions the size of 400 lines. This is one of them, and it's the one that I think is easiest to follow.

First off, my Issue class, for storing the "error message":

public class Issue {
    private var severity:Severity; //See the Severity Enum Class for more details. 
    private var snippet:String;
    private var message:String;
    private var type:String;
    private var relatedLine:Line;

    public function Issue(issueSeverity:Severity, concernedLine:Line, issueSnippet:String, issueMessage:String, issueType:String) {
        severity = issueSeverity;
        relatedLine = concernedLine;
        message = issueMessage;
        type = issueType;
        snippet = issueSnippet;
    }

    public function getRelatedLineNumber():uint {
        return relatedLine.getLineNumber();
    }

    public function getSeverity():Severity {
        return severity;
    }

    public function getSnippet():String {
        return snippet;
    }

    public function getType():String {
        return type;
    }

    public function getMessage():String {
        return message;
    }

    public function printMessage():String {
        return type + " - " + severity.getIDStandardCapitalization() + ": " + message;
    }

    /**
     * For tracing purposes. It works, but it's not nice.
     * @return output regarding the issue.
     */
    public function _debugPrint():String {
        return severity.getID() + " " + type + " Issue relating to Line " + getRelatedLineNumber() + "\n" + "Line " + getRelatedLineNumber() + ": " + relatedLine.getLineAsString() + "\nIssue entails: " + message;
    }
}

Printing this message is handled by another class, which is out of scope for this question.

A line looks like this:

lineName:"content"

The GrammarChecker is responsible for checking if the "content" section of the line adheres somewhat to grammar... I can't make any assumptions as to whether the content is English, French, German, Spanish... well, I do know most of them will use a latin character set.

Most of the checks, as you'll see are the same.

The checkLineOldStyle function, responsible for validating a line as a string:

    private function checkLineOldStyle(l:Line, lineString:String, varsubsReplaced:Boolean = false, dialogue:Dialogue = null, options:Options = null):void {
        if (l.isCommentLine()) {
            if (options == null || options.getSetting("commentfilter")) {
                return;
            }
        }

        var linebreak:String = "";
        if (options != null) {
            linebreak = options.getLineBreak();
        } else {
            linebreak = "\r\n";
        }

        var line:String = l.getRaw();
        var showStrippedVersion:Boolean = false;
        var issue:Issue = null;
        var issueMessageDefault:String = "";
        var lineContainsYOU:Boolean = (l.getRaw().indexOf("YOU") != -1);
        if (lineString != "") {
            if (options != null) {
                if (options.getSetting("substitutes")) {
                    if (varsubsReplaced) {
                        if (lineContainsYOU) {
                            issueMessageDefault = "The following issue was found after replacing variable substitutions, and stripping the line of delay characters and triggers." + linebreak;
                            issueMessageDefault += "Keep in mind that you can place characters in YOU/YOUR/ME/MY variable insertions, like \"Hello*, YOU*!\"." + linebreak;
                        } else {
                            issueMessageDefault = "The following issue was found after replacing variable substitutions, and stripping the line of delay characters and triggers:" + linebreak;
                        }
                    } else {
                        if (lineContainsYOU) {
                            issueMessageDefault = "The following issue was found after stripping the line of delay characters, triggers and variable substitutions." + linebreak;
                            issueMessageDefault += "Keep in mind that you can place characters in YOU/YOUR/ME/MY variable insertions, like \"Hello*, YOU*!\"." + linebreak;
                        } else {
                            issueMessageDefault = "The following issue was found after stripping the line of delay characters, triggers and variable substitutions:" + linebreak;
                        }

                    }
                } else {
                    issueMessageDefault = "The following issue was found after stripping the line of delay characters and triggers:" + linebreak;
                }
            } else {
                if (lineContainsYOU) {
                    issueMessageDefault = "The following issue was found after stripping the line of delay characters, triggers and variable substitutions." + linebreak;
                    issueMessageDefault += "Keep in mind that you can place characters in YOU/YOUR/ME/MY variable insertions, like \"Hello*, YOU*!\"." + linebreak;
                } else {
                    issueMessageDefault = "The following issue was found after stripping the line of delay characters, triggers and variable substitutions:" + linebreak;
                }
            }
            line = lineString;
            showStrippedVersion = true;
        }
        var issueMessage:String = issueMessageDefault;
        var issueSnippet:String = "";
        if (line.indexOf("\"") != -1 && line.indexOf("\"") == line.indexOf("\",")) {
            issueSnippet = StringFunctions.getSnippet(line, "\",");
            issueMessage += "Sentence starting with a comma (grammatical error) on dialogue line on line " + l.getLineNumber() + ", col " + (line.indexOf("\",")) + " near \"" + issueSnippet + "\"" + linebreak;
            issueMessage += l.printLineMessage(linebreak);
            if (showStrippedVersion) {
                issueMessage += "Stripped Line " + l.getLineNumber() + ": " + lineString + linebreak;
            }
            issueMessage += linebreak;
            issue = new Issue(Severity.getEnum("MINOR"), l, issueSnippet, issueMessage, "Grammar");
            addIssue(issue);
            issueMessage = issueMessageDefault;
        }
        if (line.indexOf("\"") != -1 && line.indexOf("\"") == line.indexOf("\"! ")) {
            issueSnippet = StringFunctions.getSnippet(line, "\"! ");
            issueMessage += "Sentence starting with a disconnected exclamation mark (grammatical error) on dialogue line on line " + l.getLineNumber() + ", col " + (line.indexOf("\"! ")) + " near \"" + issueSnippet + "\"" + linebreak;
            issueMessage += l.printLineMessage(linebreak);
            if (showStrippedVersion) {
                issueMessage += "Stripped Line " + l.getLineNumber() + ": " + lineString + linebreak;
            }
            issueMessage += linebreak;
            issue = new Issue(Severity.getEnum("MINOR"), l, issueSnippet, issueMessage, "Grammar");
            addIssue(issue);
            issueMessage = issueMessageDefault;
        }
        if (line.indexOf("\"") != -1 && line.indexOf("\"") == line.indexOf("\". ")) {
            issueSnippet = StringFunctions.getSnippet(line, "\". ");
            issueMessage += "Sentence starting with a disconnected period (grammatical error) on dialogue line on line " + l.getLineNumber() + ", col " + (line.indexOf("\". ")) + " near \"" + issueSnippet + "\"" + linebreak;
            issueMessage += l.printLineMessage(linebreak);
            if (showStrippedVersion) {
                issueMessage += "Stripped Line " + l.getLineNumber() + ": " + lineString + linebreak;
            }
            issueMessage += linebreak;
            issue = new Issue(Severity.getEnum("MINOR"), l, issueSnippet, issueMessage, "Grammar");
            addIssue(issue);
            issueMessage = issueMessageDefault;
        }
        if (line.indexOf(" ,") != -1) {
            issueSnippet = StringFunctions.getSnippet(line, " ,");
            issueMessage += "Space before comma (grammatical error) on dialogue line on line " + l.getLineNumber() + ", col " + (line.indexOf(" ,")) + " near \"" + issueSnippet + "\"" + linebreak;
            issueMessage += l.printLineMessage(linebreak);
            if (showStrippedVersion) {
                issueMessage += "Stripped Line " + l.getLineNumber() + ": " + lineString + linebreak;
            }
            issueMessage += linebreak;
            issue = new Issue(Severity.getEnum("MINOR"), l, issueSnippet, issueMessage, "Grammar");
            addIssue(issue);
            issueMessage = issueMessageDefault;
        }
        if (line.indexOf(",,") != -1) {
            issueSnippet = StringFunctions.getSnippet(line, ",,");
            issueMessage += "Double comma (grammatical error) on dialogue line on line " + l.getLineNumber() + ", col " + (line.indexOf(",,")) + " near \"" + issueSnippet + "\"" + linebreak;
            issueMessage += l.printLineMessage(linebreak);
            if (showStrippedVersion) {
                issueMessage += "Stripped Line " + l.getLineNumber() + ": " + lineString + linebreak;
            }
            issueMessage += linebreak;
            issue = new Issue(Severity.getEnum("MINOR"), l, issueSnippet, issueMessage, "Grammar");
            addIssue(issue);
            issueMessage = issueMessageDefault;
        }
        if (line.indexOf("  ") != -1 && (options == null || !options.getSetting("NoDoubleSpace"))) {
            var splitLine:Array = line.split("\"");
            var doubleSpaceInBadLocation:Boolean = false;
            for (var i:uint = 1, isize:uint = splitLine.length; i < isize; i += 2) { //after first quotation mark, after third, after 5th... blah"HERE blah blah" fff {"HEREset":{"HEREvar":"HEREvalue"}}
                if (splitLine[i].indexOf("  ") != -1) {
                    doubleSpaceInBadLocation = true;
                }
            }
            if (doubleSpaceInBadLocation) { //to prevent lines with whitespace in excess from tripping errors
                issueSnippet = StringFunctions.getSnippet(line, "  ");
                issueMessage += "Double space on line " + l.getLineNumber() + ", col " + (line.indexOf("  ")) + " near \"" + issueSnippet + "\"" + linebreak;
                issueMessage += l.printLineMessage(linebreak);
                if (showStrippedVersion) {
                    issueMessage += "Stripped Line " + l.getLineNumber() + ": " + lineString + linebreak;
                }
                issueMessage += linebreak;
                issue = new Issue(Severity.getEnum("MINOR"), l, issueSnippet, issueMessage, "Grammar");
                addIssue(issue);
                issueMessage = issueMessageDefault;
            }
        }
        if (line.indexOf(" !") != -1 && !l.isIndexInSubstitution(line.indexOf(" !"))) {
            issueSnippet = StringFunctions.getSnippet(line, " !");
            issueMessage += "Grammatical error (space before exclamation mark) on line " + l.getLineNumber() + ", col " + (line.indexOf(" !")) + " near \"" + issueSnippet + "\"" + linebreak;
            issueMessage += l.printLineMessage(linebreak);
            if (showStrippedVersion) {
                issueMessage += "Stripped Line " + l.getLineNumber() + ": " + lineString + linebreak;
            }
            issueMessage += linebreak;
            issue = new Issue(Severity.getEnum("MINOR"), l, issueSnippet, issueMessage, "Grammar");
            addIssue(issue);
            issueMessage = issueMessageDefault;
        }
        if (line.indexOf(" ?") != -1 && !l.isIndexInSubstitution(line.indexOf(" ?"))) {
            issueSnippet = StringFunctions.getSnippet(line, " ?");
            issueMessage += "Grammatical error (space before question mark) on line " + l.getLineNumber() + ", col " + (line.indexOf(" ?")) + " near \"" + issueSnippet + "\"" + linebreak;
            issueMessage += l.printLineMessage(linebreak);
            if (showStrippedVersion) {
                issueMessage += "Stripped Line " + l.getLineNumber() + ": " + lineString + linebreak;
            }
            issueMessage += linebreak;
            issue = new Issue(Severity.getEnum("MINOR"), l, issueSnippet, issueMessage, "Grammar");
            addIssue(issue);
            issueMessage = issueMessageDefault;
        }
        if (line.indexOf(",!") != -1) {
            issueSnippet = StringFunctions.getSnippet(line, ",!");
            issueMessage += "Grammatical error (comma directly before exclamation mark) on line " + l.getLineNumber() + ", col " + (line.indexOf(",!")) + " near \"" + issueSnippet + "\"" + linebreak;
            issueMessage += l.printLineMessage(linebreak);
            if (showStrippedVersion) {
                issueMessage += "Stripped Line " + l.getLineNumber() + ": " + lineString + linebreak;
            }
            issueMessage += linebreak;
            issue = new Issue(Severity.getEnum("MINOR"), l, issueSnippet, issueMessage, "Grammar");
            addIssue(issue);
            issueMessage = issueMessageDefault;
        }
        if (line.indexOf(",?") != -1) {
            issueSnippet = StringFunctions.getSnippet(line, ",?");
            issueMessage += "Grammatical error (comma directly before question mark) on line " + l.getLineNumber() + ", col " + (line.indexOf(",?")) + " near \"" + issueSnippet + "\"" + linebreak;
            issueMessage += l.printLineMessage(linebreak);
            if (showStrippedVersion) {
                issueMessage += "Stripped Line " + l.getLineNumber() + ": " + lineString + linebreak;
            }
            issueMessage += linebreak;
            issue = new Issue(Severity.getEnum("MINOR"), l, issueSnippet, issueMessage, "Grammar");
            addIssue(issue);
            issueMessage = issueMessageDefault;
        }
        if (line.indexOf("!,") != -1) {
            issueSnippet = StringFunctions.getSnippet(line, "!,");
            issueMessage += "Grammatical error (comma directly after exclamation mark) on line " + l.getLineNumber() + ", col " + (line.indexOf("!,")) + " near \"" + issueSnippet + "\"" + linebreak;
            issueMessage += l.printLineMessage(linebreak);
            if (showStrippedVersion) {
                issueMessage += "Stripped Line " + l.getLineNumber() + ": " + lineString + linebreak;
            }
            issueMessage += linebreak;
            issue = new Issue(Severity.getEnum("MINOR"), l, issueSnippet, issueMessage, "Grammar");
            addIssue(issue);
            issueMessage = issueMessageDefault;
        }
        if (line.indexOf("?,") != -1) {
            issueSnippet = StringFunctions.getSnippet(line, "?,");
            issueMessage += "Grammatical error (comma directly after question mark) on line " + l.getLineNumber() + ", col " + (line.indexOf("?,")) + " near \"" + issueSnippet + "\"" + linebreak;
            issueMessage += l.printLineMessage(linebreak);
            if (showStrippedVersion) {
                issueMessage += "Stripped Line " + l.getLineNumber() + ": " + lineString + linebreak;
            }
            issueMessage += linebreak;
            issue = new Issue(Severity.getEnum("MINOR"), l, issueSnippet, issueMessage, "Grammar");
            addIssue(issue);
            issueMessage = issueMessageDefault;
        }
        if (line.indexOf(".,.") != -1) {
            issueSnippet = StringFunctions.getSnippet(line, ".,.");
            issueMessage += "Grammatical error (comma amidst periods: .,.) on line " + l.getLineNumber() + ", col " + (line.indexOf(".,.")) + " near \"" + issueSnippet + "\"" + linebreak;
            issueMessage += l.printLineMessage(linebreak);
            if (showStrippedVersion) {
                issueMessage += "Stripped Line " + l.getLineNumber() + ": " + lineString + linebreak;
            }
            issueMessage += linebreak;
            issue = new Issue(Severity.getEnum("MINOR"), l, issueSnippet, issueMessage, "Grammar");
            addIssue(issue);
            issueMessage = issueMessageDefault;
        } else {
            if (line.indexOf(".,") != -1) {
                if ((line.indexOf("...,") + 2) != line.indexOf(".,")) {
                    issueSnippet = StringFunctions.getSnippet(line, ".,");
                    issueMessage += "Grammatical error (period before comma ending: .,) on line " + l.getLineNumber() + ", col " + (line.indexOf(".,")) + " near \"" + issueSnippet + "\"" + linebreak;
                    issueMessage += l.printLineMessage(linebreak);
                    if (showStrippedVersion) {
                        issueMessage += "Stripped Line " + l.getLineNumber() + ": " + lineString + linebreak;
                    }
                    issueMessage += linebreak;
                    issue = new Issue(Severity.getEnum("MINOR"), l, issueSnippet, issueMessage, "Grammar");
                    addIssue(issue);
                    issueMessage = issueMessageDefault;
                }
            } else if (line.indexOf(",.") != -1) {
                if (line.indexOf(",...") != line.indexOf(",.")) {
                    issueSnippet = StringFunctions.getSnippet(line, ",.");
                    issueMessage += "Grammatical error (comma before period ending: ,.) on line " + l.getLineNumber() + ", col " + (line.indexOf(".,")) + " near \"" + issueSnippet + "\"" + linebreak;
                    issueMessage += l.printLineMessage(linebreak);
                    if (showStrippedVersion) {
                        issueMessage += "Stripped Line " + l.getLineNumber() + ": " + lineString + linebreak;
                    }
                    issueMessage += linebreak;
                    issue = new Issue(Severity.getEnum("MINOR"), l, issueSnippet, issueMessage, "Grammar");
                    addIssue(issue);
                    issueMessage = issueMessageDefault;
                }
            }
        }
        if (line.indexOf(".!.") != -1) {
            issueSnippet = StringFunctions.getSnippet(line, ".!.");
            issueMessage += "Grammatical error (Exclamation mark amidst periods: .!.) on line " + l.getLineNumber() + ", col " + (line.indexOf(".!.")) + " near \"" + issueSnippet + "\"" + linebreak;
            issueMessage += l.printLineMessage(linebreak);
            if (showStrippedVersion) {
                issueMessage += "Stripped Line " + l.getLineNumber() + ": " + lineString + linebreak;
            }
            issueMessage += linebreak;
            issue = new Issue(Severity.getEnum("MINOR"), l, issueSnippet, issueMessage, "Grammar");
            addIssue(issue);
            issueMessage = issueMessageDefault;
        } else {
            if (line.indexOf("!.") != -1) {
                if (line.indexOf("!...") != line.indexOf("!.")) {
                    issueSnippet = StringFunctions.getSnippet(line, "!.");
                    issueMessage += "Grammatical error (double sentence ending: !.) on line " + l.getLineNumber() + ", col " + (line.indexOf("!.")) + " near \"" + issueSnippet + "\"" + linebreak;
                    issueMessage += l.printLineMessage(linebreak);
                    if (showStrippedVersion) {
                        issueMessage += "Stripped Line " + l.getLineNumber() + ": " + lineString + linebreak;
                    }
                    issueMessage += linebreak;
                    issue = new Issue(Severity.getEnum("MINOR"), l, issueSnippet, issueMessage, "Grammar");
                    addIssue(issue);
                    issueMessage = issueMessageDefault;
                }
            }
            if (line.indexOf(".!") != -1) {
                if ((line.indexOf("...!") + 2) != line.indexOf(".!")) {
                    issueSnippet = StringFunctions.getSnippet(line, ".!");
                    issueMessage += "Grammatical error (double sentence ending: .!) on line " + l.getLineNumber() + ", col " + (line.indexOf(".!")) + " near \"" + issueSnippet + "\"" + linebreak;
                    issueMessage += l.printLineMessage(linebreak);
                    if (showStrippedVersion) {
                        issueMessage += "Stripped Line " + l.getLineNumber() + ": " + lineString + linebreak;
                    }
                    issueMessage += linebreak;
                    issue = new Issue(Severity.getEnum("MINOR"), l, issueSnippet, issueMessage, "Grammar");
                    addIssue(issue);
                    issueMessage = issueMessageDefault;
                }
            }
        }
        if (line.indexOf(".?.") != -1) {
            issueSnippet = StringFunctions.getSnippet(line, ".?.");
            issueMessage += "Grammatical error (Question mark amidst periods: .?.) on line " + l.getLineNumber() + ", col " + (line.indexOf(".?.")) + " near \"" + issueSnippet + "\"" + linebreak;
            issueMessage += l.printLineMessage(linebreak);
            if (showStrippedVersion) {
                issueMessage += "Stripped Line " + l.getLineNumber() + ": " + lineString + linebreak;
            }
            issueMessage += linebreak;
            issue = new Issue(Severity.getEnum("MINOR"), l, issueSnippet, issueMessage, "Grammar");
            addIssue(issue);
            issueMessage = issueMessageDefault;
        } else {
            if (line.indexOf("?.") != -1) {
                if (line.indexOf("?...") != line.indexOf("?.")) {
                    issueSnippet = StringFunctions.getSnippet(line, "?.");
                    issueMessage += "Grammatical error (double sentence ending: ?.) on line " + l.getLineNumber() + ", col " + (line.indexOf("?.")) + " near \"" + issueSnippet + "\"" + linebreak;
                    issueMessage += l.printLineMessage(linebreak);
                    if (showStrippedVersion) {
                        issueMessage += "Stripped Line " + l.getLineNumber() + ": " + lineString + linebreak;
                    }
                    issueMessage += linebreak;
                    issue = new Issue(Severity.getEnum("MINOR"), l, issueSnippet, issueMessage, "Grammar");
                    addIssue(issue);
                    issueMessage = issueMessageDefault;
                }
            }
            if (line.indexOf(".?") != -1) {
                if ((line.indexOf("...?") + 2) != line.indexOf(".?")) {
                    issueSnippet = StringFunctions.getSnippet(line, ".?");
                    issueMessage += "Grammatical error (double sentence ending: .?) on line " + l.getLineNumber() + ", col " + (line.indexOf(".?")) + " near \"" + issueSnippet + "\"" + linebreak;
                    issueMessage += l.printLineMessage(linebreak);
                    if (showStrippedVersion) {
                        issueMessage += "Stripped Line " + l.getLineNumber() + ": " + lineString + linebreak;
                    }
                    issueMessage += linebreak;
                    issue = new Issue(Severity.getEnum("MINOR"), l, issueSnippet, issueMessage, "Grammar");
                    addIssue(issue);
                    issueMessage = issueMessageDefault;
                }
            }
        }

        if (line.indexOf("%0A ") != -1) {
            issueSnippet = StringFunctions.getSnippet(line, "%0A ");
            issueMessage += "Space at start of new line on line " + l.getLineNumber() + ", col " + (line.indexOf("%0A ")) + " near \"" + issueSnippet + "\"" + linebreak;
            issueMessage += l.printLineMessage(linebreak);
            if (showStrippedVersion) {
                issueMessage += "Stripped Line " + l.getLineNumber() + ": " + lineString + linebreak;
            }
            issueMessage += linebreak;
            issue = new Issue(Severity.getEnum("MINOR"), l, issueSnippet, issueMessage, "Grammar");
            addIssue(issue);
            issueMessage = issueMessageDefault;
        }
        if (line.indexOf(":\" ") != -1) {
            issueSnippet = StringFunctions.getSnippet(line, ":\" ");
            issueMessage += "Space at start of new line on line " + l.getLineNumber() + ", col " + (line.indexOf(":\" ")) + " near \"" + issueSnippet + "\"" + linebreak;
            issueMessage += l.printLineMessage(linebreak);
            if (showStrippedVersion) {
                issueMessage += "Stripped Line " + l.getLineNumber() + ": " + lineString + linebreak;
            }
            issueMessage += linebreak;
            issue = new Issue(Severity.getEnum("MINOR"), l, issueSnippet, issueMessage, "Grammar");
            addIssue(issue);
            issueMessage = issueMessageDefault;
        }
        if (line.indexOf(" . ") != -1) {
            issueSnippet = StringFunctions.getSnippet(line, " . ");
            issueMessage += "Space before period for new line on line " + l.getLineNumber() + ", col " + (line.indexOf(" . ")) + " near \"" + issueSnippet + "\"" + linebreak;
            issueMessage += l.printLineMessage(linebreak);
            if (showStrippedVersion) {
                issueMessage += "Stripped Line " + l.getLineNumber() + ": " + lineString + linebreak;
            }
            issueMessage += linebreak;
            issue = new Issue(Severity.getEnum("MINOR"), l, issueSnippet, issueMessage, "Grammar");
            addIssue(issue);
            issueMessage = issueMessageDefault;
        }
        if (issue == null) {
            if (lineString == "") {
                if (options == null) {
                    checkLineOldStyle(l, l.getLineStripped(true, true, true), false, dialogue, options);
                } else {
                    checkLineOldStyle(l, l.getLineStripped(true, true, options.getSetting("substitutes")), false, dialogue, options);
                }
            } else if (!varsubsReplaced) {
                if (options == null || options.getSetting("substitutes")) {
                    checkLineOldStyle(l, l.getLineStrippedLiteralSubs(true, true), true, dialogue, options);
                }
            }
        }
    }

My issue with this code is that I have a lot of repetition like this:

        if (line.indexOf("\"") != -1 && line.indexOf("\"") == line.indexOf("\". ")) {
            issueSnippet = StringFunctions.getSnippet(line, "\". ");
            issueMessage += "Sentence starting with a disconnected period (grammatical error) on dialogue line on line " + l.getLineNumber() + ", col " + (line.indexOf("\". ")) + " near \"" + issueSnippet + "\"" + linebreak;
            issueMessage += l.printLineMessage(linebreak);
            if (showStrippedVersion) {
                issueMessage += "Stripped Line " + l.getLineNumber() + ": " + lineString + linebreak;
            }
            issueMessage += linebreak;
            issue = new Issue(Severity.getEnum("MINOR"), l, issueSnippet, issueMessage, "Grammar");
            addIssue(issue);
            issueMessage = issueMessageDefault;
        }
        if (line.indexOf(" ,") != -1) {
            issueSnippet = StringFunctions.getSnippet(line, " ,");
            issueMessage += "Space before comma (grammatical error) on dialogue line on line " + l.getLineNumber() + ", col " + (line.indexOf(" ,")) + " near \"" + issueSnippet + "\"" + linebreak;
            issueMessage += l.printLineMessage(linebreak);
            if (showStrippedVersion) {
                issueMessage += "Stripped Line " + l.getLineNumber() + ": " + lineString + linebreak;
            }
            issueMessage += linebreak;
            issue = new Issue(Severity.getEnum("MINOR"), l, issueSnippet, issueMessage, "Grammar");
            addIssue(issue);
            issueMessage = issueMessageDefault;
        }
        if (line.indexOf(",,") != -1) {
            issueSnippet = StringFunctions.getSnippet(line, ",,");
            issueMessage += "Double comma (grammatical error) on dialogue line on line " + l.getLineNumber() + ", col " + (line.indexOf(",,")) + " near \"" + issueSnippet + "\"" + linebreak;
            issueMessage += l.printLineMessage(linebreak);
            if (showStrippedVersion) {
                issueMessage += "Stripped Line " + l.getLineNumber() + ": " + lineString + linebreak;
            }
            issueMessage += linebreak;
            issue = new Issue(Severity.getEnum("MINOR"), l, issueSnippet, issueMessage, "Grammar");
            addIssue(issue);
            issueMessage = issueMessageDefault;
        }

Every time I declare something I check for, and then make a snippet, message, line, maybe a stripped line, create an issue and add the issue. Then I reset the message to the default. I'm repeating myself over and over, and I'd like to fix that somehow.

addIssue, for reference, just stores the issues in an associative array with linenumber as key and a value of issues.

    protected function addIssue(i:Issue):void {
        var lineNumber:uint = i.getRelatedLineNumber();
        if (issues[lineNumber] == null) {
            issues[lineNumber] = new Array();
        }
        issues[lineNumber].push(i);
    }

I use the issues array for printing issues per-line.

The following things are checked (bit of an old list, might be more):

[  ] - Double space is always bad. Can be disabled with #DialogueChecker-NoDoubleSpace#.
[,,] - Double comma.
[ ,] - Space in front of comma is a grammatical error.
[,?] - Grammatical error.
[,!] - Grammatical error.
[ !] - Space before exclamation mark is a grammatical error.
[ ?] - Space before question mark is a grammatical error.
[!.] - Grammatical error. (Whitelisted: [!...].)
[?.] - Grammatical error. (Whitelisted: [?...].)
[%0A ] - Space at start of new line
[:" ] - Space at start of new line (or you have spaces in your variables - which is not wrong (maybe I could catch this by whitelisting [":" ]))
[ . ] - Space before end of line is a grammatical error.
[.!.] - Grammatical error.
[.?.] - Grammatical error.
[.?] - Grammatical error, double sentence ending.
[.,] - Grammatical error, wrongfully continued sentence.
[.!] - Grammatical error, double sentence ending.
[.,.] - Grammatical error. Maybe it's a wrongfully typed ellipsis?
[!,] - Grammatical error, as a finished sentence shouldn't be continued with a comma.
[?,] - Grammatical error, as a finished sentence shouldn't be continued with a comma.
[,.] - Grammatical error, as a comma indicates that more text will follow.
- Lines starting with a comma, as this is not a proper way to begin a sentence.
- Lines starting with a disconnected exclamation mark [! ].
- Lines starting with a disconnected period [. ].
- All of the above, when the following is stripped: 
-- Anything between asterisks that contain YOU, YOUR, ME or MY (substitutes) 
-- delay characters ("Б")
-- triggers (anything between square brackets, game uses these to simulate actions e.g. "Hello.[OPEN_EYES]!" would print as "Hello.!",
with character eyes being opened between the period and the exclamation mark)

What I need help with is breaking my checkLine function apart. If I try to relocate some of the checks, I end up having to redefine the variables line, issue, issueMessage, issueSnippet, linebreak and so on. Because of this, I prefer to keep my checks in one function, which will then grow out into a massive bloated function. If I could somehow avoid this boilerplate, I could write functions that handle a set of checks, and make my class read in a more logical manner.

\$\endgroup\$
1

1 Answer 1

1
\$\begingroup\$

This answer doesn't solve my main issue - I'm just writing it to note how I keep making stupid duplicated code.

        if (options != null) {
            if (options.getSetting("substitutes")) {
                if (varsubsReplaced) {
                    if (lineContainsYOU) {
                        issueMessageDefault = "The following issue was found after replacing variable substitutions, and stripping the line of delay characters and triggers." + linebreak;
                        issueMessageDefault += "Keep in mind that you can place characters in YOU/YOUR/ME/MY variable insertions, like \"Hello*, YOU*!\"." + linebreak;
                    } else {
                        issueMessageDefault = "The following issue was found after replacing variable substitutions, and stripping the line of delay characters and triggers:" + linebreak;
                    }
                } else {
                    if (lineContainsYOU) {
                        issueMessageDefault = "The following issue was found after stripping the line of delay characters, triggers and variable substitutions." + linebreak;
                        issueMessageDefault += "Keep in mind that you can place characters in YOU/YOUR/ME/MY variable insertions, like \"Hello*, YOU*!\"." + linebreak;
                    } else {
                        issueMessageDefault = "The following issue was found after stripping the line of delay characters, triggers and variable substitutions:" + linebreak;
                    }

                }
            } else {
                issueMessageDefault = "The following issue was found after stripping the line of delay characters and triggers:" + linebreak;
            }
        } else {
            if (lineContainsYOU) {
                issueMessageDefault = "The following issue was found after stripping the line of delay characters, triggers and variable substitutions." + linebreak;
                issueMessageDefault += "Keep in mind that you can place characters in YOU/YOUR/ME/MY variable insertions, like \"Hello*, YOU*!\"." + linebreak;
            } else {
                issueMessageDefault = "The following issue was found after stripping the line of delay characters, triggers and variable substitutions:" + linebreak;
            }
        }

If you build the string that changes dynamically, you'll have a much smaller section of code.

You have 4 different things that may or may not be included in the string: - replaced substitutes - removed substitutes - delay characters - triggers

replaced and removed substitutes are exclusive to each other. (but you can also have neither)

Delay characters and triggers are ALWAYS in the string.

So if you write

var handledVarSubs:Boolean = false;
issueMessageDefault = "The following issue was found after ";
if(options != null){
    if(options.getSetting("substitutes") && varsubsReplaced){
        issueMessageDefault += "replacing variable substitutions, and ";
    }
    handledVarSubs = true;
}
issueMessageDefault += "stripping the line of delay characters";
if(!handledVarSubs){
    issueMessageDefault += ", triggers and variable substitutions";
} else {
    issueMessageDefault += " and triggers";
}
if(lineContainsYOU){
    issueMessageDefault += "." + linebreak;
    issueMessageDefault += "Keep in mind that you can place characters in YOU/YOUR/ME/MY variable insertions, like \"Hello*, YOU*!\"." + linebreak;
} else {
    issueMessageDefault += ":" + linebreak;
}

Less duplication, same output.

You can refactor after that - like, varSubs are always handled if options is not null, so the variable can disappear...

issueMessageDefault = "The following issue was found after ";
if(options != null){
    if(options.getSetting("substitutes") && varsubsReplaced){
        issueMessageDefault += "replacing variable substitutions, and ";
    }
}
issueMessageDefault += "stripping the line of delay characters";
if(options != null){
    issueMessageDefault += " and triggers";
} else {
    issueMessageDefault += ", triggers and variable substitutions";
}
if(lineContainsYOU){
    issueMessageDefault += "." + linebreak;
    issueMessageDefault += "Keep in mind that you can place characters in YOU/YOUR/ME/MY variable insertions, like \"Hello*, YOU*!\"." + linebreak;
} else {
    issueMessageDefault += ":" + linebreak;
}

and a slight bit of reordering...

issueMessageDefault = "The following issue was found after ";
if(options != null){
    if(options.getSetting("substitutes") && varsubsReplaced){
        issueMessageDefault += "replacing variable substitutions, and stripping the line of ";
    }
} else {
    issueMessageDefault += "stripping the line of variable substitutions, ";
}
issueMessageDefault += "delay characters and triggers";
if(lineContainsYOU){
    issueMessageDefault += "." + linebreak;
    issueMessageDefault += "Keep in mind that you can place characters in YOU/YOUR/ME/MY variable insertions, like \"Hello*, YOU*!\"." + linebreak;
} else {
    issueMessageDefault += ":" + linebreak;
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.