Short version:
- Standing
five , which just keeps number five, is pretty worthless. Do not get around this for no reason (sometimes you have to because of syntax or input rules). - Named variables in Quaternion.cs are not strictly necessary, but you can make the code much more readable with them than without it.
- Named variables in ext4 / resize.c are not constants. They are called counters. Their names obscure their functions a bit, but this code actually does correctly conform to the specialized coding standards of the project.
What happens to Quaternion.cs?
This is pretty easy.
Right after that:
double zero = 0; double one = 1;
The code does the following:
return zero.GetHashCode() ^ one.GetHashCode();
Without local variables, what does the alternative look like?
return 0.0.GetHashCode() ^ 1.0.GetHashCode(); // doubles, not ints!
What a mess! Readability is definitely on the side of creating local people here. Moreover, I think that the explicit notation of the variables indicates “We thought carefully about this” much more clearly than just writing one confusing return statement.
What happens with resize.c?
In the case of ext4 / resize.c, these numbers are not constants at all. If you follow the code, you will see that they are counters, and their values ​​actually change over several iterations of the while loop.
Notice how they are initialized :
unsigned three = 1; unsigned five = 5; unsigned seven = 7;
Three are one, right? What does it mean?
Look what actually happens that update_backups passes these variables by reference to the ext4_list_backups function:
static unsigned ext4_list_backups(struct super_block *sb, unsigned *three, unsigned *five, unsigned *seven)
These are counters that are stored for several calls. If you look at the body of the function , you will see that it is juggling with counters to find the next power 3, 5 or 7, creating the sequence that you see in the comment: 1, 3, 5, 7, 9, 25, 27, and c .
Now, for the strangest part: the three variable is initialized to 1 because 3 0 = 1. Power 0 is a special case, though, since it is the only time 3 x = 5 x = 7 x . Try your hand at rewriting ext4_list_backups to work with all three counters initialized to 1 (3 0 5 0 7 0 ), and you will see how much more cumbersome the code becomes. Sometimes it’s easier to just tell the caller to do something funky (initialize the list to 1, 5, 7) in the comments.
So, five = 5 good coding style?
Is "five" a good name for what the five variable represents in resize.c? In my opinion, this is not a style that you should imitate only in any random project that you accept. The simple name five does not indicate the purpose of the variable. If you are working on a web application or quickly prototyping a video chat client or something similar and decide to name the variable five , you are probably going to create headaches and annoyances for everyone who needs to maintain and modify your code.
However, this is one example where general programming information does not reflect the full picture . Take a look at the kernel coding style document , especially in the naming chapter.
GLOBAL variables (which will only be used if you really need them) have descriptive names, as well as global functions. If you have a function that counts the number of active users, you should call it "count_active_users ()" or the like, you should not call it "cntusr ()".
...
Local variable names must be short and precise. If you have some random integer counter, it should probably be called "i." Calling loop_counter is not productive if there is no chance of it being misunderstood. Similarly, "tmp" can be just about any type that is used to store a temporary value.
If you are afraid to mix the names of local variables, you have another problem called function-growth-hormone-imbalance syndrome. See Chapter 6 (Functions).
Part of this is the C-style coding tradition. Part of this is focused social engineering. A lot of kernel code is sensitive material, and it has been reviewed and tested many times. Since Linux is a large open source project, it is not very painful for contributions - in most cases, the biggest problem is to check these contributions for quality.
Calling this five variable instead of something like nextPowerOfFive is a way to discourage contributors from interfering with code that they don't understand. This is an attempt to get you to really read the code that you are modifying in detail, in turn, before trying to make any changes.
Did the kernel developers help with the right solution? I can not tell. But this is clearly a targeted move.