Earlier quoted context omitted.
If this example is too "clever" for someone, even sight unseen, they aren't (yet?) cut out to be a kernel hacker. I don't disagree with your general principle, but the linked example is idiomatic and understandable C for someone with a decent knowledge of the language, which is (I'd imagine/hope) the standard expected of contributors to Linux.
Actually I believe this particular example is basic and pretty understandable C. I was talking about the more broad idea of knowingly writing clever code to raise the entry barrier for new devs. Or just writing clever code for whatever the reason. It's almost guaranteed to backfire, to new devs who will have to maintain it or to yourself, when you will have to read it in a less smart moment.
Applying the Linus Torvalds “Good Taste” Coding Requirement
211–220 of 302 posts
Re: Applying the Linus Torvalds “Good Taste” Coding Requirement
#212I dunno, the first example seems unsatisfying. The original has that ugly condition, the "good" version seems overly clever. And for all the talk of taste and aesthetics, both versions ignore an elephant in the room: defensively dealing with `entry` being absent from the list. Not that I've never abused addresses like this. But having written this multiple times, I currently prefer something like this: remove_list_en…
Independently of style, taste, BigCo, edge cases, assumptions and other considerations, I still find a major regression in your version: Instead of one variable allocation and one test in the loop, your version has one allocation and TWO tests. Adding weight in loops has a major performance impact.
I actually tend to be pretty lax about coding style, believe it or not. But dereferencing NULL is undefined behavior.
[1] My guru in this regard is DJ Bernstein: https://cr.yp.to/qmail/qmailsec-20071101.pdf
Re: Applying the Linus Torvalds “Good Taste” Coding Requirement
#213Earlier quoted context omitted.
not true at all.... kernel developers are not mythical beasts, they're just regular low-level developers. Nothing special. I've got 20 years experience developing this kind of stuff and to me, its not as simple as it could be, therefore it could be better.
You have 20 years of experience on this kind of stuff and don't recognize the textbook example code for handling the head pointer case of a linked list? Well, okay... (It's at least 23 years old because I learned from textbooks that have this example code way back then. It probably dates all the way back to K&R.)
The point isn't "can I understand it"...
the point is "Would someone who hadn't seen it before have to do another mental operation to understand it?"
...and yes... dereferencing is another operation you have to do in your head to understand it.
Re: Applying the Linus Torvalds “Good Taste” Coding Requirement
#214"The cheapest, fastest, and most reliable components are those that aren’t there."[1]
That quote is a reminder to me that there is a certain artistry behind engineering anything.
Re: Applying the Linus Torvalds “Good Taste” Coding Requirement
#215I dunno, the first example seems unsatisfying. The original has that ugly condition, the "good" version seems overly clever. And for all the talk of taste and aesthetics, both versions ignore an elephant in the room: defensively dealing with `entry` being absent from the list. Not that I've never abused addresses like this. But having written this multiple times, I currently prefer something like this: remove_list_en…
Bog-standard, humdrum enterprise programming != kernel programming When every branch removed on a mainline path in the kernel saves years of compute time globally and every byte removed in the kernel saves terabytes of memory globally, being clever is absolutely the right thing to do. Removing that one branch takes away an expensive pipeline stall[0]. The version akkartik has above adds a extra branch (and expensive…
Re: Applying the Linus Torvalds “Good Taste” Coding Requirement
#216For my money, Linus's example of "good taste" gives up rather a lot of clarity to achieve succinctness. The original is simple and clear. His preferred version is shorter, but also harder to understand because of its use of a complicated indirection. And that's not good taste. It's just showing off. “Programs must be written for people to read, and only incidentally for machines to execute.” ― Harold Abelson, Structu…
Re: Applying the Linus Torvalds “Good Taste” Coding Requirement
#217Earlier quoted context omitted.
For heaven's sake, everyone, please stop getting hung up on the fact that there's no error checking. Example code usually doesn't show error checking because it obscures the main point of the example.
Not shown: assertions :-) (which, for kernal code, would be turned off later, anyway) In languages other than C, with exceptions and stack traces, most precondition checks just become clutter, anyway. Although it would be nice to have "Eiffel" style pre/post-condition (invariant assertion) blocks out-of-line that can be enabled or disabled...
Re: Applying the Linus Torvalds “Good Taste” Coding Requirement
#218I know Sufficiently Smart Compilers are a running joke, and I know a lot of people think the idea is a bad one, but come on...this should be the job of a compiler. The first example is way more understandable. Yes, it branches, and yes it is more code and less efficient. But a compiler should be able to derive the more efficient version from the more understandable code. And it's a shame that it doesn't.
In fact if you read the article there's no claim being made that the second version is more efficient, and I don't think it is
Re: Applying the Linus Torvalds “Good Taste” Coding Requirement
#219I dunno, the first example seems unsatisfying. The original has that ugly condition, the "good" version seems overly clever. And for all the talk of taste and aesthetics, both versions ignore an elephant in the room: defensively dealing with `entry` being absent from the list. Not that I've never abused addresses like this. But having written this multiple times, I currently prefer something like this: remove_list_en…
I don't think anyone's mentioned that there are two functional benefits to Linus's version: testability and performance. The author touched on testability. Dropping the "if" case eliminates one code path that may contain bugs. But also, the revised code may perform a lot better if you're searching a lot of short lists. In that case, the target item has a large, though not likely, chance of being first in the list. Th…
In the better version, why doesn't the while condition (that must be tested at least once) present predictability problems whereas the if check in parent does?
Re: Applying the Linus Torvalds “Good Taste” Coding Requirement
#220Both versions suck. If "entry" is not found in the list, the code will run off the end of the list, either de-referencing zero and faulting or going off into junk, depending on how the list ends. Writing low-level list manipulation more than once sucks. It leads to bugs. Such manipulation should be encapsulated. That's why we have containers in modern languages. The Linux kernel is still C, not C++, which leads to to…