[Good Programming habits] reuse code to improve maintainability

Source: Internet
Author: User

Code reuse has two levels in software development. The first level is to reuse existing software modules when designing a new software function or developing a new project. This Reuse may be called better design reuse. Another layer is that when programmers develop a software module, the module should be reused as much as possible. From the perspective of programming habits, this refers to the latter.

Assume that there is a module of a Double-Linked List, DLL) two-way Linked List. If this module already has two functions during development, namely, dll_push_tail () and dll_pop_head (). These two functions Add a new node to the end of the linked list, delete the node from the linked list, and return the header node. The Code Implementation 1 is shown.

Dll. c
00088: void dll_push_tail (dll_t * _ p_dll, dll_node_t * _ p_node)
00089 :{
00090: if (0 = _ p_dll-> tail _){
00091: _ p_dll-> head _ = _ p_dll-> tail _ = _ p_node;
00092: _ p_node-> next _ = _ p_node-> prev _ = 0;
00093 :}
00094: else {
00095: dll_node_t * p_tail = _ p_dll-> tail _;
00096:
00097: p_tail-> next _ = _ p_node;
00098: _ p_node-> prev _ = p_tail;
00099: _ p_node-> next _ = 0;
00100: _ p_dll-> tail _ = _ p_node;
00101 :}
00102:
00103: _ p_dll-> count _ ++;
00104 :}
00105:
00106: dll_node_t * dll_pop_head (dll_t * _ p_dll)
00107 :{
00108: dll_node_t * p_node = _ p_dll-> head _;
00109:
00110: if (p_node! = 0 ){
00111: _ p_dll-> count _--;
00112: _ p_dll-> head _ = p_node-> next _;
00113: if (0 = _ p_dll-> head _){
00114: _ p_dll-> tail _ = 0;
00115 :}
00116: else {
00117: p_node-> next _-> prev _ = 0;
00118 :}
00119 :}
00120:
00121: return p_node;
00122 :}
Figure 1

If you need to add a new linked list operation function dll_merge () to merge two linked lists. The implementation of this function may be 2. The idea is also very simple, that is, to extract nodes from the _ p_src linked list and put them at the end of the _ p_dest linked list.

Dll. c
00165: void dll_merge (dll_t * _ p_dest, dll_t * _ p_src)
00166 :{
00167: dll_node_t * p_node = _ p_src-> head _;
00168:
00169: while (0! = P_node ){
00170: if (0 = _ p_dest-> tail _){
00171: _ p_dest-> head _ = _ p_dest-> tail _ = p_node;
00172: _ p_dest-> next _ = _ p_dest-> prev _ = 0;
00173 :}
00174: else {
00175: dll_node_t * p_tail = _ p_dest-> tail _;
00176:
00177: p_tail-> next _ = _ p_dest;
00178: _ p_dest-> prev _ = p_tail;
00179: _ p_dest-> next _ = 0;
00180: _ p_dest-> tail _ = _ p_dest;
00181 :}
00182:
00183: _ p_dest-> count _ ++;
00184: p_node = p_node-> next _;
00185 :}
00186:
00187: _ p_src-> count _ = 0;
00188: _ p_src-> head _ = 0;
00189: _ p_src-> tail _ = 0;
00190 :}
Figure 2

Is there a problem? From the functional point of view, there is no problem, but from the maintainability point of view, this implementation is not good, instead, a better implementation is through code reuse, as shown in 3.

Dll. c
00175: void dll_merge (dll_t * _ p_dest, dll_t * _ p_src)
00176 :{
00177: dll_node_t * p_node = dll_pop_head (_ p_src );
00178:
00179: while (0! = P_node ){
00180: dll_push_tail (_ p_dest, p_node );
00181: p_node = dll_pop_head (_ p_src );
00182 :}
00183 :}
Figure 3

Obviously, the code reuse method is easier to read and maintain. When implementing a software module, we should consider extracting some common basic functions from the required functions, such as the dll_pop_head () and dll_push_tail () mentioned here ()), and the functions implemented by these functions are orthogonal, that is, the functions do not overlap ). Next, other functions, such as the dll_merge () mentioned here, can be implemented using the most basic functions by building blocks.

It should be noted that the dll_merge () implemented in Reuse mode introduces function calls, and function calls may bring a certain processor overhead because of passing parameters, the overhead is related to the processing capability of the processor. However, for most modern processors, such overhead is small and negligible. In addition, if you want to remove the overhead caused by function calls, you can consider using the inline method. Take the implementation of dll_merge () as an example. If dll_push_tail () and dll_pop_head () are defined as inline, then dll_merge () to call these two functions.

This article from "to Jane Li cloud" blog, please be sure to keep this source http://yunli.blog.51cto.com/831344/263932

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.