Products > Programming
Goto bad Spooktober tale: Counting goto in the Linux kernel
RoGeorge:
Current version of the Linux kernel at https://www.kernel.org/ is 6.5.9, download and unpack:
--- Code: --- wget [url]https://cdn.kernel.org/pub/linux/kernel/v6.x/linux-6.5.9.tar.xz[/url]
tar -xvf linux-6.5.9.tar.xz
--- End code ---
Count all lines
--- Code: --- find ./linux-6.5.9 -name '*.c' | xargs cat | wc -l
--- End code ---
23174856 ???
Count the goto lines
--- Code: --- find ./linux-6.5.9 -name '*.c' | xargs cat | grep -c 'goto '
--- End code ---
193266 :scared:
Save them in a file for further inspection
--- Code: --- find ./linux-6.5.9 -name '*.c' | xargs cat | grep 'goto ' > goto.txt
--- End code ---
Most of them are for treating errors :P
Counting again, without goto err/out/fail
--- Code: --- find ./linux-6.5.9 -name '*.c' | xargs cat | grep 'goto ' | egrep -vc 'goto err|goto out|goto fail'
--- End code ---
73786 :o
Siwastaja:
goto is a normal part of C and trying to avoid using it for political reasons leads to less readable spaghetti code. This is well known but some beginners still misunderstand some old tongue-in-cheek articles taken out of context.
Now for C++, amount of goto will be less as there are other ways for error handling (arguably better, arguably worse), but goto has its uses even in C++.
DiTBho:
What's the point here?
The linux kernel is not a good reference neither about programming style, nor about how a kernel should be written.
Linux is a kernel developped in the 90s that practically "works", and it "works" because there are a lot of people who follow it and are willing to fix it, just look at how many interactions there are every time you go into regression.
Linux v6 stopped correctly booting on PPC-G4 and G5 PowerMacs, and dozens of messages have been raining into the mailing lists for 5 months every single day... and this is precisely the correlation between writing software with lots of gotos and that approach, or writing software with a completely different approach: the first difference you notice is the number of iterations necessary to solve a bug.
Linux is certainly not written well from this point of view as it barely passes the level DO178B level E, which I remind you must be read in terms of "software life cycle" and "resources necessary for development, maintenance and bug fixing". It does not practically matter for the final user, especially for mainstream platforms (x86, amd64, arm, arm64) due to the mechanism with which it is supported by large communities, but things change dramatically when you are alone developing something or solving a problem.
DiTBho:
--- Quote from: Siwastaja on October 29, 2023, 07:37:46 am ---goto is a normal part of C and trying to avoid using it for political reasons leads to less readable spaghetti code. This is well known but some beginners still misunderstand some old tongue-in-cheek articles taken out of context.
--- End quote ---
:-DD
I've never read anything so funny considering that it shows that it's quicker to write just any bullshit (political reasons ? LOL) than to waste time explaining things well: why waste time on a forum when the typical hourly Windirver consulting rate for DO178B/Level A is 100/hr at least? The fewer people know how to do it, the more opportunities you have to bring home wheelbarrows of gold.
magic:
IMO by far the biggest problem with Linux code is very little documentation (which also suggests the distressing possibility that there may be very little design to it as well :scared:).
It's like those assholes have never heard of comments.
Of course I can read the code and see what it does, but the problem is that usually I'm reading code because it's doing something it shouldn't be doing, and good luck guessing what's the intended operation of every little part and which of those parts is wrong.
I couldn't care less about goto.
Navigation
[0] Message Index
[#] Next page
Go to full version