2010-01-13 52 views
2

我一直在寻找改善以下代码重构的意见/想法需要

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(); 
    } 
} 

预先感谢任何帮助

回答

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"); 

    ... 
+0

谢谢!这确实看起来比多个while循环更好。 = D – AlteredConcept 2010-01-13 00:51:14

+0

这也比我的好多了。 – mnuzzo 2010-01-13 00:54:46

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”会丢失堆栈跟踪,但只是“throw”会保留它。我已经纠正了这个问题。

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

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

+0

-1'赶上(异常前){throw ex;}'这会丢失堆栈跟踪。如果你不打算解决这个问题,就不要抓住例外。 – 2010-01-13 00:45:12

+1

确实'throw'比'throw ex'更好,但是如果你在catch块中没有做任何事情,try/catch块仍然是无用的......只要让异常冒出堆栈并被下一个抓住尝试/ catch块 – 2010-01-13 01:02:14

+0

Oy,我的Java熟悉再次咬我。在Java中,您必须指定何时抛出异常。我会离开它,因为它仍然是有效的代码,如果他们想要的话可​​以把其他东西放在那里。 – mnuzzo 2010-01-13 01:07:40