Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Why try not to increase the number of parameters of the function in C++?

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "Why not to increase the number of parameters of the function as far as possible in C++", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let's let the editor take you to learn "Why don't try to increase the number of parameters of the function in C++?"

Reason (reason)

Too many parameters will increase the possibility of confusion. Compared with other ways, the cost of passing many parameters is usually high.

Discussion (discussion)

There are usually two reasons why a function has too many arguments:

Lack of abstraction. There is a lack of abstraction, so the composite data is passed separately rather than as a separate object containing invariants. This is not a problem of extending the parameter list, but an error caused by the fact that the combined values are not protected by forced invariants.

Violates the principle of "one function, one responsibility". The function attempts to do more than one thing and should be refactored.

Example (sample)

The tag library merge () is at the upper limit where we can handle it comfortably:

TemplateOutputIterator merge (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp)

Note that this is because of the first problem mentioned earlier: lack of abstraction. STL passes iterator pairs (combined values without wrappers) instead of range (abstraction).

Translator's note: range is a new feature introduced by Clover 14. Translator's note: range is a new feature introduced by Clover 14.

There are four template parameters and six function parameters. To simplify the most common and simplest usage, the comparison operation can be defaulted to

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report