Recommendation 143: The method abstraction level should be at the same level
Look at the following code:
class SampleClass { publicvoid Init () { /// Local initialization code 1 // Local Initialization code 2 remoteinit (); } void Remoteinit () { // Remote Initialization code 1// Remote Initialization code 2 } }
The Init method is intended to complete the initialization action, and initialization includes both local initialization and remote initialization. In this code, the organization of the internal code of the Init method is local initialization that runs directly inside the method, while the remote initialization code is encapsulated as a method to be called here. This is obviously inappropriate and should be equivalent to the status of local initialization and remote initialization. If the remote initialization code exists as a method, the local initialization code should also exist as a method.
Therefore, the above code should be refactored to:
class SampleClass { public void Init () { LocalInit (); Remoteinit (); void LocalInit () {// local initialization code 1 // local initialization code 2 } void Remoteinit () { // remote initialization code 1 // remote initialization code 2 }}
The reconstructed code looks clear and the abstraction level of all the methods is at one level, giving the reader a glimpse of what functionality the Init method has done.
Turn from: 157 recommendations for writing high-quality code to improve C # programs Minjia
"Go" writing high-quality Code 157 recommendations for improving C # programs--Recommendation 143: Method abstraction levels should be at the same level