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.
Discussion
Sign in or Join to comment or subscribe
NP Incomplete
2020-12-08T16:09:48Z ago
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 Santo
Bennington, Nebraska
Jerod co-hosts The Changelog, crashes JS Party & takes out the trash (his old code) once in awhile.
2020-12-09T14:49:33Z ago
I agree with you. If the
if
branch 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).