如何在C#中将代码块封装为函数?替换if/else分支逻辑
重构C#代码:用函数替代重复的if/else分支逻辑
原代码中CopyPasteFiles方法根据IsMale和IsFemale的判断,存在大量重复的文件复制、路径处理逻辑。我们可以通过提取通用函数来消除重复,让代码更简洁易维护。
原代码
public void CopyPasteFiles(string modelInfoFilePath, string definitionFilePath, string pedName) { string messageBoxText = "Looks like a ped with this name already Exists. Please try a new Name."; if (this.IsMale) { modelInfoFilePath = Tokenizer.Detokenize(modelInfoFilePath); this.NewModelInfoPath = modelInfoFilePath.Replace("Z_Z_ProxyPed_MR1_000_Dummy", pedName); if (!File.Exists(this.NewModelInfoPath)) { File.Copy(modelInfoFilePath, this.NewModelInfoPath, false); FileInfo fileInfo = new FileInfo(this.NewModelInfoPath); fileInfo.IsReadOnly = false; } else { RsMessageBox.Show(messageBoxText); } definitionFilePath = Tokenizer.Detokenize(definitionFilePath); this.NewDefinitionPath = definitionFilePath.Replace("Z_Z_ProxyPed_MR1_000_Dummy", pedName); if (!File.Exists(this.NewDefinitionPath)) { File.Copy(definitionFilePath, this.NewDefinitionPath, false); FileInfo fileInfo = new FileInfo(this.NewDefinitionPath); fileInfo.IsReadOnly = false; } else { RsMessageBox.Show(messageBoxText); } } else if (this.IsFemale) { modelInfoFilePath = Tokenizer.Detokenize(modelInfoFilePath); this.NewModelInfoPath = modelInfoFilePath.Replace("Z_Z_ProxyPed_FR1_000_Dummy", pedName); if (!File.Exists(this.NewModelInfoPath)) { File.Copy(modelInfoFilePath, this.NewModelInfoPath, false); FileInfo fileInfo = new FileInfo(this.NewModelInfoPath); fileInfo.IsReadOnly = false; } else { RsMessageBox.Show(messageBoxText); } definitionFilePath = Tokenizer.Detokenize(definitionFilePath); this.NewDefinitionPath = definitionFilePath.Replace("Z_Z_ProxyPed_FR1_000_Dummy", pedName); if (!File.Exists(this.NewDefinitionPath)) { File.Copy(definitionFilePath, this.NewDefinitionPath, false); FileInfo fileInfo = new FileInfo(this.NewDefinitionPath); fileInfo.IsReadOnly = false; } else { RsMessageBox.Show(messageBoxText); } } }
重构后的代码
1. 提取通用文件处理函数
把重复的文件复制、路径处理逻辑封装成独立函数:
private void CopyAndUpdateFile(string originalPath, string dummyToken, string pedName, out string newPath, string messageBoxText) { string detokenizedPath = Tokenizer.Detokenize(originalPath); newPath = detokenizedPath.Replace(dummyToken, pedName); if (!File.Exists(newPath)) { File.Copy(detokenizedPath, newPath, false); FileInfo fileInfo = new FileInfo(newPath); fileInfo.IsReadOnly = false; } else { RsMessageBox.Show(messageBoxText); } }
2. 简化主方法
修改CopyPasteFiles方法,调用通用函数消除分支重复:
public void CopyPasteFiles(string modelInfoFilePath, string definitionFilePath, string pedName) { string messageBoxText = "Looks like a ped with this name already Exists. Please try a new Name."; string dummyToken = string.Empty; if (this.IsMale) { dummyToken = "Z_Z_ProxyPed_MR1_000_Dummy"; } else if (this.IsFemale) { dummyToken = "Z_Z_ProxyPed_FR1_000_Dummy"; } else { // 可选:处理既非男性也非女性的情况 return; } // 处理ModelInfo文件 CopyAndUpdateFile(modelInfoFilePath, dummyToken, pedName, out this.NewModelInfoPath, messageBoxText); // 处理Definition文件 CopyAndUpdateFile(definitionFilePath, dummyToken, pedName, out this.NewDefinitionPath, messageBoxText); }
重构说明
- 消除代码冗余:将两个分支中重复的路径解令牌、替换、复制、取消只读逻辑统一封装,避免重复编写相同代码。
- 提升可维护性:后续若需修改文件处理逻辑(比如添加日志、调整复制规则),只需修改通用函数,无需在多个分支重复操作。
- 逻辑更清晰:主方法仅负责判断性别并获取对应替换令牌,再调用通用函数处理文件,职责单一,可读性更强。
内容的提问来源于stack exchange,提问作者Allwyn samson
相关产品推荐
相关产品推荐

