重构所需的建议/想法

时间:2010-01-13 00:24:51

标签: .net-3.5 c#-3.0 console-application

我一直在寻找改进以下代码的想法

static void Main(string[] args)
{
    bool validInput1 = false;
    string input1 = string.Empty;
    bool validInput2 = false;
    string input2 = string.Empty;

    bool validFilePath = false;
    string filePath = string.Empty;


    try
    {
        Console.WriteLine("Import process started.");

        while (!validFilePath)
        {
            Console.Write("Please enter the full path to the file you would like to import: ");
            filePath = Console.ReadLine().Replace("\"","");
            if (File.Exists(filePath))
                validFilePath = true;
        }

        while (!validInput1)
        {
            Console.Write("Enter a valid eventID the import file: ");
            input1 = Console.ReadLine();
            if (ValidEventID(input1.Trim().Length))
                validInput1 = true;
        }

        while (!validInput2)
        {
            Console.Write("Enter a valid import type code: ");
            input2 = Console.ReadLine();
            if (input2.Trim().ToUpper() == "EX" || input2.Trim().ToUpper() == "EL")
                validInput2 = true;
        }


        var records = Utilities.ParseCSV(filePath);
        var import = new Import
        {
            EventId = input1,
            ImportType = input2
        };


        import.ImportEventDates(records);

        Console.WriteLine("Import process completed.");
        Console.ReadLine();
    }
    catch (Exception ex)
    {
        Console.WriteLine("Error encountered");
        Console.WriteLine(ex.ToString());
        Console.ReadLine();
    }
}

提前感谢您提供任何帮助

2 个答案:

答案 0 :(得分:4)

我写了一个简单的方法来检索和验证用户输入:

public static string PromptUntilValid(string promptText, Func<string, bool> validationPredicate)
{
    string input;
    do
    {
        Console.Write(promptText);
        input = Console.ReadLine();
    } while (!validationPredicate(input))
    return input;
}

这将允许您的代码重构如下:

    ...

    filePath = PromptUntilValid(
        "Please enter the full path to the file you would like to import: ",
        s => File.Exists(s));

    input1 = PromptUntilValid(
        "Enter a valid eventID the import file: ",
        s => ValidEventID(s.Trim().Length));

    input2 = PromptUntilValid(
        "Enter a valid import type code: ",
        s => s.Trim().ToUpper() == "EX" || s.Trim().ToUpper() == "EL");

    ...

答案 1 :(得分:0)

您可以尝试将while循环输出并将它们放在各自的函数中,这些函数返回有效的文件路径和输入,如果它们被捕获则抛出异常。例如:

public string FindValidFilepath()
{
    string filePath = string.Empty
    try{
        while (!validFilePath)
        {
            Console.Write("Please enter the full path to the file you would like to import: ");
            filePath = Console.ReadLine().Replace("\"","");
            if (File.Exists(filePath))
                validFilePath = true;
        }
    } catch (Exception ex) {
        throw;
    }
    return filePath
}

并从Main函数调用它。如果你必须添加代码,你可以做的其他事情将减少错误是在你的If语句中的代码周围放置花括号。虽然没有它们在语法上是合法的,但它会在以后弹出错误。我曾被过去的错误所困扰。

编辑:重新抛出此异常是为了使原始的Try-Catch块保持原位。另外,我已经知道“throw ex”会丢失堆栈跟踪但只是“抛出”保留它。我已经纠正了这个问题。

编辑2:我想到的另一件事是,尝试捕获特定的异常并输出一个特定的错误,解释用户出了什么问题,以便他们更好地理解问题。除非输出堆栈跟踪是必需的,否则大多数用户不理解堆栈跟踪。

另外,请原谅任何小的语法错误,我比Java更熟悉Java。