More and less features
I had a small argument and was wondering what people think:
In C ++ (or more generally), you prefer the code to be broken down into several shorter functions, with main () only consisting of a list of functions in logical order, or you prefer functions only when needed (i.e. when they are multiple used)? Or maybe something in between?
source to share
Small functions please
It is common wisdom that smaller functions are better, and I think this is true. In fact, there is a company with an analysis tool that evaluates individual features by the number of decisions they make versus the number of unit tests they have.
The theory is that you may or may not be able to reduce complexity across the entire application, but you have complete control over how difficult it is in any given function.
A dimension called cyclical complexity is believed to correlate positively with bad code ... in particular, the more paths there are, the higher the method is its CCN number, the worse it is written, the more difficult it is to understand and therefore change or even start with from the very beginning, and the more it will require testing.
Ok, found a tool. It is called, gem, C hange R isk A nalysis and P index of repression.
Recently, the principle of encoding information has only grown once with new acronyms, in particular DRY (don't repeat yourself) and DIE (Duplicate Evil) ... I believe we can partly thank the RoR community for promoting this philosophy ...
source to share
Separate functions, but never separate functionality.
The functionality can be split into layers, then each layer can be split into different functions. For example, when we process a series, the main loop for addition and subtraction should be in the primary function. This can be thought of as level 1. Now the functionality for finding power can be classified into level 2. It can be implemented as a helper function. Likewise, finding the factorial also applies to layer 2, which will be another subfunction. Always consider functionality, never count the number of lines. The number of lines can vary from 3 to 300, it doesn't matter. This will improve the readability and maintainability of our code. This is my splitting idea.
source to share
I think the only answer is somewhere in between. If you decompose the functions every time, it becomes unreadable. Likewise, if you never disintegrate, it also becomes unreadable.
I like to group functions into semantic differences. This is one logical unit for some calculations. Keep your devices small, but big enough to actually do something useful.
source to share
My favorite granularity rule for a function is "no more than 24 lines and 80 characters each" - and that's not just because 80 x 24 terminals were all the rage "back when I started"; I think this is a reasonable guide to functions that you can "grasp like one eye", at least in C or languages ββnot much richer than C. "A function only does one thing", AKA "a function has one function" (playing to the meaning of "function" as "role" or "goal"!) is a secondary rule I use in languages ββwhere "too much functionality" can easily be packaged in 24 lines. But the "lexical eye" landmark - 24 x 80 - is still my main one.
source to share
Small functions are good, and smaller ones are better.
About five to eight lines of code is my upper limit on the size of a function. Besides, this is too complicated. You should be able to:
- Suppose the callee does what his name indicates,
- Read a function definition in seconds and
- Convince yourself that the first assumption means the real function is correct.
Another thing is that you have to use your functions before you write your code. When you see how you are going to use this function, you will see what pre- and post-conditions should perform the function.
Anything that is clearly not correct at first glance must be proven correct in the current comment. If it is complex, sub-functions of the factor.
source to share
Whatever helps with code reuse and readability, I believe.
Writing multiple functions on a single line just for that doesn't help with readability, so they should be grouped into classes that make sense, and then split the functions so you can quickly understand what's going on in that function.
If you have to skip to figure out what's going on, your design is flawed.
source to share
I prefer functions (or methods) that fit a single screen of code, so I can see at a glance whatever I need to refer to in order to understand how this function works. I usually have about 50 lines in editor windows, which are also typically 80 columns, so I can fit the two next to each other on the monitor and cross-reference between the two pieces of code.
So, I usually consider 50 lines to be the maximum. The only time I would think about allowing more is when you have one long initialization function or something completely linear (no variables, conditionals, or loops), since that's not where you want the whole this context and some APIs require a whole bunch of initialization to get up and running, and splitting it up into smaller functions hardly helps much.
All in all, though, nice, small, easy-to-understand functions that do one thing and are well known are far preferable to big stretch monsters that are hundreds of lines and dozens of variables to keep track of indented 10 levels.
source to share