"Go" writing high-quality Code 157 recommendations for improving C # programs--Recommendation 143: Method abstraction levels should be at the same level

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.