TED talk slides
Recently I watched Linus Torvalds’ TED talk on YouTube (go to 14:22 for the taste test) and in the middle of the talk, he said something about coding taste. There are 2 images on the screen, the first one is poor taste, which is the one taught on early computer science course, and the second one is the good taste, which he prefers.


Both are C-like pseudocode and not real code, but they still deliver the message. Linus said that in good code:
Sometimes you can see a problem in a different way and rewrite it so that a special case goes away and becomes the normal case.
We can see the special case he mentioned, which is the final if statement. The good code doesn’t have it.
I’m actually more familiar with the first poor taste code since I went to CS class back in uni, and I have 0 clue on how the good taste code is supposed to work. All those pointers and addresses are too arcane for now. So I decided to understand that just to cross off an old entry on my bucket list.
Before we go to the implementation, let’s understand what makes the poor taste poor and the good taste good. I think a little bit of interactivity can help.
The special case
A singly linked list is a non-contiguous data structure. The nodes are sprawled on the heap. That means, we have to walk from the beginning to iterate the nodes. For now, focus on just the walking.
The poor taste code walks by having prev, a pointer that trails one step behind the current node named walk. The prev is needed since prev->next might need update/maintenance to keep the list in sync.
// Walk the listNode* prev = NULL;Node* walk = head;while (walk != entry) { prev = walk; walk = walk->next;}
// Remove the entry by updating the// head or the previous entryif (!prev) head = entry->next;else prev->next = entry->next;Pay attention to prev since after the walk, we might update prev->next. Notice that if entry is on the head, prev points to null, while on later entries, prev points to an actual object.
- entry on 1st node:
previs null, loop never runs (this is the special case) - entry on 2nd node:
previs not null - entry on 3rd node:
previs not null - entry on nth node:
previs not null
When prev is null, we cannot update prev->next, so we have to update head. This very existence of special case in this problem is what Linus calls “not very good taste”.
Now, how about the better tasting code? There is no prev anymore, instead we have the only walking variable called indirect. Note that indirect is not a regular pointer, but a pointer to pointer (hence the different arrow style).
// Walk the list, looking for the thing that// points to the entry we want to removeNode** indirect = &head;while ((*indirect) != entry) indirect = &(*indirect)->next;
// .. and just remove it*indirect = entry->next;Every step now looks the same, there is no special case at all. indirect always points to a pointer. Good taste code treats the head as if it was a regular next pointer.
- entry on 1st node:
indirectpoints at a pointer - entry on 2nd node:
indirectpoints at a pointer - entry on 3rd node:
indirectpoints at a pointer - entry on nth node:
indirectpoints at a pointer
Since everything looks the same, the removal is just one statement. Also, indirect has this nice ability of walking and updating head at the same time since it is Node**, writing through *indirect will update the head too.
Implementation
The first thing I did was ask Claude to make me a singly linked list API but leave the implementation for me to write myself. This is because I’m curious whether indirect trick can be implemented far beyond removing a list entry or not.
#pragma once#include <stdbool.h>#include <stdlib.h>
typedef struct Node Node;struct Node { int val; Node* next;};
Node* node_new(int val);void list_print(const Node* const head);size_t list_len(const Node* head);void list_free(Node** head);void push_front(Node** head, int val);void push_back(Node** head, int val);bool insert_at(Node** head, size_t idx, int val);Node* find(Node* head, int val);bool pop_front(Node** head, int* out);bool remove_value(Node** head, int val);Turns out, after implementing both the poor and good taste, the indirect trick can be used on many other methods as well. The rule of thumb: the functions that walk and possibly rewire head are good candidates for using indirect trick, which are push_back(...), insert_at(...), and remove_value(...).
I will show only the insert_at because this is the most fun one to implement. The full code is on GitHub.
bool insert_at(Node** head, size_t idx, int val) { // walk until we find a stopping point Node* prev = NULL; Node* walk = *head; size_t curr_idx = 0; while (!(walk == NULL || curr_idx == idx)) { prev = walk; walk = walk->next; curr_idx += 1; } if (curr_idx != idx) { return false; } Node* new_node = node_new(val); new_node->next = walk; if (prev == NULL) { // push front special case *head = new_node; } else { // general case prev->next = new_node; } return true;}bool insert_at(Node** indirect, size_t idx, int val) { // walk the indirect until we find a stopping point size_t curr_idx = 0; while (!(*indirect == NULL || curr_idx == idx)) { indirect = &(*indirect)->next; curr_idx += 1; } if (curr_idx != idx) { return false; } Node* new_node = node_new(val); // everything is general case new_node->next = *indirect; *indirect = new_node; return true;}Sentinel node
Linus does not mention sentinel node on the TED talk, but I think sentinel/dummy node is a cool alternative worth knowing. Can we somehow skip the Node** ceremonies and just make the prev never null? Turns out that is possible.
bool insert_at(Node** head, size_t idx, int val) { // make dummy list on the stack that points to the front Node dummy = {.val = -1, .next = *head}; Node* prev = &dummy; Node* walk = dummy.next; // walk until we find a stopping point size_t curr_idx = 0; while (!(walk == NULL || curr_idx == idx)) { prev = walk; walk = walk->next; curr_idx += 1; } if (curr_idx != idx) { return false; } Node* new_node = node_new(val); new_node->next = walk; prev->next = new_node; *head = dummy.next; return true;}Still less elegant than the indirect trick, but this might be easier to think about. Note that we must think carefully about the sentinel node on the stack. In this singly linked list case, it works fine because nothing in the list points back to the dummy. On doubly linked list tho, we need to include the sentinel node on the list itself to make sure no dangling pointer issues, since stack lifetime ends when the function returns.