C + + Proverbs: Isolate parameter-independent code from a template

Source: Internet
Author: User
Tags inheritance

Templates are an excellent way to save time and avoid code duplication. Without having to enter 20 similar classes, each containing 15 member functions, you can enter a class template (class template) and have the compiler instantiate the 20 specific classes you need (specific class ) and 300 functions. (member functions of class template) are implicitly instantiated only when they are used, so you will get all 300 member functions (members functions) only if each function is actually in use. Function templates also have similar charms. Without having to write a lot of functions, you can write a function template and let the compiler do the rest. Isn't this a very important technique?

Yes, good. Sometimes. If you're not careful, using the templates (template) can lead to code bloat: repetitive (or almost repetitive) code, data, or binary code for both. The results will make the source code look compact and tidy, but the target code is bloated and loose. Being bloated and loose is rarely a fad, so you need to know how to avoid such binary expansions.

Your main tool has an imposing name commonality and variability analysis (versatility and variability), but there is nothing imposing on this idea. Even if you have never used a template in your career, you should do so from the beginning to the end.

When you write a function, and you realize that some part of the implementation of this function is essentially the same as the implementation of another function, would you just copy the code? Of course not. You separate the common code from the two functions, put it into a third function, and let the two functions call the new function. In other words, you analyze those two functions to find the common and changing artifacts, you move the generic artifacts into a new function and keep the changed artifacts in the original function. Similarly, if you write a class and you realize that some of the components of this class are the same as the artifacts of another class, you do not copy those generic artifacts. Instead, you move the generic widget into a new class, and then you use Inheritance (inheritance) or composition (compound) to make the original classes access to these common features. The different artifacts in the original classes--the changing artifacts--remain in their original positions.

Related Article

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.