TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

`three = 1` in the linux sourcecode

176 pointsby shark234about 11 years ago

18 comments

lifthrasiirabout 11 years ago
<a href="https://github.com/torvalds/linux/blob/d158fc7f36a25e19791d25a55da5623399a2644f/fs/ext4/resize.c#L652" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;torvalds&#x2F;linux&#x2F;blob&#x2F;d158fc7f36a25e19791d2...</a><p>Variables `three`, `five` and `seven` are better described as `next_power_of_three`, `next_power_of_five` and `next_power_of_seven`. Since the `ext4_list_backups` function should iterate through 1 (= 3^0 = 5^0 = 7^0), 3, 5, 7, 3^2, 5^2, 3^3, 7^2, ... and 1 should not repeat three times, the initial value of `next_power_of_three` (or any of others) should be 1 and those of others should not be 1. The naming is a bit unfortunate (couldn&#x27;t they be `threes` etc., for example?) but actually makes sense.
评论 #7296686 未加载
simiasabout 11 years ago
Read the comment above ext4_list_backups right above:<p><a href="https://github.com/torvalds/linux/blob/d158fc7f36a25e19791d25a55da5623399a2644f/fs/ext4/resize.c#L652" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;torvalds&#x2F;linux&#x2F;blob&#x2F;d158fc7f36a25e19791d2...</a><p><pre><code> &#x2F;* * Iterate through the groups which hold BACKUP superblock&#x2F;GDT copies in an * ext4 filesystem. The counters should be initialized to 1, 5, and 7 before * calling this for the first time. In a sparse filesystem it will be the * sequence of powers of 3, 5, and 7: 1, 3, 5, 7, 9, 25, 27, 49, 81, ... * For a non-sparse filesystem it will be every group: 1, 2, 3, 4, ... *&#x2F; </code></pre> I&#x27;m not sure what&#x27;s so noteworthy about that. There&#x27;s ton of arcane code in any kernel, as long as it&#x27;s commented correctly there&#x27;s no issue.
评论 #7296613 未加载
评论 #7297845 未加载
评论 #7296747 未加载
keypusherabout 11 years ago
If only there was a system for fixing the code yourself instead of having to post about it to a widely read tech site.
评论 #7296888 未加载
cauliturtleabout 11 years ago
&gt; There are only two hard things in Computer Science: cache invalidation and naming things.<p>&gt; -- Phil Karlton
评论 #7296687 未加载
评论 #7296633 未加载
评论 #7296634 未加载
koverstreetabout 11 years ago
&#x2F;yawn<p><a href="https://github.com/torvalds/linux/blob/d158fc7f36a25e19791d25a55da5623399a2644f/drivers/md/bcache/bset.c#L406" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;torvalds&#x2F;linux&#x2F;blob&#x2F;d158fc7f36a25e19791d2...</a>
评论 #7298038 未加载
jakobeabout 11 years ago
Lot&#x27;s of people suggesting to add explanatory comments. I don&#x27;t think that&#x27;s a good idea, it would be better to change the names of the variables:<p><pre><code> unsigned counter1 = 1; unsigned counter2 = 5; unsigned counter3 = 7; </code></pre> or use an array:<p><pre><code> unsigned counter[3] = {1,5,7}; </code></pre> No more confusion. That being said, I doubt that the names of these local variables is actually a real source of confusion and is not a problem that needs to be fixed.
评论 #7298767 未加载
adobriyanabout 11 years ago
Why don&#x27;t you all &quot;deserves better comment&quot; people send a patch?
评论 #7297097 未加载
评论 #7296823 未加载
userbinatorabout 11 years ago
That&#x27;s a very... awkward of doing quite a simple thing, generating ascending interleaved powers. I&#x27;d probably use one or more arrays of multipliers to maintain the counters. Simpler and more general -- not often that you can get a solution with both these two attributes.
raverbashingabout 11 years ago
And here&#x27;s how the &quot;rainbows and butterflies&quot; world of &quot;code perfectionists&quot; come crashing down.<p>Yes, the naming is unfortunate. No, there&#x27;s probably no way to make it better (and if you think code reviews suck, wait until you see a kernel code review) (Well, this came directly from Linus but there&#x27;s usually some discussion as well)<p>But in the end this has shipped and is running. (Well, the double goto from Apple as well, but I doubt that went through a code review)
jherikoabout 11 years ago
there are many, many problems in this code style which lead to this kind of problem.<p>localised comments may help but naming things properly would help more (and negate a comment being required at all), actually adopting some better practices would be a better fix going forward.<p>looking at the called function&#x27;s comment:<p>The counters should be initialized to 1, 5, and 7 before * calling this for the first time.<p>so how about making a function with the same name, ending in &#x27;_first&#x27; (maybe rename the original _next) which initialises these variables internally before calling the version intended to be called multiple times...?<p>since the parameters are not well described by their names renaming them would help too... it is not a pointer to a 3 a 5 or a 7. it might not even be pointing at multiples... but current_multiple_of_three etc. would be better. there is some reason why these values are significant in this algorithm (which I do not know) and the best names would capture that.<p>the code is moderately difficult to read in the function itself - the method of swapping around a local copy of a pointer based on the conditions is not very easy to follow. although makes sense here to avoid excessive code... comments would help make it clearer what is going on.
yusukeshinyamaabout 11 years ago
At line 655: &quot;... In a sparse filesystem it will be the sequence of powers of 3, 5, and 7: ...&quot;<p>So I think this means three = pow(3, 0);
评论 #7296605 未加载
chrisBobabout 11 years ago
Wasn&#x27;t there a similar thread a few months back about &quot;two&quot; being corrected to equal 2?
评论 #7296817 未加载
Vextasyabout 11 years ago
Seems like a perfectly reasonable naming scheme to me. Anyone foolish enough to think that the variables are there to hold the values 3, 5 and 7 is probably wasting their time on the code in the first place.<p>Besides, it made you read the code and the comments.
blueskin_about 11 years ago
Reminds me of &#x27;how to write unmaintainable code&#x27;:<p><a href="http://mindprod.com/jgloss/unmaincamouflage.html" rel="nofollow">http:&#x2F;&#x2F;mindprod.com&#x2F;jgloss&#x2F;unmaincamouflage.html</a>
athenagorasabout 11 years ago
Trinitarian theology?
Trindazabout 11 years ago
I finally have an answer to the &quot;what&#x27;s your favorite line of linux source code&quot; question.
评论 #7298417 未加载
zamalekabout 11 years ago
Is this because there is some inconsistency in the GDT documentation?
wazooxabout 11 years ago
That deserves an explanation in the code comments...
评论 #7296624 未加载
评论 #7296674 未加载