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 keep the function short in C++

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article will explain in detail how to keep the function short in C++, and the content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.

F.3: Keep functions short and simple (keep the function short) Reason (reason)

Large functions are hard to read, more likely to contain complex code, and more likely to have variables in larger than minimal scopes. Functions with complex control structures are more likely to be long and more likely to hide logical errors

Large functions are difficult to understand, are more likely to contain complex code, and may contain variables that exceed the minimum scope. Code that contains complex control structures is more likely to be long code and easier to hide logic errors.

Example (sample)

Consider (consider the following code):

Double simple_func (doubleval, int flag1, int flag2) / / simple_func: takes a value and calculates the expected ASIC output, / / given the two mode flags. {double intermediate; if (flag1 > 0) {intermediate = func1 (val); if (flag2% 2) intermediate = sqrt (intermediate);} else if (flag1 = =-1) {intermediate = func1 (- val) If (flag2% 2) intermediate = sqrt (- intermediate); flag1 =-flag1;} if (abs (flag2) > 10) {intermediate = func2 (intermediate);} switch (flag2 / 10) {case 1: if (flag1 =-1) return finalize (intermediate, 1.171); break; case 2: return finalize (intermediate, 13.1); default: break } return finalize (intermediate, 0.);}

This is too complex. How would you know if all possible alternatives have been correctly handled? Yes, it breaks other rules also.

We can refactor:

The code is too complex. How do you know if all possible branches have been handled correctly? Yes, it also violates other rules.

We can ReFactor this code:

Double func1_muon (doubleval, int flag) {/ /?} double func1_tau (doubleval, int flag1, int flag2) {/ /?} double simple_func (doubleval, int flag1, int flag2) / / simple_func: takes a value and calculates the expected ASIC output, / / given the two mode flags. {if (flag1 > 0) return func1_muon (val, flag2) If (flag1 = =-1) / / handled by func1_tau: flag1 =-flag1; return func1_tau (- val, flag1, flag2); return 0;}

Note (Note)

"It doesn't fit on a screen" is often a good practical definition of "far too large." One-to-five-line functions should be considered normal.

"one-screen display" is usually a good practice for defining "too large" code. One to five lines of code should be considered normal.

Note (Note)

Break large functions up into smaller cohesive and named functions. Small simple functions are easily inlined where the cost of a function call is significant.

The difference between large functions is divided into smaller cohesive functions and named. Short functions are easy to linline when function calls are too expensive.

Translator's note: there is no need to worry too much about the increased call cost because the function is short.

Enforcement (implementation recommendations)

Flag functions that do not "fit on a screen." How big is a screen? Try 60 lines by 140 characters; that's roughly the maximum that's comfortable for a book page.

Mark the function that "it won't fit on a screen". How big is a screen? Try 60 rows and 80 columns. This is probably the maximum value of a comfortable one-page book.

Flag functions that are too complex. How complex is too complex? You could use cyclomatic complexity. Try "more than 10 logical path through." Count a simple switch as one path.

So much for sharing about how to keep the function short in C++. I hope the above can be helpful and learn more. If you think the article is good, you can share it for more people to see.

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