在 C# 中重构 if 语句块
Refactoring block of if statements in c#
我有一个函数想要重构,但我有点卡住了。
public async Task<IActionResult> SaveFileContents(string filename, string connectionId, string fileContents, string repositoryName, string branch, string button)
{
var repository = ViewModel.Products.FirstOrDefault(x => x.RepositoryName == repositoryName);
var cachedFilename = MemoryCache.Get<string>(connectionId);
if (button=="btn-config")
{
var testsFolderPath = repository != null ? repository.TestsFolderPath : "";
var testsDirectory = GetTestsDirectory(repositoryName, branch, testsFolderPath);
if (cachedFilename.Contains(filename))
{
var fullPath = string.Empty;
var configPath = GetConfigPath(repositoryName, branch);
if (Path.GetFileName(configPath) == filename)
{
fullPath = configPath;
}
else
{
fullPath = Path.Combine(testsDirectory, filename);
}
await IOHelper.WriteContentsToFileAsync(fullPath, fileContents);
}
}
else
{
var stepFilePath = repository != null ? repository.StepFilePath : "";
var stepFileDirectory = GetStepFile(repositoryName, branch, stepFilePath);
if (cachedFilename.Contains(filename))
{
var fullPath = string.Empty;
var stepFile = GetStepFile(repositoryName, branch, stepFilePath);
if (Path.GetFileName(stepFile) == filename)
{
fullPath = stepFileDirectory;
}
else
{
fullPath = Path.Combine(stepFilePath, filename);
}
await IOHelper.WriteContentsToFileAsync(fullPath, fileContents);
}
}
return Ok();
}
正如您所观察到的,大多数行都在做类似的事情。我设法将其重构为
var repository = ViewModel.Products.FirstOrDefault(x => x.RepositoryName == repositoryName);
var cachedFilename = MemoryCache.Get<string>(connectionId);
if (button == "btn-config")
{
var testsFolderPath = repository != null ? repository.TestsFolderPath : "";
var testsDirectory = GetTestsDirectory(repositoryName, branch, testsFolderPath);
var configPath = GetConfigPath(repositoryName, branch);
}
else
{
var testsDirectory = repository != null ? repository.StepFilePath : "";
var configPath = GetStepFile(repositoryName, branch, testsDirectory);
var stepFile = GetStepFile(repositoryName, branch, testsDirectory);
}
if (cachedFilename.Contains(filename))
{
var fullPath = string.Empty;
if (Path.GetFileName(configPath) == filename)
{
fullPath = configPath;
}
else
{
fullPath = Path.Combine(testsDirectory, filename);
}
await IOHelper.WriteContentsToFileAsync(fullPath, fileContents);
}
return Ok();
}
但是第二个 if 语句中的值表明它们在当前上下文中不存在。非常感谢任何指南和提示。
重构代码的问题在于,您的几个变量在您尝试使用它们之前超出了范围。现在,在 if...else 块中声明了以下内容:testsFolderPath
、testsDirectory
和 configPath
。这意味着当您点击第二个 if 块时,这些变量将不再存在。
您需要在您使用它们的所有地方都可以访问的范围内声明它们,如下所示:
string testsFolderPath = String.Empty;
string testsDirectory = String.Empty;
string configPath = String.Empty;
if (button == "btn-config")
{
testsFolderPath = repository != null ? repository.TestsFolderPath : "";
testsDirectory = GetTestsDirectory(repositoryName, branch, testsFolderPath);
configPath = GetConfigPath(repositoryName, branch);
}
else
{
testsDirectory = repository != null ? repository.StepFilePath : "";
configPath = GetStepFile(repositoryName, branch, testsDirectory);
stepFile = GetStepFile(repositoryName, branch, testsDirectory);
}
if (cachedFilename.Contains(filename))
{
var fullPath = string.Empty;
if (Path.GetFileName(configPath) == filename)
{
fullPath = configPath;
}
else
{
fullPath = Path.Combine(testsDirectory, filename);
}
await IOHelper.WriteContentsToFileAsync(fullPath, fileContents);
}
return Ok();
我对 testsFolderPath
、testsDirectory
和 configPath
的类型做了假设,因为您没有明确地将它们声明为特定类型。
进一步重构你重构的代码,
var repository = ViewModel.Products.FirstOrDefault(x => x.RepositoryName == repositoryName);
var cachedFilename = MemoryCache.Get<string>(connectionId);
if (button == "btn-config")
{
var testsFolderPath = repository != null ? repository.TestsFolderPath : "";
var testsDirectory = GetTestsDirectory(repositoryName, branch, testsFolderPath);
var configPath = GetConfigPath(repositoryName, branch);
}
else{
var testsDirectory = repository != null ? repository.StepFilePath : "";
var configPath = GetStepFile(repositoryName, branch, testsDirectory);
var stepFile = GetStepFile(repositoryName, branch, testsDirectory);
}
string fullPath = string.empty;
if (cachedFilename.Contains(filename) && Path.GetFileName(configPath) == filename)
{
fullPath = configPath;
}
else{
fullPath = Path.Combine(testsDirectory, filename);
}
await IOHelper.WriteContentsToFileAsync(fullPath, fileContents);
return Ok();
将两个 if 检查放在一起
我有一个函数想要重构,但我有点卡住了。
public async Task<IActionResult> SaveFileContents(string filename, string connectionId, string fileContents, string repositoryName, string branch, string button)
{
var repository = ViewModel.Products.FirstOrDefault(x => x.RepositoryName == repositoryName);
var cachedFilename = MemoryCache.Get<string>(connectionId);
if (button=="btn-config")
{
var testsFolderPath = repository != null ? repository.TestsFolderPath : "";
var testsDirectory = GetTestsDirectory(repositoryName, branch, testsFolderPath);
if (cachedFilename.Contains(filename))
{
var fullPath = string.Empty;
var configPath = GetConfigPath(repositoryName, branch);
if (Path.GetFileName(configPath) == filename)
{
fullPath = configPath;
}
else
{
fullPath = Path.Combine(testsDirectory, filename);
}
await IOHelper.WriteContentsToFileAsync(fullPath, fileContents);
}
}
else
{
var stepFilePath = repository != null ? repository.StepFilePath : "";
var stepFileDirectory = GetStepFile(repositoryName, branch, stepFilePath);
if (cachedFilename.Contains(filename))
{
var fullPath = string.Empty;
var stepFile = GetStepFile(repositoryName, branch, stepFilePath);
if (Path.GetFileName(stepFile) == filename)
{
fullPath = stepFileDirectory;
}
else
{
fullPath = Path.Combine(stepFilePath, filename);
}
await IOHelper.WriteContentsToFileAsync(fullPath, fileContents);
}
}
return Ok();
}
正如您所观察到的,大多数行都在做类似的事情。我设法将其重构为
var repository = ViewModel.Products.FirstOrDefault(x => x.RepositoryName == repositoryName);
var cachedFilename = MemoryCache.Get<string>(connectionId);
if (button == "btn-config")
{
var testsFolderPath = repository != null ? repository.TestsFolderPath : "";
var testsDirectory = GetTestsDirectory(repositoryName, branch, testsFolderPath);
var configPath = GetConfigPath(repositoryName, branch);
}
else
{
var testsDirectory = repository != null ? repository.StepFilePath : "";
var configPath = GetStepFile(repositoryName, branch, testsDirectory);
var stepFile = GetStepFile(repositoryName, branch, testsDirectory);
}
if (cachedFilename.Contains(filename))
{
var fullPath = string.Empty;
if (Path.GetFileName(configPath) == filename)
{
fullPath = configPath;
}
else
{
fullPath = Path.Combine(testsDirectory, filename);
}
await IOHelper.WriteContentsToFileAsync(fullPath, fileContents);
}
return Ok();
}
但是第二个 if 语句中的值表明它们在当前上下文中不存在。非常感谢任何指南和提示。
重构代码的问题在于,您的几个变量在您尝试使用它们之前超出了范围。现在,在 if...else 块中声明了以下内容:testsFolderPath
、testsDirectory
和 configPath
。这意味着当您点击第二个 if 块时,这些变量将不再存在。
您需要在您使用它们的所有地方都可以访问的范围内声明它们,如下所示:
string testsFolderPath = String.Empty;
string testsDirectory = String.Empty;
string configPath = String.Empty;
if (button == "btn-config")
{
testsFolderPath = repository != null ? repository.TestsFolderPath : "";
testsDirectory = GetTestsDirectory(repositoryName, branch, testsFolderPath);
configPath = GetConfigPath(repositoryName, branch);
}
else
{
testsDirectory = repository != null ? repository.StepFilePath : "";
configPath = GetStepFile(repositoryName, branch, testsDirectory);
stepFile = GetStepFile(repositoryName, branch, testsDirectory);
}
if (cachedFilename.Contains(filename))
{
var fullPath = string.Empty;
if (Path.GetFileName(configPath) == filename)
{
fullPath = configPath;
}
else
{
fullPath = Path.Combine(testsDirectory, filename);
}
await IOHelper.WriteContentsToFileAsync(fullPath, fileContents);
}
return Ok();
我对 testsFolderPath
、testsDirectory
和 configPath
的类型做了假设,因为您没有明确地将它们声明为特定类型。
进一步重构你重构的代码,
var repository = ViewModel.Products.FirstOrDefault(x => x.RepositoryName == repositoryName);
var cachedFilename = MemoryCache.Get<string>(connectionId);
if (button == "btn-config")
{
var testsFolderPath = repository != null ? repository.TestsFolderPath : "";
var testsDirectory = GetTestsDirectory(repositoryName, branch, testsFolderPath);
var configPath = GetConfigPath(repositoryName, branch);
}
else{
var testsDirectory = repository != null ? repository.StepFilePath : "";
var configPath = GetStepFile(repositoryName, branch, testsDirectory);
var stepFile = GetStepFile(repositoryName, branch, testsDirectory);
}
string fullPath = string.empty;
if (cachedFilename.Contains(filename) && Path.GetFileName(configPath) == filename)
{
fullPath = configPath;
}
else{
fullPath = Path.Combine(testsDirectory, filename);
}
await IOHelper.WriteContentsToFileAsync(fullPath, fileContents);
return Ok();
将两个 if 检查放在一起