Live data from Hacker News

Linus Torvalds' good taste argument for linked lists, explained

github.com

241–250 of 339 posts

Re: Linus Torvalds' good taste argument for linked lists, explained

#241
What Torvalds made is not a good taste argument for linked lists — he made a linked list argument for good taste. Or rather, a linked list argument about what good taste means.

I assume the author of this article had spent too much time being confused by the ‘Windows Subsystem for Linux’ nomenclature.

Re: Linus Torvalds' good taste argument for linked lists, explained

#242

Earlier quoted context omitted.

That's a pretty big freakin thing to leave out, though. Code that depends on its context is, some would say, bad.

This is why Linus' code shouldn't be copied verbatim and used in the real world. The Kernel is an extremely isolated and contained system. At the very least, the code should add comments on the the assumptions that are vital...otherwise n00bs will copy the code as gospel and run into all sorts of problems.

The actual code in the kernel is well documented (and different, lines 103-149): https://github.com/torvalds/linux/blob/master/include/linux/...

Re: Linus Torvalds' good taste argument for linked lists, explained

#245
The following is the code I wrote before reading the example pieces of code.

    void remove(IntList* l, IntListItem* target) {
      if (l->head == target) {
        l->head = l->head->next;
        return;
      }
      IntListItem* prev = l->head;
      while (prev->next != target) prev = prev->next;
      prev->next = prev->next->next;
    }
Skimming the comments here, I was surprised not to see an equivalent piece of code mentioned. To me my code is more readable than both of the first and second examples presented in the article. Does that mean my taste is peculiar?

Re: Linus Torvalds' good taste argument for linked lists, explained

#246
post #193

Earlier quoted context omitted.

My taste is that function level comments describe the "what" and inline comments describe the "why". So at a function level the comments is giving a highlevel description to the reader as to the functionality contained, and at the line level comments exist only to say describe the programmers intentionality, why it was implemented this way rather than some other way. I have generally found (there are always exception…

The main takeaway here might be that two 25 year veterans both make function level and line level comments. The particulars are somewhat of a matter of flavor preference :)

That is not a particularly useful takeaway, especially when one of them is pointing out that particular comments in a function are problematic.

The particulars matter.

Re: Linus Torvalds' good taste argument for linked lists, explained

#247
This keeps getting reposted/rehashed regularly. And the code is there for those wanting to look into system include headers, like sys/queue.h (I haven't actually checked for chicken and egg scenario in commit history of various libc implementation, but would hope that double pointer has always been there)

Re: Linus Torvalds' good taste argument for linked lists, explained

#248

I'm not sure why the article removed comments from the code and replaced variable names like "indirect" with "p". Here are the two code samples verbatim from Linus's presentation: remove_list_entry(entry) { prev = NULL; walk = head; // Walk the list while (walk != entry) { prev = walk; walk = walk->next; } // Remove the entry by updating the // head or the previous entry if (!prev) head = entry->next; else prev->next…

Hacker news would benefit from code blocks.

Hacker news has code blocks. Have the block indented by for spaces.

    this line has 4 spaces in front of it.
What Hacker news could use instead are quote blocks. Now people often use the code blocks as quote blocks.

Re: Linus Torvalds' good taste argument for linked lists, explained

#249

The following is the code I wrote before reading the example pieces of code. void remove(IntList* l, IntListItem* target) { if (l->head == target) { l->head = l->head->next; return; } IntListItem* prev = l->head; while (prev->next != target) prev = prev->next; prev->next = prev->next->next; } Skimming the comments here, I was surprised not to see an equivalent piece of code mentioned. To me my code is more readable t…

This is exactly the right answer.

There are two cases here (1. when the target is at the front of the list and necessitates changing the head of list, and 2. when the head doesn't need to be changed.) You handle both the cases separately.

Both the classical and the "elegant" versions are worse than this one.

Re: Linus Torvalds' good taste argument for linked lists, explained

#250

Let's draw a list IntListItem -> IntListItem -> IntListItem and then draw the position of the list head: IntList -> IntListItem -> IntListItem -> IntListItem You can see that the if-branch in the cs101 answer comes because there is a ('virtual') element of the list (the IntList head) that is different from the other elements of the list. If we were to make them the same (C# code): interface IListItem { IntListItem Ne…

You've just introduced the extra indirection of virtual function pointers, perhaps through an extra indirection layer of interfaces, which will be a big performance hit, so you've hardly hit upon the "real issue".
Post reply on HN