如何在 C# 中将一个大方法拆分为多个较小的方法

How split a big method to smaller several methods in C#

如何在 C# 中将一个大的、困难的方法最佳地拆分为几个较小的方法?这个问题有什么感知或者功能吗?

假设您正在使用 Visual Studio 编辑您的 C# 代码,那么您尝试执行的操作有内置功能:Extract Method Refactoring。请注意,这仍然是一个手动过程 - 没有自动工具知道如何将整个方法分开。


重构时要注意什么?摘自 Robert C. Martin 的 Clean Code:

  • The first rule of functions is that they should be small. The second rule of functions is that they should be smaller than that.

  • Functions should do one thing. They should do it well. They should do it only.

  • We want the code to read like a top-down narrative.

  • Use descriptive names.

  • The ideal number of arguments for a function is zero (niladic). Next comes one (monadic), followed closely by two (dyadic). Three arguments (triadic) should be avoided where possible. More than three (polyadic) requires very special justification