我编写了一个小函数来监视一些目录。如果创建了一个文件,它将签入扩展名列表以进行匹配。你能告诉我我能不能在任何方面使我的功能更好?
private void sysWatcher_Created(object sender, FileSystemEventArgs e) {
try {
if (File.Exists(e.FullPath)) {
var fileName = Path.GetFileName(e.FullPath);
var fullPath = e.FullPath;
var changeType = e.ChangeType;
var fileExtension = Path.GetExtension(e.FullPath);
_extensionList.RemoveAll(r => string.IsNullOrEmpty(r.Trim()));
_ignoreList.RemoveAll(r => string.IsNullOrEmpty(r.Trim()));
// Checking for match in Extension List
bool _extResult = _extensionList.Any(s => e.FullPath.Contains(s));
if (_extResult) {
// Checking for match in Ignore List
bool _ignResult = _ignoreList.Any(s => e.FullPath.Contains(s));
if (!_ignResult) {
if (!GeneralSettings.autoMode) {
App.Current.Dispatcher.Invoke(delegate {
if (File.Exists(e.FullPath)) {
var fileInfo = new FileInfo(e.FullPath);
var wFunc = new WarningItem(e.FullPath, e.Name, AppSettings.SizeSuffix(fileInfo.Length),
fileInfo.CreationTime.ToString("g"), fileInfo.Extension);
var wWin = new WarningWin();
wWin.init(wFunc);
wWin.ShowDialog();
// Add to Warning List and List Box
_warningList.Add(new WarningItem {
DirName = Path.GetDirectoryName(e.FullPath),
CreationDate = DateTime.Now.ToString("g"),
Extension = fileInfo.Extension,
FullPath = e.FullPath,
FileName = e.Name,
FileSize = AppSettings.SizeSuffix(fileInfo.Length),
StatusImage = new Uri("/Images/inWatch.Folder.png", UriKind.RelativeOrAbsolute)
});
lstWarning.ItemsSource = _warningList;
tabWarning.IsSelected = true;
cmdSearchWarnings.Visibility = Visibility.Visible;
}
});
}
else {
Scan.scanFile(e.FullPath);
}
}
}
}
}
catch (Exception ex) {
GeneralSettings.LogException(ex);
}
}发布于 2015-07-06 05:27:08
这里有一个很好的箭头反图案示例,但是可以通过反转条件和使用return轻松地解决这个问题。
if (!File.Exists(e.FullPath)) { return; }
_extensionList.RemoveAll(r => string.IsNullOrEmpty(r.Trim()));
_ignoreList.RemoveAll(r => string.IsNullOrEmpty(r.Trim()));
// Checking for match in Extension List
bool _extResult = _extensionList.Any(s => e.FullPath.Contains(s));
if (!_extensionList.Any(s => e.FullPath.Contains(s))) { return ; }
if (_ignoreList.Any(s => e.FullPath.Contains(s))) { return; }
if (GeneralSettings.autoMode)
{
Scan.scanFile(e.FullPath);
return;
}
App.Current.Dispatcher.Invoke(delegate {
if (File.Exists(e.FullPath)) {
var fileInfo = new FileInfo(e.FullPath);
var wFunc = new WarningItem(e.FullPath, e.Name, AppSettings.SizeSuffix(fileInfo.Length),
fileInfo.CreationTime.ToString("g"), fileInfo.Extension);
....
....
....}通过这种方式,您的代码变得更易读,因为减少了水平间距。
关于@janos点关于“无点变量”:
这些变量不仅毫无意义,而且从未被使用过。
发布于 2015-07-04 12:43:27
这段代码中有几个奇怪的元素。
这些变量看起来毫无意义:
var fullPath = e.FullPath;
var changeType = e.ChangeType;您可以在需要的地方使用e.FullPath和e.ChangeType,额外的局部变量似乎没有任何用途。
每次创建文件时执行此代码都是非常奇怪的:
_extensionList.RemoveAll(r => string.IsNullOrEmpty(r.Trim()));
_ignoreList.RemoveAll(r => string.IsNullOrEmpty(r.Trim()));为什么你每次都需要过滤垃圾而不是只过滤一次呢?
更好的是:垃圾是如何进入这些列表的?最好是防止这种情况,并以一种不可能将垃圾输入这些列表的方式组织您的代码。
最后,r.Trim()的结果永远不会是空的,所以空检查是没有意义的.
Set ?与其将扩展和忽略的扩展放入列表中,不如使用sets。如果不需要lambda的话,检查它们是否包含元素将更加有效。
try块太大了。最好限制try块的范围,以便更容易地看到操作可能失败的代码段。
与其捕获一般的Exception,不如捕获可以引发的最特定类型的异常。
https://codereview.stackexchange.com/questions/95759
复制相似问题