首页 > 代码库 > Individual Project复审

Individual Project复审

我复审的是12061160刘垚鹏的代码。

刘垚鹏写的是多线程,程序有两个线程,一个线程用来读文件,另一个线程用来统计词频。

程序使用了线程安全的容器,大大减少了进程同步的难度。

没看出什么大问题,就强行挑点小毛病吧。

1.代码风格

1 public class Word : IComparable<Word>   //  2 {3     public string wordtodisplay;    //  将要显示的单词4     public int Frequency;           //  出现次数5     ... ...6 }

按照各种C#书的代码风格,这段改成下面这个样子可能好一些。

1 public class Word : IComparable<Word>   //  2 {3     public string WordToDisplay { get; set; }    //  将要显示的单词4     public int Frequency { get; set; }           //  出现次数5     ... ...6 }

 2.类命名自说明性差

 1 public abstract class MyThread 2         { 3             System.Threading.Thread myThread; 4             //wait = Infinite (-1)表示无限期等待 5             public MyThread(int wait = -1) 6             { 7                 myThread = new System.Threading.Thread(new System.Threading.ThreadStart(() => 8                 { 9                     string tmp;10                     while (true)11                         //尝试从 BlockingCollection<T> 中移除某个项12                         if (messages.TryTake(out tmp, wait))13                             HandleMessage(tmp);14                 }));15                 myThread.Start();16             }17             //数据处理方法,需要重写18             public abstract void HandleMessage(string msg);19             public void Close()20             {21                 //终止线程22                 myThread.Abort();23                 myThread = null;24             }25         }

这个类中包含了读取信息的功能,但是简单的命名为MyThread,可读性较差。

3.代码重复较多

  1 public class Computer : MyThread  2         {  3             //static int maxstrlength = 70;  4             public void Handle(string word)  5             {  6                 string higherword = word.ToUpper();  7                 Word w = null;  8                 //用TryGetValue代替containsKey方法  9                 if (myDictionary.TryGetValue(higherword, out w)) 10                 { 11                     string str = word.ToString(); 12                     if (String.CompareOrdinal(str, w.wordtodisplay) < 0) 13                         w.wordtodisplay = str; 14                     w.Frequency++; 15                 } 16                 else 17                 { 18                     myDictionary.TryAdd(higherword, w = new Word(word)); 19                 } 20             } 21             //计算结束后的委托:输出结果 22             Action<ICollection<Word>> finish; 23             public Computer(Action<ICollection<Word>> fun) 24             { 25                 finish = fun; 26             } 27             //int state = 0; 28             //StringBuilder sb0 = new StringBuilder(maxstrlength),sb1 = new StringBuilder(maxstrlength); 29             public override void HandleMessage(string msg) 30             { 31                 if (msg == "AstrNeVErexIST") 32                 { 33                     finish(myDictionary.Values); 34                     return; 35                 } 36                 string[] splited = Regex.Split(msg, "[^a-zA-Z0-9]"); 37                 //  再判断分割出的部分是否符合word的定义 38                 foreach (string s in splited) 39                 { 40                     //  如果符合定义,就对其进行计数 41                     if (Regex.IsMatch(s, "^[a-zA-Z]{3}[a-zA-Z0-9]*")) 42                     { 43                         Handle(s); 44                     } 45                 } 46             } 47         } 48         //定义extended mode2 的词频统计接口 49         public class _Computer : MyThread 50         { 51             //static int maxstrlength = 70; 52             public void Handle(string word) 53             { 54                 string higherword = word.ToUpper(); 55                 Word w = null; 56                 //用TryGetValue代替containsKey方法 57                 if (myDictionary.TryGetValue(higherword, out w)) 58                 { 59                     string str = word.ToString(); 60                     if (String.CompareOrdinal(str, w.wordtodisplay) < 0) 61                         w.wordtodisplay = str; 62                     w.Frequency++; 63                 } 64                 else 65                 { 66                     myDictionary.TryAdd(higherword, w = new Word(word)); 67                 } 68             } 69             //计算结束后的委托:输出结果 70             Action<ICollection<Word>> finish; 71             public _Computer(Action<ICollection<Word>> fun) 72             { 73                 finish = fun; 74             } 75             //int state = 0; 76             //StringBuilder sb0 = new StringBuilder(maxstrlength),sb1 = new StringBuilder(maxstrlength); 77             public override void HandleMessage(string msg) 78             { 79                 if (msg == "AstrNeVErexIST") 80                 { 81                     finish(myDictionary.Values); 82                     return; 83                 } 84                 Match match; 85                 int index = 0; 86                 char cc; 87                 while ((match = re2.Match(msg, index)).Success) 88                 { 89                     if (match.Index - 1 >= 0) 90                     { 91                         cc = msg[match.Index - 1]; 92                         if (cc <= 9 && cc >= 0) 93                         { 94                             index = match.Index + match.Value.IndexOf( ) - 1; 95                             continue; 96                         } 97                     } 98                     Handle(match.Value); 99                     //找到空格位置100                     index = match.Index + match.Value.IndexOf( ) - 1;101                 }102             }103         }104         //定义extended mode2 的词频统计接口105         public class __Computer : MyThread106         {107             public void Handle(string word)108             {109                 string higherword = word.ToUpper();110                 Word w = null;111                 //用TryGetValue代替containsKey方法112                 if (myDictionary.TryGetValue(higherword, out w))113                 {114                     string str = word.ToString();115                     if (String.CompareOrdinal(str, w.wordtodisplay) < 0)116                         w.wordtodisplay = str;117                     w.Frequency++;118                 }119                 else120                 {121                     myDictionary.TryAdd(higherword, w = new Word(word));122                 }123             }124             //计算结束后的委托:输出结果125             Action<ICollection<Word>> finish;126             public __Computer(Action<ICollection<Word>> fun)127             {128                 finish = fun;129             }130             public override void HandleMessage(string msg)131             {132                 if (msg == "AstrNeVErexIST")133                 {134                     finish(myDictionary.Values);135                     return;136                 }137                 Match match;138                 int index = 0;139                 char cc;140                 while ((match = re3.Match(msg, index)).Success)141                 {142                     if (match.Index - 1 >= 0){143                         cc = msg[match.Index - 1];144                         if (cc <= 9 && cc >= 0)145                         {146                             index = match.Index + match.Value.IndexOf( ) - 1;147                             continue;148                         }149                     }150                     Handle(match.Value);151                     //找到空格位置152                     index = match.Index + match.Value.IndexOf( ) - 1;153                 }154             }155         }

Computer类,_Computer类和__Computer类十分相似,可以合并成一个类,而不是把相似的代码写三遍

Individual Project复审