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

How to make the interface clear in C++

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

Share

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

This article introduces the relevant knowledge of "how to make the interface clear in C++". In the operation of actual cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

I.1: Make interfaces explicit (make the interface clear) Reason (reason)

Correctness. Assumptions not stated in an interface are easily overlooked and hard to test.

Correct. Assumptions not specified in the interface are easy to ignore and difficult to test.

Example, bad (negative example)

The way to control function behavior through global (namespace-scoped) variables (call patterns) is obscure and difficult to understand, such as:

Int round (double d) {return (round_up)? Ceil (d): d; / / don't: "invisible" dependency} It will not be obvious to a caller that the meaning of two calls of round. It is not easy for callers to understand that two calls to round (7. 2) may return different results.

Exception (exception)

Sometimes we control the details of a set of operations through environment variables. For example, the normal output is still detailed output, or it may be debugging or optimization. The use of non-local control can be difficult to understand and can only be applied to other fixed semantic implementation details.

Example, bad (negative example)

Reporting through non-local variables (e.g.errno) is easily ignored.

For example:

It is easy to ignore through non-local variable reporting (such as error coding). For example:

/ / don't: no test of printf's return valuefprintf (connection, "logging:% d% d\ n", x, y, s)

What if the connection is closed and there is no log output?

Alternative: Throw an exception. An exception cannot be ignored.

Optional: throw an exception. Exceptions are not ignored.

Another way of saying: avoid passing information through the interface in a non-local or implied state. Note that a non-const member passes information to another member function through the object state function.

Another way of saying: an interface should be a function or a set of functions. Interfaces can be template functions, and function groups can be classes and class templates.

Enforcement (implementation recommendations)

(Simple) A function should not make control-flow decisions based on the values of variables declared at namespace scope.

(simple) functions should not determine control flow based on variables in the scope of the namespace.

(Simple) A function should not write to variables declared at namespace scope.

(simple) functions should not overwrite variables defined in the namespace scope.

This is the end of the content of "how to make the interface clear in C++". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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