C# 用于流控制的模式优于嵌套if语句
有人能提出一个更好的模式来处理像这样的步骤吗 我现在唯一能想到的另一种方法是对流控制使用异常,但我已经读到它是不可取的,因为它基本上是一个goto语句C# 用于流控制的模式优于嵌套if语句,c#,asp.net,design-patterns,C#,Asp.net,Design Patterns,有人能提出一个更好的模式来处理像这样的步骤吗 我现在唯一能想到的另一种方法是对流控制使用异常,但我已经读到它是不可取的,因为它基本上是一个goto语句 if (FileHandler.CheckIfNewFilesExist(sourceFolderPath)) { if (FileHandler.MoveFolder(sourceFolderPath, temporyFolderPath)) {
if (FileHandler.CheckIfNewFilesExist(sourceFolderPath))
{
if (FileHandler.MoveFolder(sourceFolderPath, temporyFolderPath))
{
if (CSVHandler.AppendUniqueIdToCSV(temporyFolderPath, filesToBeAppended))
{
FileHandler.CopyFolder(temporyFolderPath, finalFolderPath);
}
}
}
一篇关于扁平化这些结构的好文章可以在
我把它粘贴在这里没有意义。在上可以找到一篇关于展平这些结构的好文章 我把它粘贴在这里没有意义。使用&&
if (FileHandler.CheckIfNewFilesExist(sourceFolderPath)
&& FileHandler.MoveFolder(sourceFolderPath, temporyFolderPath)
&& CSVHandler.AppendUniqueIdToCSV(temporyFolderPath, filesToBeAppended))
{
FileHandler.CopyFolder(temporyFolderPath, finalFolderPath);
}
使用&&
在您的情况下,可以使用
&&
运算符将所有条件组合成单个if
,但如果每个if
块中有一些额外的代码,则可以反转if
的:
if (!FileHandler.CheckIfNewFilesExist(sourceFolderPath))
return;
// Perhaps some more code ...
if (!FileHandler.MoveFolder(sourceFolderPath, temporyFolderPath))
return;
// Perhaps some more code ...
if (!CSVHandler.AppendUniqueIdToCSV(temporyFolderPath, filesToBeAppended))
return;
FileHandler.CopyFolder(temporyFolderPath, finalFolderPath);
显然,您必须将代码提取到一个方法中,以便能够使用
返回在您的情况下,所有条件都可以使用&&
操作符组合成一个if
,但是如果每个if
块中都有一些额外的代码,您可以反转if
的:
if (!FileHandler.CheckIfNewFilesExist(sourceFolderPath))
return;
// Perhaps some more code ...
if (!FileHandler.MoveFolder(sourceFolderPath, temporyFolderPath))
return;
// Perhaps some more code ...
if (!CSVHandler.AppendUniqueIdToCSV(temporyFolderPath, filesToBeAppended))
return;
FileHandler.CopyFolder(temporyFolderPath, finalFolderPath);
显然,您必须将代码提取到一个方法中才能使用return
您可以非常轻松地创建一个流畅的界面,这样做:
FileHandler.CheckIfNewFilesExist(sourceFolderPath)
.ThenIf(() => FileHandler.MoveFolder(sourceFolderPath, temporyFolderPath))
.ThenIf(() =>
CSVHandler.AppendUniqueIdToCSV(temporyFolderPath, filesToBeAppended))
.ThenDo(() => FileHandler.CopyFolder(temporyFolderPath, finalFolderPath));
这是:
public static class BooleanEx
{
public static bool ThenIf(this bool @this, Func<bool> that)
{
return @this ? that() : false;
}
public static void ThenDo(this bool @this, Action action)
{
if (@this)
{
action();
}
}
}
公共静态类BooleanEx
{
公共静态bool ThenIf(this bool@this,Func that)
{
返回@this?that():false;
}
公共静态void ThenDo(this bool@this,Action-Action)
{
如果(@this)
{
动作();
}
}
}
您可以非常轻松地创建一个流畅的界面,实现以下功能:
FileHandler.CheckIfNewFilesExist(sourceFolderPath)
.ThenIf(() => FileHandler.MoveFolder(sourceFolderPath, temporyFolderPath))
.ThenIf(() =>
CSVHandler.AppendUniqueIdToCSV(temporyFolderPath, filesToBeAppended))
.ThenDo(() => FileHandler.CopyFolder(temporyFolderPath, finalFolderPath));
这是:
public static class BooleanEx
{
public static bool ThenIf(this bool @this, Func<bool> that)
{
return @this ? that() : false;
}
public static void ThenDo(this bool @this, Action action)
{
if (@this)
{
action();
}
}
}
公共静态类BooleanEx
{
公共静态bool ThenIf(this bool@this,Func that)
{
返回@this?that():false;
}
公共静态void ThenDo(this bool@this,Action-Action)
{
如果(@this)
{
动作();
}
}
}
对于4个步骤来说是合理的。如果你没有更多的步骤(>=10),我不会寻找不同的解决方案。4个步骤看起来很合理。如果你没有更多的步骤(>=10),我不会寻找不同的解决方案。现在我已经看到了,这似乎很明显!谢谢,我已经看过了,很明显!谢谢