Linked lists, pointer tricks, and good taste ↦
A conceptual explanation of two ways to implement a linked list and why Linus Torvalds believes one implementation has good taste:
[…] I don’t want you to understand why it doesn’t have the if statement. But I want you to understand that 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, and that’s good code
I haven’t considered linked list particulars for a long time (no job interviews lately, I guess 😜), but I found this explanation clear and illuminating. Maybe you will too.
Sign in or Join to comment or subscribe
If I want a future reader of my code to glance at it and immediately understand what’s happening, so they can move along to whatever it is they actually need to be doing, I’m going with the cs101 answer.
Linus’ thing puts me in mind of code golf. Can be entertaining, but I’m not doing that in production code.
Jerod co-hosts The Changelog, crashes JS Party, and takes out the trash (his old code) once in awhile.
I agree with you. If the
ifbranch implementation were more complicated and messy than it is, I could see the argument that Linus’ preference would actually be more straight forward. But it’s dead simple.
What I appreciate about thought exercises like this is to be able to see multiple ways of accomplishing the same thing. In this case the difference is marginal and a matter of taste, but in some cases a different way of approaching a problem can be the difference between success and failure (or maintainable code and unmaintainable code).