Two star programming -- Linus Torvalds
2016-02-01 00:00
183 查看
原文地址:http://wordaligned.org/articles/two-star-programming
中文地址:http://www.csdn.net/article/2013-01-10/2813559-two-star-programming
A few weeks ago Linus Torvalds answered some questions on slashdot. All his responses make good reading but one in particular caught my eye. Asked to describe his favourite kernel hack, Torvalds grumbles he rarely looks at code these days — unless it’s to sort out someone else’s mess. He then pauses to admit he’s proud of the kernel’s fiendishly cunning filename lookup cache before continuing to moan about incompetence.
At the opposite end of the spectrum, I actually wish more people understood the really core low-level kind of coding. Not big, complex stuff like the lockless name lookup, but simply good use of pointers-to-pointers etc. For example, I’ve seen too many people who delete a singly-linked list entry by keeping track of the
and whenever I see code like that, I just go “This person doesn’t understand pointers”. And it’s sadly quite common.
People who understand pointers just use a “pointer to the entry pointer”, and initialize that with the address of the list_head. And then as they traverse the list, they can remove the entry without using any conditionals, by just doing a
Well I thought I understood pointers but, sad to say, if asked to implement a list removal function I too would have kept track of the previous list node. Here’s a sketch of the code:
This person doesn’t understand pointers
The linked list is a simple but perfectly-formed structure built from nothing more than a pointer-per-node and a sentinel value, but the code to modify such lists can be subtle. No wonder linked lists feature in so many interview questions!
The subtlety in the implementation shown above is the conditional required to handle any nodes removed from the head of the list.
Now let’s look at the implementation Linus Torvalds had in mind. In this case we pass in a pointer to the list head, and the list traversal and modification is done using a pointer to the next pointers.
Two star programming
Much better! The key insight is that the links in a linked list are pointers and so pointers to pointersare the prime candidates for modifying such a list.
§
The improved version of
中文地址:http://www.csdn.net/article/2013-01-10/2813559-two-star-programming
A few weeks ago Linus Torvalds answered some questions on slashdot. All his responses make good reading but one in particular caught my eye. Asked to describe his favourite kernel hack, Torvalds grumbles he rarely looks at code these days — unless it’s to sort out someone else’s mess. He then pauses to admit he’s proud of the kernel’s fiendishly cunning filename lookup cache before continuing to moan about incompetence.
At the opposite end of the spectrum, I actually wish more people understood the really core low-level kind of coding. Not big, complex stuff like the lockless name lookup, but simply good use of pointers-to-pointers etc. For example, I’ve seen too many people who delete a singly-linked list entry by keeping track of the
preventry, and then to delete the entry, doing something like
if (prev) prev->next = entry->next; else list_head = entry->next;
and whenever I see code like that, I just go “This person doesn’t understand pointers”. And it’s sadly quite common.
People who understand pointers just use a “pointer to the entry pointer”, and initialize that with the address of the list_head. And then as they traverse the list, they can remove the entry without using any conditionals, by just doing a
*pp = entry->next.
Well I thought I understood pointers but, sad to say, if asked to implement a list removal function I too would have kept track of the previous list node. Here’s a sketch of the code:
This person doesn’t understand pointers
typedef struct node { struct node * next; .... } node; typedef bool (* remove_fn)(node const * v); // Remove all nodes from the supplied list for which the // supplied remove function returns true. // Returns the new head of the list. node * remove_if(node * head, remove_fn rm) { for (node * prev = NULL, * curr = head; curr != NULL; ) { node * const next = curr->next; if (rm(curr)) { if (prev) prev->next = next; else head = next; free(curr); } else prev = curr; curr = next; } return head; }
The linked list is a simple but perfectly-formed structure built from nothing more than a pointer-per-node and a sentinel value, but the code to modify such lists can be subtle. No wonder linked lists feature in so many interview questions!
The subtlety in the implementation shown above is the conditional required to handle any nodes removed from the head of the list.
Now let’s look at the implementation Linus Torvalds had in mind. In this case we pass in a pointer to the list head, and the list traversal and modification is done using a pointer to the next pointers.
Two star programming
void remove_if(node ** head, remove_fn rm) { for (node** curr = head; *curr; ) { node * entry = *curr; if (rm(entry)) { *curr = entry->next; free(entry); } else curr = &entry->next; } }
Much better! The key insight is that the links in a linked list are pointers and so pointers to pointersare the prime candidates for modifying such a list.
§
The improved version of
remove_if()is an example of two star programming: the doubled-up asterisks indicate two levels of indirection. A third star would be one too many.
相关文章推荐
- Mac终端下,一些常见快捷键
- 微博UI
- 下拉控价Spinner的使用
- Linux 帐号管理
- Codeforces Round #341 (Div. 2) [Codeforces621]
- ntpdate——linux firefox 访问163邮箱 失败 https proload
- 初步介绍PHP扩展开发经验分享
- IMUNES—Integrated Multiprotocol Network Emulator/Simulator
- has been mounted times without being checked, check forced
- Android调用默认浏览器打开指定Url的方法实例
- 批量将 *.c 预处理为 *.i (递归处理文件夹中所有文件)gcc -E
- Criteria.Tools.Executor————测试
- Linux 0.11内核编译和bochs上的实验环境的搭建
- 用C/C++扩展你的PHP 为你的php增加功能
- Android NDK and pthread
- (OK-HALF) To Find a Rogue DHCP Server—tcpdump/dhclient—nmap
- linux-0.11内核调试运行阅读环境的搭建及使用
- acl 3.1.2版本发布,网络通信与服务器编程框架
- (OK) Linux epoll模型—socket epoll server client chat
- 格式化 C/C++语言源码——命令indent