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 express intention in C++

2025-03-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly explains "how to express intention in C++". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "how to express intention in C++".

P.3: Express intent (expression of intention) Reason (reason)

As long as the code is not a direct statement of intent (such as in a name or comment), there may be inconsistencies between actual behavior and intention.

Example (example) gsl::index I = 0bot while (I < v.size ()) {/ /... Do something with v [i]...}

The intention to cycle through each element of v is not expressed. The implementation details of index are exposed (so it may be misused), and the valid scope of I exceeds the scope of the loop, which may be intentional or unintentional. Readers cannot understand this just through this code.

Better (better):

For (const auto& x: v) {/ * do something with the value of x * /}

Now, there is no clear hint of the iteration mechanism and the loop operator that acts on the const element, so there will be no unexpected changes. If you need to modify it, write:

For (auto& x: v) {/ * modify x * /}

Sometimes, as a better choice, you can use a naming algorithm. The for_each in Ranges TS is used in this example because it can directly express the intention.

For_each (v, [] (int x) {/ * do something with the value of x * /}); for_each (par, v, [] (int x) {/ * do something with the value of x * /})

The last deformation makes it clearer that we don't care about the order in which the elements in v are processed.

Programmers should be familiar with

Guide to The guidelines support library support Library

The ISO C++ Standard Library ISO C++ Standard Library

Whatever foundation libraries are used for the current project (s) any basic libraries used in the current project.

Translator's note: the prerequisite for choosing the way of writing that best expresses your intention is that you have to choose first. This requires familiarity with the language used and the functions of various libraries. Note (Note)

To put it another way: show what to do rather than how.

Note (Note)

Some languages are better at expressing intentions than others.

Example (sample)

If two integer values represent the coordinates of a point in a two-dimensional plane, you can say:

Draw_line (int, int); / obscure incomprehensible draw_line (Point, Point); / / clearer clear Enforcement (implementation recommendations)

Look for common styles with better choices.

Simple for loops vs. Range-for loops for loops, relatively simple for loops and range

F (threading, int) interfaces vs. F (span) interfaces f (threading, int) interface to f (span) interface

Loop variables in too large a scope (looping variables used in a wide range)

Direct new and delete operations of naked new and delete

Functions with many parameters of built-in types contains many functions with built-in type parameters

At this point, I believe you have a deeper understanding of "how to express intention in C++". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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